0

我想gvR在单击按钮时找到嵌套的 GridView,以便我可以向其中添加一行。我将发布 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;
}
4

2 回答 2

0

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

GridView gvR = (GridView)gvRP.Rows[0].FindControl("gvR");
gvR.DataSource = lstRateDetails;
gvR.DataBind();
于 2012-06-11T17:20:39.767 回答
0

我最终做的是使用NamingContainerLinkBut​​ton 来查找选定的gvRGridView。这是代码:

    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");
于 2012-06-11T18:33:38.910 回答