Adding control at run time

A

Andrea Moro

I must to be honest. It's many years that I don't develop
with ASP, but the basic are the same, and VB.NET programming
isn't so much different from ASP.NET ... so the hardest thing
is understand why something that should work ... it doesn't.

For instance I'm looking for a solution that let me to add
web controls to a form at run time, just to have small
routine to build the form according to certain parameters.

I build a simplest routine

Sub WriteForm
Dim x as new textbox
x.id = "abc"
x.maxlength = 10

page.controls.add(x)
End Sub

I place a <% WriteForm %> inside a <form runat="server" ....
html code ... but when I go in debug ... my textbox doesn't appear.

It seems to be right ... no run-time error appear, but also no
textbox.

Previous test case like response.write("<asp:texbox ... etc.
doesn't work as well.

What's the matter?

Thanks
Andrea
 
S

Scott Mitchell [MVP]

Andrea said:
Sub WriteForm
Dim x as new textbox
x.id = "abc"
x.maxlength = 10

page.controls.add(x)
End Sub

This code is a step in the right direction. Rather than adding the
controls to Page, though, you likely will want to add it to a specific
PlaceHolder control. To do this, you will need to add a PlaceHolder to
the page, and then do:

PlaceHolderID.Controls.Add(x)
I place a <% WriteForm %> inside a <form runat="server" ....
html code ... but when I go in debug ... my textbox doesn't appear.

To call the WriteForm() method, simply call it from your Page_Load event
handler.

For more information on working with dynamic controls, check out these
articles:
http://aspnet.4guysfromrolla.com/articles/081402-1.aspx
http://aspnet.4guysfromrolla.com/articles/082102-1.aspx
http://aspnet.4guysfromrolla.com/articles/092904-1.aspx

hth

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top