473,566 Members | 3,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically added controls and postback

I have a situation where I have to dynamically add controls at runtime.
I am not sure how to deal with postback and retaining values in
textboxes. I have seen examples where you have to include properties
with the same name in the C# codebehind class. But this defeats the use
of dynamic controls. Is there anyway to access and maintain the values
in the fields?
Nov 18 '05 #1
1 1558
Matthew:

I'm currently working on a project that needs to do the same thing you're describing. I had a lot of trouble finding good information on the topic, but after a lot of googling and even more trial/error, what I did was as follows:

- Create an HTML table on my form, gave it an ID and set runat="server"

- Wrote some functions that handle the layout. Basically what I did was to have the functions create new controls, append the controls to new HtmlTableCells, append the cells to new HtmlTableRows and then append the rows to the table on my form.

- Place a call to the layout functions in my page's OnInit. This, from what I was able to gather on various posts I found is critical.

- On postback, I get a reference to my form's table, then use FindControl(Con trolName) to grab each control and get it's value and process as needed.

I've had no problems with this approach. I'm sure there are better (easier?) ways to handle this, but this method seems to be working out fine for the time being. If this needs any clarification, respond and I'll see what I can do.

----- Matthew Smith wrote: -----

I have a situation where I have to dynamically add controls at runtime.
I am not sure how to deal with postback and retaining values in
textboxes. I have seen examples where you have to include properties
with the same name in the C# codebehind class. But this defeats the use
of dynamic controls. Is there anyway to access and maintain the values
in the fields?

Nov 18 '05 #2

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

Similar topics

4
2492
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 the table. I know that I can dynamically add controls (eg a textbox) to the page controls collection of a web form in a server event which will...
2
1937
by: djc | last post by:
On the page_load event I am querying a database and binding data to some text boxes, list boxes, and a repeater control. When the page loads it uses the value of one of the database fields (status) to determine what options should be available for this particular item (which is an issue... small issue tracking system). Each of these options is...
4
3103
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 UserControls into the PlaceHolder's child control collection depending upon which of the three radio buttons is selected. Each of the three UserControls have...
3
2527
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 controls are not present nor their valves in Request.Form.AllKeys.
2
2446
by: Pete Moss | last post by:
During a postback event, I am having trouble retrieving the state of a CheckBox Control that I am dynamically adding to a DataGrid Control using ASP.NET 1.1. I have no trouble adding the TemplateColumn dynamically. I persist a DataSet in my Session object and I bind the CheckBox to the DataSet. So far so good. The CheckBoxes show up and the...
1
1782
by: Mike | last post by:
Hi, I am having troubles dealing with dynamically created WebControls. I have this HTML code: <form id="Form1" runat="server" method="post"> <table width="960" align="center"> <tr> <td align="center"> ......
1
7588
by: jelle.huygen | last post by:
Hello, I have a problem in ASP.NET 2.0 with the viewstate of my dynamically added user control. I have reproduced the problem with a very simple user control and a very simple page. On my usercontrol is a button and a label. Everytime the button is clicked a counter which is stored in the viewstate is increased and displayed in the...
7
2125
by: Nathan Sokalski | last post by:
I have a page which I dynamically add several usercontrols (*.ascx files) to using the following code: Public Sub Refresh() For Each section As DataRow In Me.GetSections().Rows CType(Me.FindControl("admin" & CStr(section("buttontext")).Replace(" ", "")), adminsection2).RefreshSection() Next End Sub
5
4445
by: Nathan Sokalski | last post by:
I have a custom control that I wrote (I inherit from System.Web.UI.WebControls.CompositeControl). I dynamically add this control to my Page, but I was told that dynamically added controls do not survive postback. This seems to be true. However, this seems to make dynamically adding controls rather pointless to me, since if you cannot retrieve...
4
10996
by: Craig Buchanan | last post by:
I dynamically add data-bound templates to a gridview in my ascx control. while this works correctly when the gridview is databound to the datatable, i'm having issues on postback. i would like to iterate thru the gridview's rows, examine the databound controls, then perform a database action. for some reason, i can't find the controls. ...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7893
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7645
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6263
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.