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
- Add controls on the page
- Write a code to set the validationGroup of the button.
- Output
Software Requirement
Prerequisite Knowledge
- Basics about asp.net programming.
- How to create a project using visual studio?
- What are different server controls in asp.net?
- Basics about control’s events.
- What are validation controls in asp.net?
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;
}
- On page load we are assigning the ValidationGroup of button equal to the validation group of txtName1.
- On click of button (with caption ‘Change Validation Group’) we are changing the validation group of button depends upon what value is entered in the textbox.
Output
Click to Enlarge
Click to Enlarge