0
votes

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

1
Where is this code within your chain of events? Page_Load? Some databound event? - Tim B James
It would be helpful to know exactly what line is erroring and what object is not being set. - Aaron Palmer
it is erroring on the last line "Dim Poids As Integer = CInt(tb.Text)" - user2550171
this code is in save button click event - user2550171
If it is in the button click event, then I suspect that you are not dynamically creating the control when the page is post back. This means that the control doesn't actually exist. Try making sure that you have added it before finding it. - Tim B James

1 Answers

0
votes

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.