Dynamic controls

Discussion in 'ASP .Net Web Controls' started by PZ, Jun 21, 2008.

  1. PZ

    PZ Guest

    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
    PZ, Jun 21, 2008
    #1
    1. Advertising

  2. Hello <Whats your name?>

    > 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

    --
    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
    Peter Bucher [MVP], Jun 22, 2008
    #2
    1. Advertising

  3. PZ

    PZ Guest

    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
    "Peter Bucher [MVP]" wrote:

    > Hello <Whats your name?>
    >
    > > 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
    >
    > --
    > 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
    >
    PZ, Jun 23, 2008
    #3
  4. PZ

    Evan Freeman Guest

    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.
    --
    Evan Freeman

    http://evanfreeman.blogspot.com
    "PZ" <> wrote in message
    news:...
    > 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
    Evan Freeman, Jun 23, 2008
    #4
  5. 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
    Peter Bucher [MVP], Jun 23, 2008
    #5
  6. PZ

    PZ Guest

    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

    "Evan Freeman" wrote:

    > 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.
    > --
    > Evan Freeman
    >
    > http://evanfreeman.blogspot.com
    > "PZ" <> wrote in message
    > news:...
    > > 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

    >
    >
    >
    PZ, Jun 23, 2008
    #6
  7. PZ

    PZ Guest

    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 [MVP]" wrote:

    > 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
    >
    PZ, Jun 23, 2008
    #7
  8. 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
    Peter Bucher [MVP], Jun 23, 2008
    #8
  9. PZ

    PZ Guest

    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 [MVP]" wrote:

    > 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
    >
    PZ, Jun 24, 2008
    #9
  10. Peter Bucher [MVP], Jun 24, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGVyb3M=?=

    Dynamic Variables? OR Dynamic Controls

    =?Utf-8?B?VGVyb3M=?=, Aug 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    412
    Scott Allen
    Aug 10, 2004
  2. Replies:
    5
    Views:
    5,555
  3. Ronald
    Replies:
    2
    Views:
    814
    Robbe Morris [C# MVP]
    Jan 17, 2006
  4. =?Utf-8?B?U3RldmUgRnVuaw==?=

    Dynamic Wizard with Dynamic Validation Controls

    =?Utf-8?B?U3RldmUgRnVuaw==?=, Oct 13, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    436
    =?Utf-8?B?U3RldmUgRnVuaw==?=
    Oct 13, 2006
  5. Denny Smolinski via .NET 247

    Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls

    Denny Smolinski via .NET 247, May 4, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    451
    Eidolon
    May 4, 2004
Loading...

Share This Page