<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtbx1" ValidateEmptyText="true" OnServerValidate="validating" Text="Please Enter Name!" ></asp:CustomValidator>
我已经尝试过了,只是可以了,但是我不知道如何阻止数据输入!。
protected void Button1_Click(object sender, EventArgs e)
{
string a = txtbx1.Text;
string b = txtbx2.Text;
string c = txtbx3.Text;
string d = txtbx4.Text;
string es = txtbx5.Text;
string f = txtbx6.Text;
if (txtbx1.Text.ToString().Equals(a) || txtbx2.Text.ToString().Equals(b) || txtbx3.Text.ToString().Equals(c) || txtbx4.Text.ToString().Equals(d) || txtbx5.Text.ToString().Equals(es) || txtbx6.Text.ToString().Equals(f))
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["cs"].ConnectionString);
using (conn)
{
SqlCommand cmd = new SqlCommand("addstd", conn);
cmd.Parameters.AddWithValue("@name", txtbx1.Text);
cmd.Parameters.AddWithValue("@fathername", txtbx2.Text);
cmd.Parameters.AddWithValue("@enrollment", txtbx3.Text);
cmd.Parameters.AddWithValue("@email", txtbx4.Text);
cmd.Parameters.AddWithValue("@batchcode", txtbx5.Text);
cmd.Parameters.AddWithValue("@semester", txtbx6.Text);
conn.Open();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
}
Response.Write("<script language=javascript>alert('Inserted Successfully!')</script>");
}
catch (Exception)
{
throw;
}
}
else if (txtbx1.Text.Trim() == string.Empty || txtbx2.Text.Trim() == string.Empty ||txtbx3.Text.Trim() == string.Empty || txtbx4.Text.Trim() == string.Empty || txtbx5.Text.Trim() == string.Empty || txtbx6.Text.Trim() == string.Empty)
{
Label1.Visible = true;
Label1.Text = "Please Enter Complete Information!";
}
}
这是我尝试过的完整代码,但没有用,即使文本框为空,它仍然会显示错误消息,而且还会提交数据
答案 0 :(得分:0)
我不明白为什么要进行如下检查。
if (txtbx1.Text.ToString().Equals(a) || txtbx2.Text.ToString().Equals(b) || txtbx3.Text.ToString().Equals(c) || txtbx4.Text.ToString().Equals(d) || txtbx5.Text.ToString().Equals(es) || txtbx6.Text.ToString().Equals(f))
{
// your code
}
将上述代码替换为您的else部分。
if (txtbx1.Text.Trim() == string.Empty || txtbx2.Text.Trim() == string.Empty ||txtbx3.Text.Trim() == string.Empty || txtbx4.Text.Trim() == string.Empty || txtbx5.Text.Trim() == string.Empty || txtbx6.Text.Trim() == string.Empty)
{
Label1.Visible = true;
Label1.Text = "Please Enter Complete Information!";
}
else
{
//database save logic
}
除此之外,必需的字段验证器应该可以工作。