简体   繁体   中英

Uniquely identify controls in nested gridview for validation

I have a nested grid. I would like to validate a dropdown control within the child grid when the user adds a record. But in a nested grid, the control IDs are not unique. If you have 2 parent rows and a nested grid under each parent the child grid controls will have the same ID. When I validate, the validation is checking all of the nested grids and not just the one that I am trying to add to.

This is the markup:

 <asp:GridView ID="GroupGridView" runat="server" AutoGenerateColumns="False" 
            Caption="Group Information" CaptionAlign="Top" CssClass="grid" 
            ShowFooter="true" DataKeyNames="GroupID">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <a href="javascript:DivExpandCollapse('div<%# Eval("GroupID")%>');">
                        <img id="imgdiv<%# Eval("GroupID")%>" width="25px" border="0" src="Images/plus.png" /> </a> 
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="GroupID">
                    <ItemTemplate>
                        <asp:Label ID="uggvLblGroupID" runat="server" Text='<%# Bind("GroupID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Group Name">
                    <ItemTemplate>
                        <asp:Label ID="uggvLblGroupName" runat="server" Text='<%# Bind("GroupName") %>'></asp:Label>
                    </ItemTemplate>    
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Action" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <asp:Button ID="uggvDeleteButton" runat="server" CausesValidation="False" CommandName="Delete" 
                                        Text="Delete" CssClass="gridActionbutton"  OnClientClick="return confirm('Are you sure you want to delete this Group Information?')" >
                        </asp:Button>
                        <tr><td colspan="100%">  
                        <div id="div<%# Eval("GroupID") %>" style="display:none">
                            <asp:GridView ID="GroupMemberGridView" runat="server" AutoGenerateColumns="false" 
                                  CssClass="grid" ShowFooter="true">
                                <Columns>
                                    <asp:TemplateField HeaderText="MemberID">
                                        <ItemTemplate>
                                            <asp:Label ID="mggvLblMemberID" runat="server" Text='<%# Bind("MemberID") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Member Name" ItemStyle-Wrap="false"> 
                                        <ItemTemplate>
                                            <asp:Label ID="mggvLblMemberName" runat="server" Text='<%# Bind("MemberName") %>'></asp:Label>
                                        </ItemTemplate>               
                                        <FooterTemplate>
                                            <asp:DropDownList ID="mggvDDLMemberName" runat="server" ClientIDMode="Static" 
                                               class="chosen-single" data-placeholder="Choose member…">
                                            </asp:DropDownList>
                                            <asp:RequiredFieldValidator ID="ReqValueDDLMemberInsert" runat="server" InitialValue="0" 
                                                   ControlToValidate="mggvDDLMemberName" ValidationGroup="'<%# "InsertGroupMemberValidation_" + Eval("GroupID") %>' 
                                                    ErrorMessage="Selection required." CssClass="message-error-dropdown">
                                            </asp:RequiredFieldValidator>                                       
                                        </FooterTemplate>
                                    </asp:TemplateField>                                
                                    <asp:TemplateField HeaderText="Action" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">                           
                                        <FooterTemplate>
                                            <asp:Button ID="mggvAddButton" runat="server" CommandName="Add" Text="Add Member" Width="90%"  
                                                CssClass="gridActionbutton" ValidationGroup='<%# "InsertGroupMemberValidation_" + Eval("GroupID") %>'> CausesValidation="true">
                                            </asp:Button>
                                        </FooterTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>
                        </div>
                    </ItemTemplate>             
                    <FooterTemplate>
                        <asp:Button ID="uggvAddButton" runat="server" CommandName="Add" Text="Add Group" Width="90%" CausesValidation="true" 
                                        CssClass="gridActionbutton" ValidationGroup="InsertGroupNameValidation"
                        </asp:Button>
                    </FooterTemplate>
                    </asp:TemplateField>
            </Columns>
        </asp:GridView>

Regardless of which 'Add Member' button I click, the validation is triggered for all of the nested grids because the Validation Groups are not unique.

How can I uniquely identify the ValidationGroup for each nested grid?

Thanks.

UPDATE The ValidationGroup identifier works when adding a member to the first nested group but not to subsequent nested grids. It seems it is still going thru all of the nested grids and not just the one from the 'Add' button you clicked.

//Access Validators and Buttons
        RequiredFieldValidator RequiredFieldValidator1 = (RequiredFieldValidator)e.Row.FindControl("RequiredFieldValidator1");
        RequiredFieldValidator RequiredFieldValidator2 = (RequiredFieldValidator)e.Row.FindControl("RequiredFieldValidator2");
        RequiredFieldValidator RequiredFieldValidator3 = (RequiredFieldValidator)e.Row.FindControl("RequiredFieldValidator3");
        Button Button = (Button)e.Row.FindControl("Button1");

        //Assign validation group to controls.
        RequiredFieldValidator1.ValidationGroup = "Gridview1" + e.Row.RowIndex.ToString();
        RequiredFieldValidator2.ValidationGroup = "Gridview1" + e.Row.RowIndex.ToString();
        RequiredFieldValidator3.ValidationGroup = "Gridview1" + e.Row.RowIndex.ToString();
        Button.ValidationGroup = "Gridview1" + e.Row.RowIndex.ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM