Question

How can I invalidate the page in textchanged event.

I have a simple form with textboxes and a button to submit I would like to disable the button or stop the submission if the text entered is not valid. the validity is to be checked in the textchanged event since I have some db operation to check the validity of the content. If I can somehow invalidate the page in the textchanged event then it might be easier pls give me some easy way to implement this thanks Shomaail

Était-ce utile?

La solution

I was able to resolve my own problem perfectly. I used the customvalidator OnServerValidate Event

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.onservervalidate(v=vs.110).aspx

Now in my TextChanged event I show up a warning if the data entered is not correct and in the button_click event of my submit button I call Page.Validate() that subsequently calls OnServerValidate event handler of each custom validator associated with a text box.

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

Autres conseils

You can set Button Enabled Property to true of false, ie:

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

On Code Behid:

      protected void txtData_TextChanged(object sender, EventArgs e)
        {
           if(txtData.Text == "something")
           {
               btnSave.Enabled = True;
           }
           else
               btnSave.Enabled = False;
        }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top