0

I have two buttons in a page. The function of the first button(Button3) is to send a confirmation mail to the mail ID in the first Textbox1. The function of second button(Button4) is to insert values into a database from textbox3 and textbox4 . I kept a required field validator for the first textbox(textbox1). So if I leave it blank and press Button3, it shows warning message that field cannot be left blank. But if the click Button4 even then it shows the same warning message that the field cannot be blank beside textbox1. Why is that so? How can I restrict the validation only for Button3 but not for the Button4?

Here's my C# code to make u understand what exactly I am talking about.

protected void Button3_Click(object sender, EventArgs e)
    {
        MailMessage objMail = new MailMessage("[email protected]", TextBox1.Text, "confirm", TextBox2.Text);
        NetworkCredential objNC = new NetworkCredential("[email protected]", "mypassword");
        SmtpClient objsmtp = new SmtpClient("smtp.gmail.com", 587);
        objsmtp.EnableSsl = true;
        objsmtp.Credentials = objNC;
        objsmtp.Send(objMail);
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        try
        {
            String sCon = "SERVER=xyz;DATABASE=xyz;UID=xyz;password=xyz";
            MySqlConnection con = new MySqlConnection(sCon);
            MySqlCommand command = con.CreateCommand();
            command.CommandText = "insert into criminals values('"+TextBox3.Text +"','"+ TextBox4.Text + "')";
            con.Open();
            command.ExecuteNonQuery();
            con.Close();

        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
}
3
  • 1
    This code is vulnerable to sql injection. It's practically begging to get hacked. Commented Nov 10, 2013 at 15:09
  • You can use the ValidationGroup property. The main concept is to assign specific validators with a specific button. Could you post your markup so that we could give you an example using ValidationGroup? Commented Nov 10, 2013 at 15:11
  • Button3, Button4, textbox1, textbox3, textbox4. Wonderful naming standard you have there. Commented Nov 10, 2013 at 15:26

2 Answers 2

9

What you need is validationGroup property The required Filed Validator will have the validationGroup .

specify the different validationGroup names for both validators.

Example:

here i'm creating TextBox with RequiredFeildValidator with ValidationGroup name Group1 associated to Button1 so that Button1 only verifies the controls which are assigned to validation Group Group1

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>


<asp:RequiredFieldValidator 
ID="RequiredFieldValidator1" 
runat="server" 
ErrorMessage="RequiredFieldValidator"
ValidationGroup="Group1" 
ControlToValidate="TextBox1">
</asp:RequiredFieldValidator>

<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="Group1" />
Sign up to request clarification or add additional context in comments.

Comments

0

You can set up different validation groups for different sections of your page. When you use the button for one validation group, it will only check the validation controls that belong to that group.

Comments

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.