Dynamic controls

P

PZ

I'm trying to set up a strategy to develop a webpage based almost entirely on
dynamic controls. However, the first few tests revealed a problem for me.

I have in my Page_Load dynamically created a Panel, an ImageButton and a
TextBox. The button and the textbox are then assigned to the Panel.

I have assigned the ImageButton with a click event and it fires ok. I fill
in a value in the TextBox control and press the button. The page is reloaded
as it should and the textBox value is preserved. So far so good.

Here is the problem/question: How do I render new controls to the panel
based upon the value in the textbox? As I understand it, you need to always
load dynamic controls in the page_load event to be able to preserve the
values/statecontrol? So basically load different controls to the same panel,
based upon what's in the textbox.

Can it be done at all? if not, how should I perform this? Any
leads/ideas/simple code examples are most welcome.

TIA
 
P

Peter Bucher [MVP]

Hello said:
Here is the problem/question: How do I render new controls to the panel
based upon the value in the textbox? As I understand it, you need to
always
load dynamic controls in the page_load event to be able to preserve the
values/statecontrol? So basically load different controls to the same
panel,
based upon what's in the textbox.

Can it be done at all? if not, how should I perform this? Any
leads/ideas/simple code examples are most welcome.
The best practice is to load all controls at once and set the Display
attribute to true or false.
Or load the controls based upon your count saved in the state anywhere,
thats the second one.

But as you`ll see, the problems came with the - as you wish - statelesse
controls.
Look at this sites:
- http://www.tgreer.com/aspnet_html_04.html
-
http://weblogs.asp.net/infinitieslo...ding-Dynamic-Controls-_2800_Part-1_2900_.aspx
(All parts)
- http://aspnet.4guysfromrolla.com/articles/081402-1.aspx
 
P

PZ

Hi Peter

Thanks for the reply. I've been trying the last example in the article, it
seemed the most promising. However, I get a problem since the
ViewState.Add("mode","1"), which is set in the click event, it is not fired
until after the postback. In that sense, I cannot determine the "mode" until
after I press the button the 2nd time, where the vewstate["mode"] then has
the value "1"

Am I missing someting?

Regards
 
E

Evan Freeman

Not sure if I'm reading this or understanding this correctly but umm:

if(MyTextbox.Text == "make a link")
{
HtmlLink foo = new HtmlLink();
foo.setAttribute("Text", "A Link");
foo.setAttribut("href", "http://evanfreeman.blogspot.com");
MyPanel.Controls.Add(foo);
}

Something like that is doable from the code behind.

unless I'm way off on what your trying to do.
 
P

Peter Bucher [MVP]

Hello PZ
Thanks for the reply. I've been trying the last example in the article, it
seemed the most promising. However, I get a problem since the
ViewState.Add("mode","1"), which is set in the click event, it is not
fired
until after the postback. In that sense, I cannot determine the "mode"
until
after I press the button the 2nd time, where the vewstate["mode"] then has
the value "1"
Thats in the nature of the Page / Control / Adapter Lifecycle.
Look at this image:
-
http://www.aspnetzone.de/blogs/pete...-page-control-adapter-lifecycle-diagramm.aspx

Use Page.OnPreRender to determine your mode,
this methode would invoke after all user events (eg. button_click).
 
P

PZ

What version of ASP.NET does this apply to? I'm running 2.0 and I can't find
the method "setAttribute" on the HTMLlink control. If I use the property Href
it does not render the website within my Panel control.

Regards

PS
 
P

PZ

Hi Peter

Sweet, that made a part of the solution work. However, seems like I can't
get the controls to maintain state. I better give you the sample code I've
been working on.

I have a panel on my website called "patest". Then I also have a button
below that panel. When I press the button, viewstate["action"] is set with
the value "filter" so a textbox i added to this panel. The textbox has
AutoPostBack set and pressing return in the textbox makes the page postback.
By the entered value in the textbox is not reproduced along the way.

The simple code:

protected TextBox tb = new TextBox();

override protected void OnInit(EventArgs e)
{
tb.ID = "okok";
tb.EnableViewState = true;
tb.Style.Add("width","200px");
tb.AutoPostBack = true;
}

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);

if (ViewState["action"] != null && ViewState["action"].ToString() ==
"filter")
{
patest.Controls.Add(tb);
}
}

protected void goClick(object sender, EventArgs e)
{
ViewState.Add("action", "filter");
}

Regards

PZ

Peter Bucher said:
Hello PZ
Thanks for the reply. I've been trying the last example in the article, it
seemed the most promising. However, I get a problem since the
ViewState.Add("mode","1"), which is set in the click event, it is not
fired
until after the postback. In that sense, I cannot determine the "mode"
until
after I press the button the 2nd time, where the vewstate["mode"] then has
the value "1"
Thats in the nature of the Page / Control / Adapter Lifecycle.
Look at this image:
-
http://www.aspnetzone.de/blogs/pete...-page-control-adapter-lifecycle-diagramm.aspx

Use Page.OnPreRender to determine your mode,
this methode would invoke after all user events (eg. button_click).

--
Gruss, Peter Bucher
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
 
P

Peter Bucher [MVP]

Hello PZ
Sweet, that made a part of the solution work. However, seems like I can't
get the controls to maintain state. I better give you the sample code I've
been working on. good

I have a panel on my website called "patest". Then I also have a button
below that panel. When I press the button, viewstate["action"] is set with
the value "filter" so a textbox i added to this panel. The textbox has
AutoPostBack set and pressing return in the textbox makes the page
postback.
By the entered value in the textbox is not reproduced along the way.
I`ve take a look at your code, now what it is:

Its the classic problem of add a dynamic control too late in the Control
Tree (beginning at Page).
Please have a look at the links i posted before, they should cleare the
missunderstoodments.

Solutions:
1. Try to move the code from page_load to page_init
2. add the textbox _every_ time to your Control Tree, set them to
Visible=false, except if you don`t want to display them
(nothing would be send to client, if the textbox is Visible=false)
 
P

PZ

Hello Peter

Well then it has to be done like that. Reminds me of conditional design on
the form, all controls must be there from the beginning.

Some of my controls are data driven, so I guess I need to do one DB lookup
too many for each time I want to render controls, just so I can hide them. I
recon the chicken/egg problem, as you can't keep controls loaded forever. But
it seems like a heavy for the server to render everything everytime.

Thanks for the tips, I now know my design path. Will probably return later
when design evolves.

Regards,

PZ

Peter Bucher said:
Hello PZ
Sweet, that made a part of the solution work. However, seems like I can't
get the controls to maintain state. I better give you the sample code I've
been working on. good

I have a panel on my website called "patest". Then I also have a button
below that panel. When I press the button, viewstate["action"] is set with
the value "filter" so a textbox i added to this panel. The textbox has
AutoPostBack set and pressing return in the textbox makes the page
postback.
By the entered value in the textbox is not reproduced along the way.
I`ve take a look at your code, now what it is:

Its the classic problem of add a dynamic control too late in the Control
Tree (beginning at Page).
Please have a look at the links i posted before, they should cleare the
missunderstoodments.

Solutions:
1. Try to move the code from page_load to page_init
2. add the textbox _every_ time to your Control Tree, set them to
Visible=false, except if you don`t want to display them
(nothing would be send to client, if the textbox is Visible=false)

--
Gruss, Peter Bucher
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
 

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,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top