从嵌套GridView

时间:2017-06-01 22:09:07

标签: c# asp.net gridview radiobuttonlist

我有一个嵌套的GridView到5级(对于演示我给了2个级别)我很难访问在GridView的第二级选择哪个RadioButtonList。 (第一级被命名为gvSecondLevel,所以请不要混淆)。我现在必须从gvThirdLevel 访问(实际上是二级网格,你会在代码中看到)。< / em>的

这是aspx代码:

<table align="left">
<tr>
    <td colspan="2">
        <asp:GridView ID="gvSecondLevel" runat="server" AutoGenerateColumns="False" Width="800px"
            ShowFooter="True" BorderStyle="none" DataKeyNames="CallCategory_ID" OnRowDataBound="gvSecondLevel_OnRowDataBound"
            OnRowCommand="gvSecondLevel_RowCommand" OnRowDeleting="gv_RowDeleting" AllowPaging="true"
            PageSize="10" OnPageIndexChanging="gvSecondLevel_PageIndexChanging" OnRowCreated="gvSecondLevel_RowCreated" >
            <Columns>
                <asp:TemplateField ItemStyle-Width="20px">
                    <ItemTemplate>
                        <a href="JavaScript:divexpandcollapse('div<%# Eval("CallCategory_ID") %>','<%# Eval("CallCategory_Name") %>','td_div<%# Eval("CallCategory_ID") %>');">
                            <img id="imgdiv<%# Eval("CallCategory_ID") %>" border="0" src="ProjectImages/expandDown.png"
                                alt="" /></a>
                    </ItemTemplate>
                    <ItemStyle Width="20px" VerticalAlign="Middle"></ItemStyle>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ID" HeaderStyle-Width="20px">
                    <ItemTemplate>
                        <asp:Label ID="lblBusinessID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"CallCategory_ID") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle Width="20px" />
                </asp:TemplateField>
                <asp:BoundField DataField="CallCategory_Name" HeaderText="Call Category Name 2nd" />
                <asp:TemplateField HeaderText="" ItemStyle-Width="20px">
                    <ItemTemplate>
                        <asp:LinkButton ID="lbtnDeleteSecondLevel" CommandArgument='<%# Eval("CallCategory_ID") %>'
                            CommandName="Delete" runat="server"></asp:LinkButton>
                    </ItemTemplate>
                    <ItemStyle Width="20px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderStyle-BorderStyle="None" FooterStyle-BorderStyle="None"
                    ItemStyle-BorderStyle="None">
                    <ItemTemplate>
                        <tr>
                            <td id="td_div<%# Eval("CallCategory_ID") %>" colspan="100%" style="border: none;">
                                <div id="div<%# Eval("CallCategory_ID") %>" style="overflow: auto; display: none;
                                    position: relative; left: 15px; overflow: auto">
                                    <asp:GridView ID="gvThirdLevel" runat="server" Width="95%" AutoGenerateColumns="false"
                                        ShowFooter="true" BorderStyle="none" DataKeyNames="CallCategory_ID" OnRowDataBound="gvThirdLevel_OnRowDataBound"
                                        OnRowCommand="gvThirdLevel_RowCommand" OnRowDeleting="gv_RowDeleting">
                                        <Columns>
                                            <asp:TemplateField ItemStyle-Width="20px">
                                                <ItemTemplate>
                                                    <a href="JavaScript:divexpandcollapseThird('div1<%# Eval("CallCategory_ID") %>','<%# Eval("CallCategory_Name") %>','td_div1<%# Eval("CallCategory_ID") %>');">
                                                        <img id="imgdiv1<%# Eval("CallCategory_ID") %>" border="0" src="ProjectImages/expandDown.png"
                                                            alt="" /></a>
                                                </ItemTemplate>
                                                <ItemStyle Width="20px" VerticalAlign="Middle"></ItemStyle>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="ID" Visible="true" HeaderStyle-Width="20px">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblCallCategoryID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "CallCategory_ID") %>'></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:BoundField DataField="CallCategory_Name" HeaderText="Call Category Name 3rd" />
                                            <asp:TemplateField HeaderText="" ItemStyle-Width="20px">
                                                <ItemTemplate>
                                                    <asp:LinkButton ID="lbtnDeleteThirdLevel" CommandArgument='<%# Eval("CallCategory_ID") %>'
                                                        CommandName="Delete" runat="server">Deactivate</asp:LinkButton>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            </Columns>
                                        <EmptyDataTemplate>
                                            <div style="color: #CC3300">
                                                No Category Found!</div>
                                        </EmptyDataTemplate>
                                        <HeaderStyle BackColor="#0063A6" ForeColor="White" />
                                        <FooterStyle BackColor="#0063A6" ForeColor="White" />
                                    </asp:GridView>
                                        <p>rbThirdLevel below gvThirdLevel</p>
                                        <asp:RadioButtonList ID="rbThirdLevel" runat="server" onselectedindexchanged="rbThirdLevel_SelectedIndexChanged" RepeatDirection="Horizontal" AutoPostBack="True">
                                        <asp:ListItem Selected="True">Active</asp:ListItem>
                                        <asp:ListItem>Inactive</asp:ListItem>
                                        </asp:RadioButtonList>
                                    <p></p>
                                    New Category:
                                    <asp:TextBox ID="txtAddCategorySecondLevel" runat="server" ></asp:TextBox>
                                    <asp:Button ID="btnAddCategorySecondLevel" runat="server" Text="Add 2nd" OnClick="btnAddCategorySecondLevel_Click" CssClass="btn" />
                                </div>
                            </td>
                        </tr>
                    </ItemTemplate>
                    <HeaderStyle BackColor="#FFFFFF"></HeaderStyle>
                    <FooterStyle BackColor="#FFFFFF" BorderStyle="None"></FooterStyle>
                    <ItemStyle BorderStyle="None" />
                </asp:TemplateField>
            </Columns>
            <EmptyDataTemplate>
                <div style="color: #CC3300">
                    No Data Found!</div>
            </EmptyDataTemplate>
            <HeaderStyle BackColor="#003E66" ForeColor="White" />
            <FooterStyle BackColor="#003E66" ForeColor="White" BorderStyle="None" />
            <PagerStyle BackColor="White" BorderStyle="None" BorderColor="White" />
        </asp:GridView>
        <p>2nd Level below </p>
    <asp:RadioButtonList ID="rbSecondLevel" runat="server" onselectedindexchanged="rbSecondLevel_SelectedIndexChanged" RepeatDirection="Horizontal" AutoPostBack="True">
        <asp:ListItem Selected="True">Active</asp:ListItem>
        <asp:ListItem>Inactive</asp:ListItem>
    </asp:RadioButtonList>
        <p></p>
        <asp:Label ID="lblMainLevel" runat="server" Text="New Category:" Visible="false"></asp:Label>
        <asp:TextBox ID="txtAddCategoryMainLevel" runat="server" Visible="false"></asp:TextBox>
        <asp:Button ID="btnAddCategoryMainLevel" runat="server" Text="Add Main" OnClick="btnAddCategoryMainLevel_Click" CssClass="btn" Visible="false" />
    </td>
