Dynamically adding and iterating through webcontrols

D

Dany P. Wu

Hi everyone,

I created a page which contains a two-column table. The first column has a
bunch of labels, and the second a bunch of textboxes. Here's the code:

======================================================
Protected WithEvents Container As System.Web.UI.WebControls.PlaceHolder

Private Sub ConstructEditTable()
Container.Controls.Add(New LiteralControl("<table>" & vbNewLine))

Dim i As Integer

For i = 0 To rows
With Container.Controls
.Add(New LiteralControl("<tr>" & vbNewLine))

'Label column
.Add(New LiteralControl("<td>" & vbNewLine))
label = New Label
label.ID = "lbl" & i.ToString
label.Text = i.ToString
label.EnableViewState = True
.Add(label)
.Add(New LiteralControl("</td>" & vbNewLine))

'Textbox column
.Add(New LiteralControl("<td>" & vbNewLine))
txtTest = New TextBox
txtTest.ID = "txt" & i.ToString
txtTest.Width = Unit.Pixel(50)
txtTest.EnableViewState = True
.Add(txtTest)
.Add(New LiteralControl("</td>" & vbNewLine))
.Add(New LiteralControl("</tr>" & vbNewLine))
End With
Next
Container.Controls.Add(New LiteralControl("</table>" & vbNewLine))
End Sub
==========================================================

The problem I have is accessing the content of the textboxes after postback.
How do I iterate through the controls in Container? Basically, after
PostBack, I would like to display the same table but with labels in the
second column, instead of textboxes. These labels would display the content
of the textboxes entered prior to PostBack.

Any ideas anyone? Any suggestions would be greatly appreciated.

Cheers,
Dany.
 
C

Cor Ligthert

Dany,

I do not know if this is exactly the answer on your question, however you
have to construct what you did every time, not only the first time the page
is sended, so in every postback in the load event by instance.

Cor
 
D

Dany P. Wu

Cor Ligthert said:
Dany,
I do not know if this is exactly the answer on your question, however you
have to construct what you did every time, not only the first time the
page is sended, so in every postback in the load event by instance.
Cor

I guess that may have something to do with the answer but I can't quite put
the two together.

Even if I recreate the whole form as below, what happened to the text
entered by users into the textboxes just before the postback? If I recreate
the whole thing then a new page will be rendered with the blank textboxes.

i.e.:

============================================
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles MyBase.Load
If Not Me.IsPostBack then
ConstructEditTable()
else
ConstructEditTable()
end if
End Sub
============================================

I'm trying to get the content of the textboxes after it's first rendered,
having users entered various things into them, and post it back.

Does this make sense to anyone? is what I am asking impossible? Surely
not............ It sounds like such a simple idea yet I am completely
stumped.
 
G

Guest

Dany P. Wu said:
I guess that may have something to do with the answer but I can't quite put
the two together.

Even if I recreate the whole form as below, what happened to the text
entered by users into the textboxes just before the postback? If I recreate
the whole thing then a new page will be rendered with the blank textboxes.

i.e.:

============================================
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles MyBase.Load
If Not Me.IsPostBack then
ConstructEditTable()
else
ConstructEditTable()
end if
End Sub
============================================

I'm trying to get the content of the textboxes after it's first rendered,
having users entered various things into them, and post it back.

Does this make sense to anyone? is what I am asking impossible? Surely
not............ It sounds like such a simple idea yet I am completely
stumped.


You can get the posted data from the Request.params property of the page.
Each textbox should result in an additional param.
 
C

Cor Ligthert

Dany,

I have placed for this a placeholder and a button on the form.

Than you can try this beneath it is your code a little bit change to make it
testable.

I hope this helps?

Cor
\\\
Private txtTest(10) As TextBox
Private Sub Page_Load(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
ConstructEditTable()
End Sub
Private Sub ConstructEditTable()
Container.Controls.Add(New _
LiteralControl("<table>" & vbNewLine))
Dim label As Label
Dim i As Integer
For i = 0 To 10
With Container.Controls
.Add(New LiteralControl("<tr>" & vbNewLine))
'Label column
.Add(New LiteralControl("<td>" & vbNewLine))
label = New Label
label.ID = "lbl" & i.ToString
label.Text = i.ToString
label.EnableViewState = True
.Add(label)
.Add(New LiteralControl("</td>" & vbNewLine))
'Textbox column
.Add(New LiteralControl("<td>" & vbNewLine))
txtTest(i) = New TextBox
txtTest(i).ID = "txt" & i.ToString
txtTest(i).Width = Unit.Pixel(50)
txtTest(i).EnableViewState = True
.Add(txtTest(i))
.Add(New LiteralControl("</td>" & vbNewLine))
.Add(New LiteralControl("</tr>" & vbNewLine))
End With
Next
Container.Controls.Add(New LiteralControl("</table>" & vbNewLine))
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim mytext As String = txtTest(1).Text
End Sub
///
 
D

David

============================================
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles MyBase.Load
If Not Me.IsPostBack then
ConstructEditTable()
else
ConstructEditTable()
end if
End Sub
============================================

I'm trying to get the content of the textboxes after it's first rendered,
having users entered various things into them, and post it back.

Does this make sense to anyone? is what I am asking impossible? Surely
not............ It sounds like such a simple idea yet I am completely
stumped.

Controls are filled with posted values during the ProcessPostData call.
Check your page trace, and you'll see that this is called immediately
before and after the Page.Load event. So if you create your controls
during Page.Load, the controls won't have their posted values until
some time after Page.Load exists.

There's a few standard ways around this, which to use depends on what
you need to do with the values, and there's pros and cons to each.

Create the controls during Page.Init, then read the values during Load

Create the controls during Page.Load, then read the values either
inside event handlers or at PreRender time.

Create the controls during load, and use the Request.Form collection to
figure out what the values are during a postback.

ASP.NET isn't particularly fond of dynamic controls. You can use them,
but you have to jump through a few hoops to do it.
 
D

Dany P. Wu

You can get the posted data from the Request.params property of the page.
Each textbox should result in an additional param.

Aye! The idea came to me at 3am this morning! Thankfully I didn't leap with
a "Eureka" and wake the remaining bed occupants up.

I know the naming rules of each textbox and therefore I can reiterate
through the list using the same logic I created them with! Simple idea,
isn't it? But once I reached 1.30am my brain was too fried.

Thanks for the effort everyone.

D.
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top