473,394 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Dynamically adding and iterating through webcontrols

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 21 '05 #1
6 915
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 21 '05 #2
"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 21 '05 #3
"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 21 '05 #4
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 21 '05 #5
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 21 '05 #6
<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 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
4
by: Sandeep | last post by:
Hi I am doing one thing in my website ,actually i want to add controls dynamically to a web form and want to access the elements but when i postback the form,and access that control, it gives...
4
by: Bas Groeneveld | last post by:
I am developing an ASP.NET application part of which consists of a data entry wizard defined by entries in a data table - ie the controls on each page of the wizard are determined by definitions in...
6
by: Dany P. Wu | last post by:
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: ...
2
by: Chad | last post by:
I have a problem that I am desperate to understand. It involves dynamically adding controls to a Table control that is built as a result of performing a database query. I am not looking to...
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
2
by: grawsha2000 | last post by:
Hello, How can I add a table to asp.net page dynamically with Code Behind style. I still can't find a way of doing it. This really causes me a big problem when I deal with records from...
1
by: DC | last post by:
Hi, we are dynamically adding webcontrols in a page like so: Type t = Type.GetType("controls.namespace.control, controls.namespace", false, true); Control c =...
0
by: Mike Collins | last post by:
I someone can please help, I am about at an end in trying to figure this out. I am adding some dynamic controls to my page (I found out that I was supposed to be doing that in the oninit event,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.