1

I have an asp.net GridView with a DropwDownlist control and a bunch of other textboxes for text entry etc. The user can add multiple rows to the grid and change existing data etc.

I need the RequiredFieldValidator to fire only for the current row being accessed. I've set the InitialValue to the correct value and I get the validator to fire, the problem I'm having is it fires for every row instead of just the one currently being worked on.

Any ideas?

My validator under the DropDown in the GridView:

<asp:RequiredFieldValidator ID="rfRoles" runat="server" ErrorMessage="Role Required"
     ControlToValidate="ddlRole" ValidationGroup=roles InitialValue="-1" Display="Dynamic" SetFocusOnError="true" Text="*">
</asp:RequiredFieldValidator>
<asp:ValidationSummary ID="validateRole" runat="server" ShowMessageBox="True" ShowSummary="false" ValidationGroup="roles" />

My save button in the grid:

<EditItemTemplate>
<asp:LinkButton ID="lnkSave" runat="server" Text="Save" CommandName="Update" CausesValidation="true" ForeColor="#AE0917" ValidationGroup="roles" Font-Bold="true"></asp:LinkButton></EditItemTemplate>
Melanie
  • 634
  • 2
  • 10
  • 29

1 Answers1

2

I believe you may be able to do something like this:

<EditItemTemplate>
<asp:RequiredFieldValidator ID="rfRoles" runat="server" ErrorMessage="Role Required"
    ControlToValidate="ddlRole" ValidationGroup="roles<%# Eval("Id") %>" InitialValue="-1" Display="Dynamic" SetFocusOnError="true" Text="*">
</asp:RequiredFieldValidator>
<asp:ValidationSummary ID="validateRole" runat="server" ShowMessageBox="True" ShowSummary="false" ValidationGroup="roles<%# Eval("Id") %>" />

<asp:LinkButton ID="lnkSave" runat="server" Text="Save" CommandName="Update" CausesValidation="true" ForeColor="#AE0917"ValidationGroup="roles<%# Eval("Id") %>" Font-Bold="true"></asp:LinkButton>
</EditItemTemplate>

Where "Id" is an identifier for your data row, such as the primary key of the data row you are editing. This way, you give each row a unique validation group.

P.S. The syntax may need a little work, I did it from memory.

SouthShoreAK
  • 3,844
  • 1
  • 21
  • 42
  • 1
    I found the correct syntax that worked for me via this post: http://stackoverflow.com/questions/6012655/how-to-use-data-bound-value-to-form-unique-identifier-for-item-control So points all around! Thanks very much, I really appreciate it! – Melanie Apr 23 '12 at 17:02