0

I'm trying to get the value of dynamically created textbox.

Dim idReponse As String
idReponse = GlobalVariableAddOneWeight.ArrayReponseId(i)

Dim textboxId As String
textboxId = "txtWeight" + idReponse

Dim tb As New TextBox()
tb = Me.Div2.FindControl(textboxId)

Dim Poids As Integer = CInt(tb.Text)

I already tried the same code in another page and its working but in this one i'm having this error:

object reference not set to an instance of an object

Abbas
  • 14,186
  • 6
  • 41
  • 72
user2550171
  • 23
  • 1
  • 2
  • 8

1 Answers1

0

My guess is that tb isn't actually getting set to anything.

Ensure that Div2 is actually the current naming container for the element with an id of textboxId.

Additionally, you will need to cast tb to a TextBox in order to call its Text property. I would break it down like this to try to narrow down the problem:

Dim obj as Object = Me.Div2.FindControl(textboxId)
Dim tb as TextBox = CType(obj, TextBox)
Dim Poids As Integer = CInt(tb.Text)

This way you can see if the problem is with getting the object itself with FindControl or with casting the object to a TextBox.

Aaron Palmer
  • 8,912
  • 9
  • 48
  • 77