1

具有 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;
            }

    }

所以请告诉我该怎么办?

4

2 回答 2

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>
于 2012-09-30T09:13:04.123 回答
0

还需要在服务器端设置控件 IsValid 状态:

  • CustomValidator控件添加到表单中;

  • 将 CustomValidator.ControlToValidate 设置为“SerialFrom_txt”;

  • 处理 CustomValidator.ServerValidate 事件而不是 SerialFrom_txt_TextChanged 事件;

  • 在 ServerValidate EventArgs args.IsValid 属性中设置控件 IsValid 状态。

于 2012-09-30T09:14:20.440 回答