简体   繁体   English

在嵌套的gridview中唯一标识控件以进行验证

[英]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. 但是在嵌套网格中,控件ID不是唯一的。 If you have 2 parent rows and a nested grid under each parent the child grid controls will have the same ID. 如果您有2个父行,并且每个父项下都有一个嵌套网格,则子网格控件将具有相同的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? 如何唯一标识每个嵌套网格的ValidationGroup?

Thanks. 谢谢。

UPDATE The ValidationGroup identifier works when adding a member to the first nested group but not to subsequent nested grids. 更新将成员添加到第一个嵌套组但不添加到后续嵌套网格时,ValidationGroup标识符将起作用。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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