我正在尝试创建注册表单,每次单击提交按钮时,我使用的属性总是返回空值,因此一旦数据库中的数据显示空行
按钮和格式的HTML代码:
<div class="tab-2 resp-tab-content" aria-labelledby="tab_item-1">
<div class="facts">
<div class="register">
<form action="#" method="post">
<asp:TextBox ID="txt_emailreg" runat="server" placeholder="Email Address:" type="text" required="" EnableViewState= "false"></asp:TextBox>
<asp:TextBox ID="txt_passwordreg" runat="server" placeholder="Password:" type="password" required="" EnableViewState= "false"></asp:TextBox>
<asp:TextBox ID="txt_fName" runat="server" placeholder="First Name:" type="text" required="" EnableViewState= "false"></asp:TextBox>
<asp:TextBox ID="txt_lName" runat="server" placeholder="Last Name:" type="text" required="" EnableViewState= "false"></asp:TextBox>
<asp:TextBox ID="txt_contactNumber" runat="server" placeholder="Contact Number:" type="text" required="" EnableViewState= "false"></asp:TextBox>
<asp:RadioButton ID="rbtn_Male" Text="Male" runat="server"/>
<asp:RadioButton ID="rbtn_Female" Text="Female" runat="server"/>
<div class="sign-up">
<asp:Button ID="btn_register" runat="server" Text="Register Account" type="submit" OnClick="btn_register_Click" UseSubmitBehavior="false" data-dismiss="modal" />
<input type="submit" value="Create Account"/>
</div>
</form>
</div>
</div>
</div>
c#code:
public string RegisterEmail
{
get { return txt_emailreg.Text; }
set { txt_emailreg.Text = value; }
}
public string RegisterPassword
{
get { return txt_passwordreg.Text; }
set { txt_passwordreg.Text = value; }
}
public string FirstName
{
get { return txt_fName.Text; }
set { txt_fName.Text = value; }
}
public string LastName
{
get { return txt_lName.Text; }
set { txt_lName.Text = value; }
}
public string ContactNumber
{
get { return txt_contactNumber.Text; }
set { txt_contactNumber.Text = value; }
}
public string Gender
{
get
{
if (rbtn_Female.Checked)
{
return "F";
}
else if (rbtn_Male.Checked)
{
return "M";
}
else
{
return "";
}
}
set
{
if (value == "F")
{
rbtn_Female.Checked = true;
rbtn_Male.Checked = false;
}
else if (value == "M")
{
rbtn_Female.Checked = false;
rbtn_Male.Checked = true;
}
}
}
protected void btn_register_Click(object sender, EventArgs e)
{
try
{
SqlConnection connAccount = new SqlConnection(connString);
connAccount.Open();
string sqlInsertStatement = @"insert into accountsTB values ('" +
RegisterEmail + "','" +
RegisterPassword + "','" +
FirstName + "','" +
LastName + "','" +
ContactNumber + "','" +
Gender + "')";
SqlCommand cmdTxt = new SqlCommand(sqlInsertStatement, connAccount);
cmdTxt.ExecuteNonQuery();
connAccount.Close();
connAccount.Dispose();
}
catch (Exception ex)
{
}
}
答案 0 :(得分:1)
在您的问题中,您已发布按钮单击事件的代码,但从未指定将值分配给方法中的变量的位置,请查看示例如何将文本框值分配给局部变量
RegisterEmail = txt_emailreg.Text;