</tr>
</table>

这是cs代码。 rbThirdLevel返回null,行字符串radiovalue,我尝试访问所选的单选按钮的值抛出错误,&#34;对象引用未设置为对象的实例。&#34; 我将非常感谢你的帮助。

  protected void rbThirdLevel_SelectedIndexChanged(object sender, EventArgs e)
    {
        string ItemStatus = String.Empty;
        DataTable Categories = new DataTable();
        foreach (GridViewRow row in gvSecondLevel.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                GridView gvThirdLevel = (GridView)row.FindControl("gvThirdLevel");
                if (gvThirdLevel != null)
                {
                    foreach (GridViewRow row2 in gvThirdLevel.Rows)
                    {
                        RadioButtonList rbThirdLevel = gvThirdLevel.Rows[row2.RowIndex].FindControl("rbThirdLevel") as RadioButtonList;
                        string radiovalue = (gvThirdLevel.Rows[row2.RowIndex].FindControl("rbThirdLevel") as RadioButtonList).SelectedItem.Value;
                        if (radiovalue == "Inactive")
                        {
                            ItemStatus = "N";
                            Categories = BLL.BLCommon.Select_CallCategoryListbyStatus(ItemStatus);
                        }
                        if (radiovalue == "Active")
                        {
                            ItemStatus = "Y";
                            Categories = BLL.BLCommon.Select_CallCategoryListbyStatus(ItemStatus);
                        }

                    }

                }

            }
        }

    }

0 个答案:

没有答案