Search code examples
c#asp.netvalidationsummary

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


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?


Solution

  • 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.