Search code examples
c#asp.netvalidationcustomvalidator

ASP.net How can I invalidate in textchanged event


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


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