1

I have an asp.net page called Default.aspx, and it's master page is Site.master. In the Default.aspx, i added a gridview with 3 databound fields and 1 Templatefield, and then, dragged a TextBox inside this templatefield.

Image Templatefield Editor

I'm trying to get the textbox values for each row in this gridview, using the FindControl method, but it's returning Nothing.

Here is the code that i'm using to retrieve these values:

For Each gvr As GridViewRow In GridView1.Rows

        Dim tb As TextBox = DirectCast(gvr.FindControl("TextBox1"), TextBox)
        Dim txt As String = tb.Text
        MsgBox(txt)

    Next

Note: I'm using masterPages, and i'm thinking this is causing the problem.

[edit]

In the Page_load event, to bound the gridview, i'm using the code:

        GridView1.DataSource = f.xDa
        GridView1.DataBind()

In the Button1, I've added the code:

For Each gvr As GridViewRow In GridView1.Rows

        Dim tb As TextBox = DirectCast(gvr.FindControl("TextBox1"), TextBox)
        Dim txt As String = tb.Text
        MsgBox(txt)

    Next

But i'm Always getting an empty textbox.

Thank's everybody!

Diego Oliveira
  • 63
  • 1
  • 3
  • 8
  • Where are you calling that code? If the GridView isn't databound yet, there probably won't be anything to show. Also, does that code throw an error, or do you just get an empty string in the MsgBox? – Josh Darnell Oct 02 '13 at 20:10
  • Yes, the Gridview works fine, with the code: GridView1.DataSource = f.xDa GridView1.DataBind() But, when i type inside the textboxes, i cant get the values inside them.... In this case, i'm getting an empty string in the msgbox. – Diego Oliveira Oct 02 '13 at 20:13
  • Inside what event is the code in your question executed? – Josh Darnell Oct 02 '13 at 20:14
  • In the Page_Load: GridView1.DataSource = f.xDa GridView1.DataBind() In a Button called Button1 i put the code: For Each gvr As GridViewRow In GridView1.Rows Dim tb As TextBox = DirectCast(gvr.FindControl("TextBox1"), TextBox) Dim txt As String = tb.Text MsgBox(txt) Next – Diego Oliveira Oct 02 '13 at 20:17
  • Please use the "edit" button to update your question with the relevant code. It's too hard to comprehend in the comments here =) – Josh Darnell Oct 02 '13 at 20:27

2 Answers2

4

You need to update your Page_Load code to this:

If Not IsPostBack Then
    GridView1.DataSource = f.xDa
    GridView1.DataBind()
End If

By the time your code gets to the Button_Click event, it has already repopulated the GridView with data from your database (overwriting what your user typed into the TextBox).

The code I've added above causes the data to be loaded only the first time - then the ASP.NET viewstate handles making sure the state of the GridView is kept up-to-date.

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
  • 1
    Hey man, yoy saved my day!!! It worked fine!! Hey @jadarnel27 How can i post an expressive thank to your post? Im new here!! – Diego Oliveira Oct 02 '13 at 21:02
  • @DiegoOliveira The comment, upvote, and accept is plenty of thanks =) I'm glad I was able to help you. I'd say when you are able, "pay it forward" by contributing good answers to the site as well. – Josh Darnell Oct 02 '13 at 21:19
0

I had a similar problem, but my gridview wasn't rendered at Page_Load so I couldn't add the "If Not IsPostBack" bindings to the Page_Load as the SQL dataset I was using wasn't yet declared.

Instead of the FindControl and calling your textbox by name, trying something like this might work if you can't use the Page_Load gridview databinding:

For Each gvr As GridViewRow In GridView1.Rows

    Dim txt As String = CType(gvr.Cells(0).Controls(0), TextBox).Text
    MsgBox(txt)

Next

The (0) in the Cells(0) is the column number in your gridview that you are trying to access. So for example If "TextBox1" is the 1st column use Cells(0), if it is the 2nd column use Cells(1) and so on. This allows the text in the textbox to be retrieved without having to add an additional section in the Page_Load