Dynamically adding and iterating through webcontrols
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.aspnet.

Post a new message to this list...

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.

Reply to this message...
 
    
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

[Original message clipped]

Reply to this message...
 
    
Dany P. Wu
"Cor Ligthert" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

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.

[Original message clipped]

Reply to this message...
 
    
jb
"Dany P. Wu" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

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

--
Jonathan Bailey.

Reply to this message...
 
    
Dany P. Wu
<jb> wrote in message news:4141a5a6$0$4124$Click here to reveal e-mail address...
[Original message clipped]

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.

Reply to this message...
 
    
David
On 2004-09-10, Dany P. Wu <Click here to reveal e-mail address> wrote:
[Original message clipped]

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.

Reply to this message...
 
    
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
///

Reply to this message...
 
 
System.ComponentModel.Container
System.EventArgs
System.Object
System.Web.UI.LiteralControl
System.Web.UI.MobileControls.TextBox
System.Web.UI.Page
System.Web.UI.WebControls.PlaceHolder
System.Web.UI.WebControls.TextBox
System.Web.UI.WebControls.Unit
System.Windows.Forms.TextBox




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com