2

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.

rakesh
  • 326
  • 7
  • 19
  • 2
    Control with `ID="TextBox1"` is in `EditItemTemplate` only. Try `e.Row.RowState == DataControlRowState.Edit`. Or give the same ID to `TextBox` and `Label`. – Alex Kudryashev Sep 09 '17 at 21:23

3 Answers3

2

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

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; 
    }
}
Faisal Mehboob
  • 609
  • 7
  • 17
0

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");
        }
    }
rakesh
  • 326
  • 7
  • 19