从按钮onclick事件调用C#函数
我试图从button onclick事件调用函数,但它没有执行该函数。代码如下:从按钮onclick事件调用C#函数,c#,asp.net,C#,Asp.net,我试图从button onclick事件调用函数,但它没有执行该函数。代码如下: <script runat="server"> protected void RegistrationButton_Click(object sender, EventArgs e) { TextBox un = Post0.FindControl("aspxTextBox_UserName") as TextBox; TextBox pwd = Pos
<script runat="server">
protected void RegistrationButton_Click(object sender, EventArgs e)
{
TextBox un = Post0.FindControl("aspxTextBox_UserName") as TextBox;
TextBox pwd = Post0.FindControl("aspxTextBox_Password") as TextBox;
TextBox cpwd = Post0.FindControl("aspxTextBox_ConfirmPassword") as TextBox;
TextBox txtE = Post0.FindControl("aspxTextBox_Email") as TextBox;
TextBox SQ = Post0.FindControl("aspxTextBox_SecurityQ") as TextBox;
TextBox SA = Post0.FindControl("aspxTextBox_SecurityA") as TextBox;
if (pwd == cpwd)
{
System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection("ConString_Online_EMS_AFRICA_db");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT INTO EMSPWD (Username, Password, Email, SecurityQ, SecurityA) VALUES (" + un + ", " + pwd + ", " + txtE + ", " + SQ + ", " + SA + " )";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
Response.Redirect("02_Registration.aspx");
}
else
{
Console.WriteLine("Passwords don't Match");
}
}
</script>
<asp:Button ID="RegistrationButton_Click" runat="server" CssClass="emsafrica-button" Text="Click to Create User Account" ValidationGroup="Login1" onclick="RegistrationButton_Click" TabIndex="7"/>
受保护的无效注册按钮\u单击(对象发送者,事件参数e)
{
TextBox un=Post0.FindControl(“aspxTextBox\u用户名”)作为TextBox;
TextBox pwd=Post0.FindControl(“aspxTextBox\u密码”)作为TextBox;
TextBox cpwd=Post0.FindControl(“aspxTextBox\u ConfirmPassword”)作为TextBox;
TextBox txtE=Post0.FindControl(“aspxtbox\u电子邮件”)作为TextBox;
TextBox SQ=Post0.FindControl(“aspxTextBox\u SecurityQ”)作为TextBox;
TextBox SA=Post0.FindControl(“aspxTextBox\u SecurityA”)作为TextBox;
如果(pwd==cpwd)
{
System.Data.SqlClient.SqlConnection sqlConnection1=新的System.Data.SqlClient.SqlConnection(“ConString_Online_EMS_AFRICA_db”);
System.Data.SqlClient.SqlCommand cmd=new System.Data.SqlClient.SqlCommand();
cmd.CommandType=System.Data.CommandType.Text;
cmd.CommandText=“将值(“+un+”、“+pwd+”、“+txtE+”、“+SQ+”、“+SA+”)插入EMSPWD(用户名、密码、电子邮件、SecurityQ、SecurityA)中”;
cmd.Connection=sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
重定向(“02_Registration.aspx”);
}
其他的
{
Console.WriteLine(“密码不匹配”);
}
}
首先,我会将您的代码放在代码后面,而不是将其嵌入页面中。如果您将它嵌入到页面中,那么我认为您需要将语言指定为C
除此之外,您的代码看起来无法正常工作,如果您对其进行修改,就会受到SQL注入攻击。我建议采取以下步骤:
您访问文本框的方法似乎也非常复杂。您使用
FindControl
而不只是使用控件的名称,这有什么原因吗?我会首先将代码放在后面的代码中,而不是将其嵌入页面中。如果您将它嵌入到页面中,那么我认为您需要将语言指定为C
除此之外,您的代码看起来无法正常工作,如果您对其进行修改,就会受到SQL注入攻击。我建议采取以下步骤:
您访问文本框的方法似乎也非常复杂。您使用
FindControl
而不只是使用控件的名称,这有什么原因吗?当Visual Studio和C在WebForms和MVC中提供了内置完整用户注册系统的非常好的现成模板时,为什么要发明登录和注册代码呢
在创建新的VS项目(我的示例来自VS2012)时,请查看模板并选择
“Visual C#->Web->ASP.NET Web表单应用程序”
或
“Visual C#->Web->ASP.NET MVC 4 Web应用程序->Internet应用程序”
这将为您提供更强大的起点,减少您需要为自己编写的代码量,而且更不容易受到SQL注入攻击。为什么要发明登录和注册代码,而Visual Studio和C#在WebForms和MVC中提供了内置完整用户注册系统的非常好的现成模板 在创建新的VS项目(我的示例来自VS2012)时,请查看模板并选择 “Visual C#->Web->ASP.NET Web表单应用程序” 或 “Visual C#->Web->ASP.NET MVC 4 Web应用程序->Internet应用程序”
这将为您提供更强大的起点,减少您需要为自己编写的代码量,并显著降低SQL注入攻击的可能性。您的密码是明文,并且您没有在SQL语句中使用参数。Yikes.C#代码是否在您的脚本标记中?请在声明按钮的位置发布.aspx代码。@juan.facorro,他在最下面。如果单击按钮后未触发事件,则可能是客户端验证程序阻止表单提交。只是猜测一下,我刚刚注意到您的上有ValidationGroup=“Login1”。您的密码是明文,并且您没有在SQL语句中使用参数。Yikes.C#代码是否在您的脚本标记中?请在声明按钮的位置发布.aspx代码。@juan.facorro,他在最下面。如果单击按钮后未触发事件,则可能是客户端验证程序阻止表单提交。只是一个猜测,我刚刚注意到你的ValidationGroup=“Login1”。打得好。通过查看LoginControl和asp.net成员资格,他还可以在事后获得许多预构建的内容。这是你想要使用预构建的Microsoft东西的时候之一,因为它可以防止很多安全漏洞。打得好。通过查看LoginControl和asp.net成员资格,他还可以在事后获得许多预构建的内容。这是您希望使用预构建的Microsoft stuff的时候之一,因为它可以防止许多安全漏洞。