0

I am trying to build a web application that requires users to register themselves. I added custom validators and other validators according to my need.

Part of the code in .aspx file

<form id="form" name="form" action="../Hi.aspx" method="post">
<table cellspacing="4" class="style1">
    <tr>
<td class="style4">
<asp:TextBox ID="TxtFirstName" runat="server" Width="157px"></asp:TextBox>
</td>
td class="style5" id="FName">
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TxtFirstName" 
 ErrorMessage="Your First Name should be at least 2 characters long" 
 onservervalidate="CustomValidator1_ServerValidate" ForeColor="Red" 
 ValidateEmptyText="True"></asp:CustomValidator>...

and correspnding code in .aspx.cs file is

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = (args.Value.Length>1);
        }

This works fine when I run only this part of the application.

Now, I want to retrieve the values of all the text fields and store them in a database.

In aspx.cs file, i wrote code as

 protected void ButtonRegister_Click(object sender, EventArgs e)
 {
      string fname = TxtFirstName.Text;
      Controllers.RegistrationController r = new Controllers.RegistrationController();
      int a = r.registerData(fname);
      if (a==1) {
           Response.Redirect("../Hi.aspx");
      }
 }     

which is called when the submit button is clicked.

The registerData() method in the RegistrationController that established connection with the database and stores the form values. The connection is established correctly and the values are retrieved and stored. But, the problem is, when i call the registerData() method from the method ButtonRegister_Click, all the validation that I have written doesn't work. Anything that is entered in the form gets stored into the database without validation.

How do I retrieve the values and store them and at the same time ensure that they are being validated?

I am new to .net, so any help is appreciated.

Thank you very much in advance.

1 Answer 1

2

You could call Page.Validate within your click method and check the result of that or specifying CausesValidation on your button should cause the valdation to run

In the long term though you might want to look at moving the rules to a lower lower (ie. business logic) such that when/if you move to supporting services you wont have to reimplement the rules in those services, of course if you're not ever planning to do that getting away with them on the ui may suffice

Sign up to request clarification or add additional context in comments.

2 Comments

Page.Validate isnt working. Is there any other method? Or would it be possible to call a .js file from .aspx for validation?
I got it by adding if (!Page.IsValid) { return; } in the click method. Thank you very much for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.