1

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?

Dhinesh
  • 181
  • 2
  • 5
  • 20

2 Answers2

4

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.

Zam
  • 2,880
  • 1
  • 18
  • 33
Sachin
  • 40,216
  • 7
  • 90
  • 102
3
<asp:TextBox id="txt_name" runat="server"ValidationGroup="check"/>
<asp:RequiredFieldValidator
 ControlToValidate="txt_name"
 ErrorMessage="Name"
 Text="*"
 runat="server"/>    
<asp:Button id="b1" Text="Submit" runat="server" ValidationGroup="check"/>

<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"/>

This will work as I have been using the same approach.

Incredible
  • 3,495
  • 8
  • 49
  • 77