473,387 Members | 1,757 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,387 software developers and data experts.

Dynamic Controls and Adding it to the Form First. Why?

I am trying to use some dynamic controls that are built and then added
to tables. The problem that I am having is the timing of when I can
populate the controls and have the state remain after a postback. The
main question would be this:

Why does this work for maintaining state after a postback for dynamic
controls:

myText = new Label();
myText.ID = "myText";
Control form = FindControl("Form1");
form.Controls.Add(myText);

if (!IsPostBack)
{
myText.Text = "Remember this text.";
}

And this does not:

myText = new Label();
myText.ID = "myText";

if (!IsPostBack)
{
myText.Text = "Remember this text.";
}

Control form = FindControl("Form1");
form.Controls.Add(myText);

And how do I solve it so that the later can work?

This becomes a real problem when I loop through several controls that
I need to populate with data and I am adding them not to the form but
to a table that is later added to the form. Similar to this psuedo
code:

create a new table
create a new tableRow

foreach piece of data

create customControl
create a tableCell
add customControl to a tableCell

**populate control with data**

continue the for loop

add tableCell to tableRow
add tableRow to table
**add table to form**

I am aware that I can populate the customControl data after I add the
whole table to the form. But in my case that would create a lot of
extra lame code to handle an ordering issue and not really lend itself
to good programming practices. I would consider that a kludge.

Finnaly, if you have a solution to the my question "And how do I solve
it so that the later can work?" I will be most grateful for the
answer.

Thanks,
Tyler
Nov 18 '05 #1
3 1789
Tyler,

First of all, I am not sure if you are on the right track. Did you consider
using either datagrid or datalist or repeater with template columns? They
are made specially for what you seem to be trying to reproduce manually.

Secondly, it helps if you specify what exactly doesn't work. Are you getting
an exception?

Eliyahu

"Tyler Carver" <xf**********@yahoo.com> wrote in message
news:81**************************@posting.google.c om...
I am trying to use some dynamic controls that are built and then added
to tables. The problem that I am having is the timing of when I can
populate the controls and have the state remain after a postback. The
main question would be this:

Why does this work for maintaining state after a postback for dynamic
controls:

myText = new Label();
myText.ID = "myText";
Control form = FindControl("Form1");
form.Controls.Add(myText);

if (!IsPostBack)
{
myText.Text = "Remember this text.";
}

And this does not:

myText = new Label();
myText.ID = "myText";

if (!IsPostBack)
{
myText.Text = "Remember this text.";
}

Control form = FindControl("Form1");
form.Controls.Add(myText);

And how do I solve it so that the later can work?

This becomes a real problem when I loop through several controls that
I need to populate with data and I am adding them not to the form but
to a table that is later added to the form. Similar to this psuedo
code:

create a new table
create a new tableRow

foreach piece of data

create customControl
create a tableCell
add customControl to a tableCell

**populate control with data**

continue the for loop

add tableCell to tableRow
add tableRow to table
**add table to form**

I am aware that I can populate the customControl data after I add the
whole table to the form. But in my case that would create a lot of
extra lame code to handle an ordering issue and not really lend itself
to good programming practices. I would consider that a kludge.

Finnaly, if you have a solution to the my question "And how do I solve
it so that the later can work?" I will be most grateful for the
answer.

Thanks,
Tyler

Nov 18 '05 #2
Jos
Tyler Carver wrote:
I am trying to use some dynamic controls that are built and then added
to tables. The problem that I am having is the timing of when I can
populate the controls and have the state remain after a postback. The
main question would be this:

Why does this work for maintaining state after a postback for dynamic
controls:

myText = new Label();
myText.ID = "myText";
Control form = FindControl("Form1");
form.Controls.Add(myText);

if (!IsPostBack)
{
myText.Text = "Remember this text.";
}

And this does not:

myText = new Label();
myText.ID = "myText";

if (!IsPostBack)
{
myText.Text = "Remember this text.";
}

Control form = FindControl("Form1");
form.Controls.Add(myText);

And how do I solve it so that the later can work?

This becomes a real problem when I loop through several controls that
I need to populate with data and I am adding them not to the form but
to a table that is later added to the form. Similar to this psuedo
code:

create a new table
create a new tableRow

foreach piece of data

create customControl
create a tableCell
add customControl to a tableCell

**populate control with data**

continue the for loop

add tableCell to tableRow
add tableRow to table
**add table to form**

I am aware that I can populate the customControl data after I add the
whole table to the form. But in my case that would create a lot of
extra lame code to handle an ordering issue and not really lend itself
to good programming practices. I would consider that a kludge.

Finnaly, if you have a solution to the my question "And how do I solve
it so that the later can work?" I will be most grateful for the
answer.

Thanks,
Tyler


I've had the same problem, and I consider it a bug in ASP.NET.
It has to do with the automatic ID's that are assigned to
dynamic controls. Only in the first case are these ID's the same
before and after postback.

I think you can work around it by assigning the ID's yourself.
But I'm guessing that this doesn't help you, because it would
cause even more "lame code".

--

Jos
Nov 18 '05 #3
> I've had the same problem, and I consider it a bug in ASP.NET.
It has to do with the automatic ID's that are assigned to
dynamic controls. Only in the first case are these ID's the same
before and after postback.
I agree, it causes a lot of grief just because you have to do things
in a certain order. In this case it does not have anything to do with
ID's (although I know that causes another problem) as you can see in
my example above I already assigned a unique ID in both cases.

I think you can work around it by assigning the ID's yourself.
But I'm guessing that this doesn't help you, because it would
cause even more "lame code".


I actually worked around the issue by adding the table to the form
first and then adding my control to the table cell and finally
populating the data. Here is the fix in my psuedo-code:

create a new table
create a new tableRow
create a tableCell

add tableCell to tableRow
add tableRow to table
**add table to form**

foreach piece of data

create customControl
add customControl to a tableCell

**populate control with data**

continue the for loop

I would still like to know why the order is necessary.

Tyler
Nov 18 '05 #4

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

Similar topics

1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
2
by: Michael | last post by:
Need some help trying to read values from web controls - specifically *finding* the controls (like a drop down list) - that are added dynamically added within an asp:panel control. The page...
3
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
2
by: Suzanne | last post by:
Hi all, I'm reposting this message as I'm experiencing this problem more and more frequently : I really hope someone out there can help me as I've been tearing my hair out on this one for a...
8
by: George Meng | last post by:
I got a tough question: The backgroud for this question is: I want to design an application works like a engine. After release, we can still customize a form by adding a button, and source code...
1
by: npverni | last post by:
I have a fairly complex form that needs to load and maintain the state of several different dynamic user controls. Here is the workflow: 1) A series of editable user controls (each containing...
1
by: Mike Collins | last post by:
I have a web form that has most of the UI designed. On this form, some of the labels, textboxes, dropdowns, etc. will be added dynamically, depending on what extra bits of data the user decides...
0
by: Scott Roberts | last post by:
I always thought that the viewstate "keys" included the control ID. As long as the control IDs were unique, there shouldn't be any conflicts. Well, it appears that that may not be the case with...
4
by: =?Utf-8?B?RHlsYW5TbWl0aA==?= | last post by:
I have a WebForm where I'm dynamically creating some controls and I'm having difficulty understanding how the state is being persisted and how to work with it. I've created a simplified example...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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?
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
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...

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.