开发者

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.W开发者_开发百科rite(textBoxText);

        }
    }

this code keeps returning "" (empty)

any idea why this is hapenning?

Thanks


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


UPDATE:

For testing purposes, try doing GridView1.DataBind(); at the begining of your method.

Try debugging like this:

Set a breakpoint at the end of the Button1_Click method.

Run the site in debug mode (F5).

When executions stops at end of Button1_Click, open the Immediate Window located at bottom of screen.

Type there:

GridView1.Rows and see if it contains the number of rows it should.

Should be something like:

System.Web.UI.WebControls.GridViewRowCollection} Count: 53 <-- number of rows

If it does return more than 0 rows then type:

GridView1.Rows[0].Controls and see if it returns the right number of controls on a row.

I could access controls on a row directly using GridView1.Rows[2].Controls[n] where n is the order of the control in the row.

Also try (TextBox)GridView1.Rows[0].FindControl("numTC") and see what it returns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