将AutoPostback设置为true并使用验证器进行下拉。 当我从下拉列表中选择“无效”值时,验证器消息 简要说明,然后发生回发并且错误消息消失了! 这是我的代码:
<asp:TextBox ID="SerialFrom_txt" runat="server" BackColor="White" BorderColor="#188F3E"
BorderStyle="Solid" BorderWidth="3px" Width="200px" OnTextChanged="SerialFrom_txt_TextChanged"
AutoPostBack="true"></asp:TextBox>
<asp:RegularExpressionValidator ID="SerialFrom_rv" runat="server" ValidationExpression="^[a-zA-Z]{2}[0-9]{7}$"
Text="error" ControlToValidate="SerialFrom_txt" />
我后面的帖子代码是:
protected void SerialFrom_txt_TextChanged(object sender, EventArgs e)
{
if (string.Compare(RoleFrom_lbl.Text, SerialFrom_txt.Text) > 0)
{
Error_lbl.Text = "Serial From must be greater than " + RoleFrom_lbl.Text + "!";
SerialFrom_txt.BackColor = System.Drawing.Color.FromName("red");
SAVE_btn.Enabled = false;
}
else
{
Error_lbl.Text = "";
SerialFrom_txt.BackColor = System.Drawing.Color.FromName("white");
SAVE_btn.Enabled = true;
}
}
所以请告诉我该怎么办?
答案 0 :(得分:3)
尝试将CausesValidation =“true”添加到文本框中,如下所示:
<asp:TextBox CausesValidation="true" ID="SerialFrom_txt" runat="server" BackColor="White" BorderColor="#188F3E" BorderStyle="Solid" BorderWidth="3px" Width="200px" OnTextChanged="SerialFrom_txt_TextChanged" AutoPostBack="true"></asp:TextBox>
答案 1 :(得分:0)
还需要在服务器端设置控制IsValid状态:
将CustomValidator控件添加到表单中;
将CustomValidator.ControlToValidate设置为“SerialFrom_txt”;
处理CustomValidator.ServerValidate事件而不是SerialFrom_txt_TextChanged事件;
在ServerValidate EventArgs args.IsValid属性中设置控件IsValid状态。