我在验证客户端名为“男性”、“女性”的单选按钮时遇到问题。我不想使用单选按钮列表,有人可以检查我的代码并纠正它吗?谢谢。
这是 .aspx 页面:
<script language="javascript" type="text/javascript" >
function ClientValidate(source, e) {
if (document.getElementById("content_radmale").checked == true || document.getElementById("content_radfemale").checked == true) {
e.IsValid = true;
}
else {
e.IsValid = false;
}
// alert("Enter gender !");
}
</script>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="please enter any option" ClientValidationFunction = "ClientValidate"
ValidationGroup="gender" ControlToValidate="txtname"></asp:CustomValidator>
和 .cs 页面:
string gender = radmale.Text;
if (radfemale.Checked)
{
gender = radfemale.Text;
}
CustomValidator1.Validate();
string sql = "INSERT INTO registration(emailaddress,username,password,name,gender)VALUES ('"+email+"','"+username+"','"+pass+"','"+name+"','"+gender+"') ";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();