Search code examples
c#asp.netvisual-studio-2010gridviewfindcontrol

gridview findcontrol returning empty ""


i am trying to read from a textbox within a gridview by using this code

    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            string textBoxText = ((TextBox)row.FindControl("numTC")).Text;
            Response.Write(textBoxText);

        }
    }

this code keeps returning "" (empty)

any idea why this is hapenning?

Thanks


Solution

  • Make sure that you are not re-binding the GridView on the PostBack of the page. This may be the issue.

    EDITS

    Make sure that the code for Binding the GridView is within the code below:

    C#

    if ( !Page.IsPostBack ){
        // Code to bind the control
    }
    

    VB

    If Not Page.IsPostBack Then
        ' Code to bind the control
    End If
    

    Otherwise what happens is that the controls is "rebuilt" and the values are all lost within the TextBox's