在嵌套网格视图中获取文本框的值

时间:2015-05-31 07:03:12

标签: c# .net gridview

我有一个嵌套的gridview,并且有一个名为TextBoxDescription的文本框,用于在第二个gridview中插入用户信息。但是当我想要捕获textbox的(TextBoxDescription)值时,它返回Null。

Html代码:

            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%"
            DataKeyNames="ReportId" OnRowDataBound="GridView2_OnRowDataBound" ForeColor="#333333">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:TemplateField>
                    <ItemStyle Width="35px" HorizontalAlign="Center" />
                    <ItemTemplate>
                        <img alt="" style="cursor: pointer" src="plus.png" />
                        <asp:Panel ID="Panel5" runat="server" Style="display: none; text-align: center;">
                            <asp:GridView ID="GridView2" border="0" runat="server" DataKeyNames ="ReportId" OnRowCommand="GridView1_RowCommand"
                                Style="direction: rtl" Width="100%" Height="100%">
                                <Columns>
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <table width="100%" height="100%" bgcolor="#F4F4F4">
                                                <tr>
                                                </tr>
                                                <tr>
                                                    <td style="width: 50%; height: 50%" hidefocus="hidefocus" unselectable="off" valign="top">
                                                        <asp:Panel ID="Panel3" runat="server" GroupingText="یاد داشت">
                                                            <asp:TextBox ID="TextBoxDescription" runat="server" Style="resize: none; width: 612px;
                                                                height: 160px;" Text='<%# Eval("UserDescription") %>' TextMode="MultiLine"></asp:TextBox>
                                                            <br />
                                                            <br />
                                                            <asp:LinkButton ID="LinkButtonSave" CommandName ="updateData" CommandArgument='<%#Eval("ReportId") %>' runat="server">ذخیره</asp:LinkButton>
                                                        </asp:Panel>
                                                    </td>
                                                </tr>
                                            </table>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>
.
.
.

C#代码:

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "updateData")
    {
        int i = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

        TextBox tb = (TextBox)row.FindControl("TextBoxDescription");
        string Text = tb.Text;
    }
}

2 个答案:

答案 0 :(得分:0)

如下所示:

GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
int index = row.RowIndex;
TextBox tb= (TextBox )GridView1.Rows[index].FindControl("TextBoxDescription");

答案 1 :(得分:0)

http://forums.asp.net/p/2053658/5919172.aspx?Getting+textbox+s+value+in+Nested+gridview

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
    <asp:TemplateField HeaderText="ID">
        <ItemTemplate>
            <asp:Label ID="lblid" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Details">
        <ItemTemplate>
            <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView2_RowCommand">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <table>
                                <tr>
                                    <td>
                                        <asp:Panel ID="Panel3" runat="server" GroupingText="یاد داشت">
                                            <asp:TextBox ID="TextBoxDescription" runat="server" Style="resize: none; width: 612px; height: 160px;"
                                                Text='<%# Eval("UserDescription") %>' TextMode="MultiLine"></asp:TextBox>
                                            <br />
                                            <br />
                                            <asp:LinkButton ID="LinkButtonSave" CommandName="updateData" CommandArgument='<%#Eval("ReportId") %>' runat="server">ذخیره</asp:LinkButton>
                                        </asp:Panel>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

 

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable gdv1source = new DataTable();
        gdv1source.Columns.Add("ID");

        gdv1source.Rows.Add("1");
        GridView1.DataSource = gdv1source;
        GridView1.DataBind();
    }
}

protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
    GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
    TextBox tb = (TextBox)row.FindControl("TextBoxDescription");
    string Text = tb.Text;

    lbltext.Text = "Text Value is: " + Text;
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        GridView gdv2 = (GridView)e.Row.FindControl("GridView2");

        BindGridView2(gdv2);
    }
}

private static void BindGridView2(GridView gdv2)
{
    DataTable gdv2source = new DataTable();
    gdv2source.Columns.Add("UserDescription");
    gdv2source.Columns.Add("ReportId");

    gdv2source.Rows.Add("UserDescription1", "1");
    gdv2source.Rows.Add("UserDescription2", "2");

    gdv2.DataSource = gdv2source;
    gdv2.DataBind();
}