Melanie
Melanie

Reputation: 584

DropDownList RequiredFieldValidator in GridView

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>

Upvotes: 1

Views: 3693

Answers (1)

SouthShoreAK
SouthShoreAK

Reputation: 4296

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.

Upvotes: 2

Related Questions