Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In web form I used few textboxes and a button. I have used a Custom validator against a textbox which validated if Date of Birth is less than 18 years. If we choose date less than 18 years it will display an error. But the problem is Button click event is also executing. What can I do? Please advice.

XML
Date Of&nbsp; Birth:</td>
<td style="width: 292px">
    <asp:TextBox id="TextBox3" runat="server" onkeypress="javascript:return false;"ValidationGroup="a">
    </asp:TextBox>
    <asp:calendarextender id="CalendarExtender2" targetcontrolid="TextBox3" runat="server">
    </asp:calendarextender>
    <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox3" ErrorMessage="CustomValidator" OnServerValidate="CustomValidator1_ServerValidate" Display="None" ValidationGroup="a">
    </asp:CustomValidator>

    <asp:ValidatorCalloutExtender ID="ValidatorCalloutExtender3" runat="server" TargetControlID="RequiredFieldValidator3">
    </asp:ValidatorCalloutExtender>
    <asp:ValidatorCalloutExtender ID="ValidatorCalloutExtender7" runat="server" TargetControlID="CustomValidator1">
    </asp:ValidatorCalloutExtender>

    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
    ErrorMessage="RequiredFieldValidator"  Display="None"
    ControlToValidate="TextBox3" SetFocusOnError="True" ValidationGroup="a">
    </asp:RequiredFieldValidator>
</td>

C#
protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string constr;
            constr = WebConfigurationManager.ConnectionStrings["JAPITConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);

            con.Open();
            string str = "insert   into  Doe_detail values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')";
            SqlCommand cmd = new SqlCommand(str, con);
            cmd.ExecuteNonQuery();
            con.Close();
            Label2.Visible = true;
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";
         
        }
        catch (Exception es)
        {
        }
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        DateTime dtStart = DateTime.Parse(TextBox3.Text);
        TimeSpan sp = DateTime.Now - dtStart;

        if (sp.Days < 18 * 365)
        {
            args.IsValid = false;
            CustomValidator1.ErrorMessage = "You can not be under 18 years";
        }
        else
            args.IsValid = true;
    }
Posted
Updated 3-Apr-12 23:32pm
v2
Comments
Nilesh Patil Kolhapur 4-Apr-12 5:00am    
it happen because u write server side event try javascript or compare validator k if any doubt give rpy
Mohamed Mitwalli 4-Apr-12 14:36pm    
you are right

1 solution

For simple validations, you should be using Javascript since it does not require a postback to the server.

To answer your question, the reason why the code on the Button1_Click is still running is because you have not checked the value of the Page.IsValid property. You might want to add it on your code, like this:

C#
protected void Button1_Click(object sender, EventArgs e)
{
    if(Page.IsValid)
    {
        try
        {
            string constr;
            constr = WebConfigurationManager.ConnectionStrings["JAPITConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);
            
            con.Open();
            string str = "insert   into  Doe_detail values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')";
            SqlCommand cmd = new SqlCommand(str, con);
            cmd.ExecuteNonQuery();
            con.Close();
            Label2.Visible = true;
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";
        }
        catch (Exception es)
        {
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900