0

This is my aspx code:

    <asp:TemplateField HeaderText="Enter_Quantity" >
    <ItemTemplate>
    <asp:TextBox ID ="TextBox1" runat="server" DataField="Total_Quantity" />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Please add a quantity"> </asp:RequiredFieldValidator>
   </ItemTemplate>
   </asp:TemplateField>

   <asp:TemplateField>
    <ItemTemplate>
    <asp:Button ID ="Button7" runat="server" OnClick="Button7_Click" CommandArgument="Button7" CommandName="Select" Text="Add To Cart" CausesValidation ="true" />
    </ItemTemplate>
    </asp:TemplateField>

Kindly note that the Button is in every Gridview Row and also the textbox column is in every Gridview row. The problem I'm facing is that when I keep the textbox empty and then click on the button. It is showing the error message "Please add a quantity". But it is showing it in every Gridview row. I want to show this error message for that specific GridView row. How do I solve this ? Can someone correct my aspx code ?

  • do you see that ? http://stackoverflow.com/questions/30433490/testbox-validation-in-specific-row-in-the-gridview and that http://www.aspsnippets.com/Articles/Validate-only-certain-selected-GridView-Row-Controls-like-TextBox-using-ASPNet-Validators.aspx – Aristos Feb 28 '17 at 06:32

1 Answers1

2

Set CauseValidation = "false".

It allows you to remove validation from specific control. Suppose you're using two buttons in your page but you want only one button to validate. But as per ASP rules all button will validate your form because of CausesValidation = true by default.

Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
Adi
  • 36
  • 2
  • it allows you to remove validation from specific control.. take suppose you're using two buttons in your page but you want only one button to validate. But as per ASP rules all button will validate your form because of CausesValidation= true by default. – Adi Oct 16 '17 at 10:34
  • 1
    Welcome to Stack Overflow! Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight Oct 16 '17 at 12:49