0

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?

user1015106
  • 45
  • 1
  • 5

2 Answers2

0

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

walther
  • 13,466
  • 5
  • 41
  • 67
  • Okay, I made a `booleanCustomValid_ServerValidate` Event, then I set the button as `booleanCustomValid.Validate();` Will that work? – user1015106 Apr 16 '12 at 20:15
  • Can you update your question with a new code you're experimenting with? Anyway, trying to rewrite 1:1 windows app to web app is usually a very bad thing to do. – walther Apr 16 '12 at 21:06
  • Yeah, I'm starting to think that, too. So have my superiors, since I've been reassigned to a new project, so thanks anyway. – user1015106 Apr 18 '12 at 21:06
0

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.

Tejs
  • 40,736
  • 10
  • 68
  • 86