在RowDataBound-Event上查找控件返回为NULL / Empty

时间:2017-09-09 18:25:54

标签: c# asp.net gridview findcontrol

我正在尝试使用FindControl从我的GridView获取所选行的文本值,但FindControl始终返回为NULL。

.ASPX代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CID" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="CID" HeaderText="CID" InsertVisible="False" ReadOnly="True" SortExpression="CID" />
        <asp:BoundField DataField="CountryID" HeaderText="CountryID" SortExpression="CountryID" />
        <asp:TemplateField HeaderText="CountryName" SortExpression="CountryName">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CountryName") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("CountryName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

C#代码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TextBox txt = e.Row.FindControl("TextBox1") as TextBox;
        string name = txt.Text; // returns as NULL
    }
}

有人能指出我在这里做错了什么,还是有其他方法可以做到这一点?我想在单击选择按钮时从上面的GridView中获取CountryName的值。

3 个答案:

答案 0 :(得分:1)

作为@AlexKurryashev以上评论,您必须从GridView的EditTemplate模式检查/查找控件:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // check if its not a header or footer row
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // check if its in a EditTemplate
        if (e.Row.RowState == DataControlRowState.Edit)
        {
            TextBox txt = e.Row.FindControl("TextBox1") as TextBox;
            string name = txt.Text;
        }
    }
}

OR

您可以使用OnRowCommand事件从选择按钮单击中获取值,如下所示:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Select")
    {
        // get row where clicked
        GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);

        Label txt = row.FindControl("Label1") as Label;
        string name = txt.Text;
    }
}

答案 1 :(得分:1)

谢谢你们俩!我能够从“ItemTemplate”获取数据。但这次我使用了不同的事件。

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        {
            Label txt = GridView1.SelectedRow.FindControl("Label1") as Label;
            string name = txt.Text;
            Label2.Text = name;

            Session["Name"] = name;
            Response.Redirect("check.aspx");
        }
    }

答案 2 :(得分:1)

通过使用此代码,我能够从Item HeaderTemplate中获取数据。

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    {
        GridViewRow headerrow = GridView1.HeaderRow;
        DropDownList ddlId = (DropDownList)headerrow.Cells[0].Controls[1].FindControl("ddlId ");
        string headerid = ddlId.SelectedValue; 
    }
}