通过单击按钮查找嵌套的网格视图

本文关键字:网格 视图 嵌套 查找 单击 按钮 | 更新日期: 2023-09-27 18:37:00

我想在单击按钮时找到嵌套的 GridView gvR,以便我可以向其添加一行。 我将发布 GridView 标记,然后发布用于将行添加到所选嵌套网格的代码。

<asp:LinkButton ID="btnAdd" runat="server" Text="Add Room" 
            onclick="btnAdd_Click"></asp:LinkButton>
        <asp:GridView ID="gvRP" runat="server" AutoGenerateColumns="false" 
            onrowdatabound="gvRP_RowDataBound" 
            onrowediting="gvRP_RowEditing">
        <Columns>
        <asp:TemplateField HeaderText="Room" ItemStyle-Width="100%">
        <ItemTemplate>
        <asp:Label runat="server" Text="Room"></asp:Label>
        <asp:DropDownList ID="ddlRoom" runat="server" AutoPostBack="True" DataTextField="Name"
            DataValueField="Id" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlRoom_SelectedIndexChanged">
            <asp:ListItem Value="-1">Select...</asp:ListItem>
        </asp:DropDownList>
        <asp:Label runat="server" AssociatedControlID="ddlRate" Text="Rate" ID="lblRate"></asp:Label><asp:DropDownList
            ID="ddlRate" runat="server" AppendDataBoundItems="true" DataTextField="Name"
            DataValueField="Id">
            <asp:ListItem Value="-1">Select...</asp:ListItem>
        </asp:DropDownList>
        <asp:Label  runat="server" Text="Adults"></asp:Label>
        <asp:TextBox ID="txtAdults" Text='<%#Bind("Adults") %>' runat="server" Width="25px"></asp:TextBox>
        <asp:Label  runat="server" Text="Children"></asp:Label>
        <asp:TextBox ID="txtChildren" Text='<%#Bind("Children") %>' runat="server"  Width="25px"></asp:TextBox>
        <asp:Label runat="server" Text="Check In"></asp:Label>
        <asp:TextBox ID="txtCheckIn" Text='<%#Bind("CheckIn") %>' runat="server" Width="75px"></asp:TextBox>
        <asp:Label  runat="server" Text="Check Out"></asp:Label>
        <asp:TextBox ID="txtCheckOut" Text='<%#Bind("CheckOut") %>' runat="server"  Width="75px"></asp:TextBox>
        <h3>Rates</h3>
        <asp:GridView ID="gvR" runat="server" AutoGenerateColumns="false">
        <Columns>
        <asp:BoundField DataField="Name" HeaderText="Rate" />
        <asp:BoundField DataField="Effective" HeaderText="Effective" />
        <asp:BoundField DataField="Expire" HeaderText="Expire" />
        <asp:BoundField DataField="Amount" HeaderText="Amount" />
        <asp:BoundField DataField="Code" HeaderText="Currency" />
        </Columns>
        </asp:GridView>
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>

添加费率的代码:

protected void AddRate(object sender, EventArgs e)
{
    lstRateDetails = (List<RateDetail>)ViewState["Rates"];
    lstRateDetails.Insert(0, new RateDetail());
    //Have no idea what to do here? I have also tried gvRatePlans.TemplateControl...
    GridView gvR = (GridView)gvRP.FindControl("gvR");
    gvR.DataSource = lstRateDetails;
    gvR.DataBind();
    ViewState["Rates"] = lstRateDetails;
}

通过单击按钮查找嵌套的网格视图

如果 gvRP 是父网格,gvR 是嵌套网格。由于嵌套网格包含在容器网格的每一行中,因此我们需要在父网格的行中找到它。您可以像这样在第一行中包含嵌套网格

GridView gvR = (GridView)gvRP.Rows[0].FindControl("gvR");
gvR.DataSource = lstRateDetails;
gvR.DataBind();

我最终做的是使用 LinkButton 的NamingContainer来查找选定的 GridView gvR。 这是代码:

    LinkButton lnk = (LinkButton)sender;
    lstRateDetails = (List<RateDetail>)ViewState["Rates"];
    lstRoomDetails = (List<RoomDetail>)ViewState["Rooms"];
    lstRateDetails.Insert(0, new RateDetail());
    GridViewRow gvr = (GridViewRow)lnk.NamingContainer;
    int selectedRow = gvr.RowIndex;
    GridView gvR = (GridView)gvRP.Rows[selectedRow].FindControl("gvR");