Articles → .NET → Invoke different validation controls from a single button using ValidationGroup property in asp.net

Invoke different validation controls from a single

button using ValidationGroup property in asp.net




















Software requirement





Prerequisite knowledge















What is the purpose of ValidationGroup property in asp.net?





Create a new project





Click to Enlarge


Add controls on the page





Click to Enlarge


Write a code to set the validationGroup of the button




Name1:<asp:TextBox ID="txtName1" runat="server"></asp:TextBox>
<asp:Button ID="btnTest" runat="server" Text="Button" />
    
<br />
   Name2:<asp:TextBox ID="txtName2" runat="server"></asp:TextBox><br />

   <asp:RequiredFieldValidator ID="rfvName1" runat="server" ValidationGroup="name1" ControlToValidate="txtName1" ErrorMessage="Please enter name1"></asp:RequiredFieldValidator>
   <asp:RequiredFieldValidator ID="rfvName2" runat="server" ValidationGroup="name2" ControlToValidate="txtName2" ErrorMessage="Please enter name2"></asp:RequiredFieldValidator>

   <br />
   <br />
   <br />
   Change Validation Group: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br />
   <asp:Button ID="btnChangeValidationGroup" runat="server" onclick="btnChangeValidationGroup_Click" Text="Change Validation Group" />




protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
            btnTest.ValidationGroup = "name1";
}

protected void btnChangeValidationGroup_Click(object sender, EventArgs e)
{
        if (!string.IsNullOrEmpty(TextBox1.Text))
            btnTest.ValidationGroup = TextBox1.Text;
}









Output





Click to Enlarge




Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Friday, February 08, 2013

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250