By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,638 Members | 1,983 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,638 IT Pros & Developers. It's quick & easy.

Dynamically adding and iterating through webcontrols

P: n/a
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.
Nov 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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 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.

Nov 18 '05 #2

P: n/a
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:ed**************@TK2MSFTNGP09.phx.gbl...
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.
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.


Nov 18 '05 #3

P: n/a
"Dany P. Wu" <da**@nospam.quicksilver.net.nz> wrote in message
news:10***************@drone1-svc-skyt.qsi.net.nz...
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:ed**************@TK2MSFTNGP09.phx.gbl...
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.

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

--
Jonathan Bailey.
Nov 18 '05 #4

P: n/a
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
///
Nov 18 '05 #5

P: n/a
On 2004-09-10, Dany P. Wu <da**@nospam.quicksilver.net.nz> wrote:
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:ed**************@TK2MSFTNGP09.phx.gbl...

============================================
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.

Nov 18 '05 #6

P: n/a
<jb> wrote in message news:41**********************@news.easynet.co.uk.. .
"Dany P. Wu" <da**@nospam.quicksilver.net.nz> wrote in message
news:10***************@drone1-svc-skyt.qsi.net.nz...
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:ed**************@TK2MSFTNGP09.phx.gbl...
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.

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.
Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.