C# TextBox.Validating Cancel Event for ASPX

1.3k Views Asked by At

I'm trying to convert code from a WinForm to a WinApp, but I know very little of aspx, and nothing of javascript.

I have programmatically created a textbox that will be given text. The text is then passed through a validation method like so:

text.Validating += new CancelEventHandler(boolean_Validating);

goes to

    private void boolean_Validating(object sender, CancelEventArgs e)
    {
        TextBox textBox = (TextBox)sender;
        string boolean = textBox.Text;

        string message = null;
        if (!checkBooleanSyntax(boolean, out message))
        {
            Response.Write("Error: " + message);
            e.Cancel = true;

            textBox.Text = message;
        }
    }

ASPX doesn't have a definition for the text.Validating part. How else can I validate the text using a Cancel Event Handler?

2

There are 2 best solutions below

3
walther On

In asp.net we use validation controls. Those can take care of the job on client side (if user hasn't disable javascript in his browser) and then on the server side (so you can be sure that user won't just disable js and can immediately freely exploit your app).

If you need some special validation with specific logic, you can write yourself a custom validator control.

Can't write everything here, because this topic can be covered by multiple chapters in a book, but look here for more information: http://www.w3schools.com/aspnet/aspnet_refvalidationcontrols.asp

http://msdn.microsoft.com/en-us/library/aa479013.aspx

5
Tejs On

It seems you are falling prey to being used to stateful applications for a long time. The web server doesn't cancel the request - you simply take the information given to you, and then send them a response.

So, if you wanted to validate the text of a text box, you typically would have a button to submit to the server, and then that button click code takes the value and validates it. If invalid, you mark up the page some in HTML or show some controls, and then render that information back to the user's browser.

Example:

 <asp:TextBox ID="textName" runat="server" CssClass="someTextYo" />
 <asp:Panel ID="panelError" runat="server" Visible="false">
     You entered bad stuff, yo
 </asp:Panel>
 <asp:Button ID="buttonSubmit" runat="server" Click="buttonSubmit_Click" />

In your code then:

 protected void buttonSubmit_Click(object sender, EventArgs e)
 {
     if(string.IsNullOrEmpty(textName.Text))
     {
         panelError.Visible = true;
     }
     else
     {
         // Save to Database, whatever
     }
 }

Additionally, you could validate these components on the client side with javascript, and thus remove the need for the round trip to the server (although, you should still validate the data server side too). For example, with jQuery:

 $('form').submit(function()
 {
     if($.trim($('.someTextYo').val()) == '')
        return false;

     return true;
 });

You can cancel the form post by proving a form submit callback that returns false.