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.