Find Control on RowDataBound-Event returns as NULL/Empty

3.7k Views Asked by At

I am trying to get text value of the selected row from my GridView using FindControl, but the FindControl always returns as NULL.

.ASPX Code:

<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# Code:

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
    }
}

Can anybody point out what I am doing wrong here or is there any other way of doing this? I wanted to get value of the CountryName from the above GridView when select button is clicked.

3

There are 3 best solutions below

0
On BEST ANSWER

Thank you both! I was able to get data from "ItemTemplate". But i used a different event this time.

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");
        }
    }
0
On

As @AlexKurryashev above comment you have to check/find control from EditTemplate mode of GridView:

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

You can use OnRowCommand event to get value from select button click as follows:

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;
    }
}
0
On

I was able to get the data from Item HeaderTemplate by using this code.

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; 
    }
}