How to use validation summary for particular button in asp.net?

Dhinesh picture Dhinesh · May 28, 2014 · Viewed 22.2k times · Source

I have a page with controls as shown below,

<asp:TextBox id="txt_name" runat="server"/>
<asp:RequiredFieldValidator
 ControlToValidate="txt_name"
 ErrorMessage="Name"
 Text="*"
 runat="server"/>    
<asp:Button id="b1" Text="Submit" runat="server"/>
<asp:Button id="b2" Text="Clear" runat="server"/>
<asp:ValidationSummary
 HeaderText="You must enter a value in the following fields:"
 DisplayMode="BulletList"
 EnableClientScript="true"
 runat="server"/>

How can I use validation summary only for Submit button?

Answer

Sachin picture Sachin · May 28, 2014

You can either use ValidationGroup or CausesValidation = "false" for Clear button.

Using CausesValidation

<asp:Button id="b2" Text="Clear" runat="server" CausesValidation="false" />

By using this the button b2 won't trigger validation.

In the second approach you can use the ValidationGroup property on each control which you want to include in the validation.