473,408 Members | 2,813 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,408 software developers and data experts.

Dynamically add server controls to a web page?

How do I dynamically add web server controls to a web form? Also, how can I
position them?

Any sample code or links would be appreciated...
--
Chris Davoli

Nov 19 '05 #1
6 2115
controls.add()

Now, you can do this a few ways, but I think the easiest is to use a
placeholder control, then you can add whatever you want to it, but the page
itself is also a control that you can directly add to..

Could I ask for a little more background though.. perhaps there are other
ways depending on what you are trying to do specifically...
--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com

"Chris Davoli" wrote:
How do I dynamically add web server controls to a web form? Also, how can I
position them?

Any sample code or links would be appreciated...
--
Chris Davoli

Nov 19 '05 #2
Chris I have a few examples of using controls dynamically on my website. Go
to http://www.aboutfortunate.com?page=codelibrary and use the text box there
to search for: "dynamic control" and you'll see all the samples.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Chris Davoli" <Ch*********@discussions.microsoft.com> wrote in message
news:9E**********************************@microsof t.com...
How do I dynamically add web server controls to a web form? Also, how can
I
position them?

Any sample code or links would be appreciated...
--
Chris Davoli

Nov 19 '05 #3
To create controls and position them, what I usually do is (and these steps
I do in PAGE_LOAD):
1) Add a PlaceHolder control in the place on the page where I want the
server control to be.
2) Create the server control using Page.LoadControl.
3) Assign an id to the control. Make sure this id is unique within the
controls collection that this control will be added to.
4) Update the server control for display.
5) Add the created control(s) to the PlaceHolder.Controls collection.

And one hint:
- You have to do this process on a postback if you want to get data &
events out of the server control. So, on a postback, do steps 1, 2, 3, and
5; make sure you skip 4 otherwise you will overwrite any value getting
posted back. And, if you don't keep the id the same, you will not get any
data or events posted back.

Jeff
"Chris Davoli" <Ch*********@discussions.microsoft.com> wrote in message
news:9E**********************************@microsof t.com...
How do I dynamically add web server controls to a web form? Also, how can
I
position them?

Any sample code or links would be appreciated...
--
Chris Davoli

Nov 19 '05 #4
Example:

ControlContainerPanel is a empty Panel at the bottom of your webform.

TextBox myNewTextBox = new TextBox();
myNewTextBox.Text = "Default.Text";

ControlContainerPanel.Controls.Add(myNewTextBox);
You can add controls to a large number of objects like Page, TabelCells,
Panels and so on. With that in mind you can position them. Another way
that might work is to add some stylesheet information using the
myNewTextBox.Style.Add("LEFT", "160px");
myNewTextBox.Style.Add("TOP", "100px");
myNewTextBox.Style.Add("POSITION", "absolute");
Cheers,
//Rutger

http://www.RutgerSmit.com

Chris Davoli wrote:
How do I dynamically add web server controls to a web form? Also, how can I
position them?

Any sample code or links would be appreciated...

Nov 19 '05 #5
We do a lot of surveys and tests. Well, I want to dynamically add questions
and the answer to the question will be determined by the way the the admin
person set up the question. The answer could be a radio button, check box or
even text box. The questions and answers are out on a data base by
survey/test. The admin person will set up a survey/test and add answers to
the test/survey.
--
Chris Davoli

"Curt_C [MVP]" wrote:
controls.add()

Now, you can do this a few ways, but I think the easiest is to use a
placeholder control, then you can add whatever you want to it, but the page
itself is also a control that you can directly add to..

Could I ask for a little more background though.. perhaps there are other
ways depending on what you are trying to do specifically...
--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com

"Chris Davoli" wrote:
How do I dynamically add web server controls to a web form? Also, how can I
position them?

Any sample code or links would be appreciated...
--
Chris Davoli

Nov 19 '05 #6
This has actually been posted a number of times lately...for this exact
scenario.
My recommendation is a Repeater with a label and placeholder control in it.
The label is for the Question and the placeHolder is for the control.
You get the question list, bind it to the repeater, and plop in the
appropriate stuff in the Item_DataBound() call of the repeater.

hth

--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com

"Chris Davoli" wrote:
We do a lot of surveys and tests. Well, I want to dynamically add questions
and the answer to the question will be determined by the way the the admin
person set up the question. The answer could be a radio button, check box or
even text box. The questions and answers are out on a data base by
survey/test. The admin person will set up a survey/test and add answers to
the test/survey.
--
Chris Davoli

"Curt_C [MVP]" wrote:
controls.add()

Now, you can do this a few ways, but I think the easiest is to use a
placeholder control, then you can add whatever you want to it, but the page
itself is also a control that you can directly add to..

Could I ask for a little more background though.. perhaps there are other
ways depending on what you are trying to do specifically...
--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com

"Chris Davoli" wrote:
How do I dynamically add web server controls to a web form? Also, how can I
position them?

Any sample code or links would be appreciated...
--
Chris Davoli

Nov 19 '05 #7

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

Similar topics

3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
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: 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...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am have facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web...
4
by: Harry | last post by:
Hello, I have a page with a RadioButtonList and a PlaceHolder control. The RadioButtonList's AutoPostBack attribute is set to TRUE and its SelectedIndexChanged event loads one of three...
2
by: Axel Dahmen | last post by:
HI, I want to dynamically add controls to a web page from within a common base class. Unfortunately, ASP.NET fails with "System.Web.HttpException: The Controls collection cannot be modified...
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...
3
by: Dotnet Gruven | last post by:
I've built a WebForm with a Table added dynamically in Page_Load when IsPostBack is false. The table includes a couple of TextBoxes, RadioButtonLists and CheckboxLists. On postback, those...
7
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.