从嵌套的gridview获取textbox.text

时间:2016-03-17 22:45:03

标签: c# html gridview parent-child

我有一个带有子gridview的父网格视图

<!-- Parent -->
<asp:GridView ID="gvParent" runat="server" AutoGenerateColumns="false" Width="100%"  CssClass="Grid"
    DataKeyNames="SupplierReference" OnRowDataBound="gvParent_OnRowDataBound" >
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>

                    <img alt = "" style="cursor: pointer" src="images/plus.png" />
                    <asp:Panel ID="pnlOrders" runat="server" Style="display: none">

                            <!-- Child -->
                            <asp:GridView ID="gvChild"  runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid" 
                            ShowFooter = "true" OnRowDataBound="gvChild_OnRowDataBound">
                            <Columns>
                                <asp:TemplateField HeaderText="Qty"  ItemStyle-Width="100px">
                                        <ItemTemplate>                  
                                                <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("Qty")%>' />                                
                                        </ItemTemplate>
                                </asp:TemplateField >     
                            </Columns>
                        </asp:GridView>

                    </asp:Panel>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkSelectQtys" runat="server"
                    CommandArgument = '<%# Eval("SupplierReference")%>' CommandName="SelectQtys"
                    OnClientClick = "return confirm('Add these materials to this task?')"
                    Text = "Add" OnClick="getQty"  ></asp:LinkButton>

            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

我正在尝试使用一个文本框,用户可以更改该值,当他们单击添加时,我希望能够将该文本提取到C#代码中并使用它。 我无法将其纳入我的C#代码。

    protected void getQty(object sender, EventArgs e)
    {
        //After clicking "add"....
        //Do something here to get text from each TextBox1 in the Child gridview


    }

有人请在我失去我留下的小头发之前帮忙......

1 个答案:

答案 0 :(得分:0)

protected void getQty(object sender, EventArgs e)
{
    //After clicking "add"....
    string s;
    for(i=0; i < gvChild.Rows.Count; i++)
    {
        s = ((TextBox)gvChild.Rows[i].FindControl("TextBox1")).Text; 
    }
    //Do what you want to with this string

}