使用按钮单击查找嵌套的GridView?

时间:2012-06-11 17:16:41

标签: c# asp.net gridview

我想在单击按钮时找到嵌套的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;
}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

    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");