Access dynamically created controls

T

T-Bone

Hi there,
I read a lot about this issue but still got no clear answer that
solves my problem.

I've a Web User Control with a placeholder called phProperties.
I create a form with a number of Textboxes dynamically from values in
a database and add them to the phProperties placeholder. The number of
Textboxes depends on the template the user clicked. Each template has
his own number of properties.
I store the controlnames in a hashtable called 'templatepropertynames'

After the user clicks a button, I want to store the values from the
textboxes in a database.

The problem is that I can't get to the controls in the Button_click
event.
I tried with FindControl but with no result.
When debugging the value of myEnumerator.value in the button_click
event is correct which is the name of the control but:
The 'value' of
phProperties.FindControl(myEnumerator.value) is always 'nothing'
in the button_click event where I expected a reference to the
textbox.


How can I retreive the values from the Textboxes in the Button_click
event.

Here my code:

Private Sub CreatePropertyFields(ByVal templateid As Integer)
Dim getproperties As New SqlCommand("select * from
TemplateProperties where templateID=" & templateid, myConnection)
Dim propertyreader As SqlDataReader
myConnection.Open()
propertyreader = getproperties.ExecuteReader
Dim templatepropertynames As New Hashtable

While propertyreader.Read
templatepropertynames(propertyreader.Item("propertyID")) =
propertyreader.Item("propertyName")
'create label for textbox
Dim propertylbl As New Label
propertylbl.Text =
propertyreader.Item("propertyName").trim
propertylbl.Width = Unit.Pixel(200)
Me.phProperties.Controls.Add(propertylbl)

'create the Textbox
Dim newcontrol As New TextBox
newcontrol.ID =
propertyreader.Item("propertyName").trim
Me.phProperties.Controls.Add(newcontrol)

'create seperator
Dim seperatorlbl As New Label
seperatorlbl.Text = "<br>"
Me.phProperties.Controls.Add(seperatorlbl)

End While
Session("propertynames") = templatepropertynames
myConnection.Close()
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNaar4.Click
Dim templatepropertynames As New Hashtable
templatepropertynames = Session("propertynames")
Dim myEnumerator As IDictionaryEnumerator =
templatepropertynames.GetEnumerator()
While myEnumerator.MoveNext()
Dim Textboxvalue As String =
CType(phProperties.FindControl(myEnumerator.value), Textbox).Text
'insert textboxvalue in the database here
End While

End Sub

Thanks in advance
Peter
 
J

John Saunders

T-Bone said:
Hi there,
I read a lot about this issue but still got no clear answer that
solves my problem.

I've a Web User Control with a placeholder called phProperties.
I create a form with a number of Textboxes dynamically from values in
a database and add them to the phProperties placeholder. The number of
Textboxes depends on the template the user clicked. Each template has
his own number of properties.
I store the controlnames in a hashtable called 'templatepropertynames'

After the user clicks a button, I want to store the values from the
textboxes in a database.

Dynamically added controls need to be added on every request, and in the
exact same order.
 
T

T-Bone

John Saunders said:
Dynamically added controls need to be added on every request, and in the
exact same order.


OK, but what about the values. If I create the controls on every page
request the values will be empty every time the page is loaded and I
never can get to the submitted values. or not?
 
P

P K

Ok, but what about the values. If I create the values on every page
request, the entered values in the Textboxes before the button click
will be gone. Or not?


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
T

T-Bone

John Saunders said:
Dynamically added controls need to be added on every request, and in the
exact same order.

Ok, but what about the values in the Textbox. If I create the controls
on every page_load the values will be empty every time. How can I get
to the submitted values?
 
J

John Saunders

T-Bone said:
"John Saunders" <[email protected]> wrote in message

Ok, but what about the values in the Textbox. If I create the controls
on every page_load the values will be empty every time. How can I get
to the submitted values?

ViewState will take care of that for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top