Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Fetch values from dynamically textbox in gridview

Author  Topic 

tomasjons
Starting Member

17 Posts

Posted - 2009-04-21 : 05:38:49
Hi there!

I am having trouble getting the data from a dynamically created textbox in gridview.

Here is my code for creating the textboxes:


Protected Sub Gridview1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then

Dim j As Integer = 0
For j = 2 To e.Row.Cells.Count - 1
Dim xTextBox As New TextBox()
xTextBox.Width = "50"
xTextBox.ID = "Tomas" & e.Row.RowIndex & "-" & j

e.Row.Cells(j).Controls.Add(xTextBox)
Dim dsAttributter As DataSet = New DataSet
dsAttributter = objStatus.GetDataSet("SELECT attVerdi FROM xtemp_funkogkaptabell WHERE ProdID = '" & e.Row.DataItem("ProdID") & "' AND attNavn = '" & GridView1.HeaderRow.Cells(j).Text & "' ")

If Not dsAttributter.Tables(0).Rows.Count = 0 Then
xTextBox.Text = dsAttributter.Tables(0).Rows(0)("attVerdi")

Else
xTextBox.Visible = False
End If
Next
End If
End Sub


And here is the code for getting the values:

Sub LagreVerdier(ByVal sender As Object, ByVal e As System.EventArgs) Handles saveVerdier.Click

Dim gvr As GridViewRow
Dim i As Integer
Dim j As Integer

Dim temp_txt_ProdID As New TextBox
Dim sSqlUpdate As String = ""
Dim objStatus = New SqlKontroll

For i = 0 To GridView1.Rows.Count - 1
gvr = GridView1.Rows(i)

For j = 2 To gvr.Cells.Count - 1

Dim fetchedTextbox As TextBox
fetchedTextbox = CType(gvr.FindControl("Tomas" & i & "-" & j), TextBox)
Response.Write(fetchedTextbox.Text)

Next

Next
End Sub

I get the error "Object reference not set to an instance of an object" because the findcontroll can't seem to find the xTextBox

Hope that someone can help out here :) Pretty, pretty desperate :)

Cheers,

Tomas


Cheers,
Tomas

whitefang
Enterprise-Level Plonker Who's Not Wrong

272 Posts

Posted - 2009-04-23 : 08:43:22
Should be
fetchedTextbox = CType(gvr.Cells(j).FindControl("Tomas" & i & "-" & j), TextBox)

Also, you might want to check your inner loop For j = 2 To gvr.Cells.Count - 1.

Shouldn't it be For j = gvr.Cells.Count - 1 to 0 since you're reverse iterating?
Go to Top of Page
   

- Advertisement -