Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.net如何在textchanged事件中失效_C#_Asp.net_Validation_Customvalidator - Fatal编程技术网

C# ASP.net如何在textchanged事件中失效

C# ASP.net如何在textchanged事件中失效,c#,asp.net,validation,customvalidator,C#,Asp.net,Validation,Customvalidator,如何在textchanged事件中使页面无效 我有一个带有文本框和按钮的简单表单要提交 如果输入的文本无效,我想禁用按钮或停止提交。 有效性将在textchanged事件中检查,因为我有一些db操作来检查内容的有效性。 如果我可以在textchanged事件中以某种方式使页面无效,那么可能会更容易 请给我一些简单的方法来实现这一点 谢谢 Shomaail您可以将按钮启用属性设置为true或false,即: <asp:TextBox runat="server" ID="txtData" O

如何在textchanged事件中使页面无效

我有一个带有文本框和按钮的简单表单要提交 如果输入的文本无效,我想禁用按钮或停止提交。 有效性将在textchanged事件中检查,因为我有一些db操作来检查内容的有效性。 如果我可以在textchanged事件中以某种方式使页面无效,那么可能会更容易 请给我一些简单的方法来实现这一点 谢谢
Shomaail

您可以将按钮启用属性设置为true或false,即:

<asp:TextBox runat="server" ID="txtData" OnTextChanged="txtData_TextChanged"
                                            AutoPostBack="true"></asp:TextBox>
 <asp:Button runat="server" ID="btnSave" OnClik="btnSave_Click"></asp:Button>

我能够完美地解决我自己的问题。我使用了customvalidator OnServerValidate事件

现在,在我的TextChanged事件中,如果输入的数据不正确,我将显示一条警告,并在我的submit按钮的button_click事件中调用Page.Validate(),该事件随后调用与文本框关联的每个自定义验证器的OnServerValidate事件处理程序

 protected void btnIssueItem_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (!Page.IsValid)
            return;
....
}


protected void tbRoomID_CustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        BAL bal = new BAL();
        args.IsValid = bal.GetRoomByRoomID(Int32.Parse(args.Value)).Count == 0 ? false : true;
    }

为什么?TextChanged事件仅在回发到服务器时触发(例如,当用户按下按钮时)。验证器的全部意义在于,在字段有效之前,您不会回发到服务器(假设启用了客户端验证)。我假设您使用的是验证程序控件。但我需要解决我的问题。有无回邮。txtbox内容必须使用dbx进行检查。如果有两个或多个txt框,则此操作将失败。任何txtbox都将使按钮启用,用户将有机会收到错误。如何验证数据?正则表达式或某种字符串比较?一旦我们在txtData_TextChanged中,我们可以通过字符串比较或正则表达式或任何其他方式进行检查。重点问题是,我需要有一种方法,如果txtbox中的任何一个数据都不正确,那么页面提交就变得不可能。我需要用一些DB操作来检查txtbox的内容
 protected void btnIssueItem_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (!Page.IsValid)
            return;
....
}


protected void tbRoomID_CustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        BAL bal = new BAL();
        args.IsValid = bal.GetRoomByRoomID(Int32.Parse(args.Value)).Count == 0 ? false : true;
    }