Dynamic control creation

Discussion in 'ASP .Net Web Controls' started by Kirill Osipov, Sep 11, 2003.

  1. Hello everybody!

    Sorry if it's not the right newsgroup to post my message to. Anyways, any
    advice is very helpful to me as I am an absolute novice in Web Forms.
    Here is a small piece of code. It is actually a lightweight scenario of what
    is going to happen in my project.
    ------------------------------------------------
    namespace bluebook
    {
    public class Testpage : Page
    {
    protected PlaceHolder ctlPlaceHolder; //ctlPlaceHolder also resides on the
    corresponding aspx page

    private void Page_Load(object sender, System.EventArgs e)
    {
    LinkButton aLB = new LinkButton();
    aLB.CommandName = "Command 1";
    aLB.Text = "Level 1";
    aLB.Command += new CommandEventHandler(OnCommand);

    ctlPlaceHolder.Controls.Add(aLB);
    this.SaveViewState();
    }

    private void OnCommand(object sender, CommandEventArgs args)
    {
    LinkButton aLB = (LinkButton) sender;
    int a = Int32.Parse( aLB.CommandName[8].ToString() );
    a++;

    LinkButton aNewLB = new LinkButton();
    aNewLB.CommandName = "Command " + a.ToString();
    aNewLB.Text = "Level " + a.ToString();

    ctlPlaceHolder.Controls.Clear();
    ctlPlaceHolder.Controls.Add(aNewLB);
    }



    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    InitializeComponent();
    base.OnInit(e);
    }
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion
    }
    }
    ------------------------------------------------

    What I was expecting is that every time I click the hyperlink, the form is
    regenerated and the text inside the hyperlink is incremented. So first it
    displays Level 1, then Level 2, then Level 3, ..., Level 10, Level 2, ...
    and so on. But instead it only increments once, and then goes back to the
    initial state.
    I realize that changing the hyperlink's text can be achieved without
    creating a new control each time an event is raised. But as I have already
    said, this is just a sample, and in the real project I am working on I DO
    NEED to create the control dynamically each time I process the event.
    Does anyone know a workaround? Or, maybe, it is a bug in the technology?
    Regards,
    Kirill Osipov
     
    Kirill Osipov, Sep 11, 2003
    #1
    1. Advertising

  2. Kirill Osipov

    Alex Guest

    Have you tried adding the
    if(!IsPostBack){
    //put pageload code here
    }
    to your Page_Load function?


    >-----Original Message-----
    >Hello everybody!
    >
    >Sorry if it's not the right newsgroup to post my message

    to. Anyways, any
    >advice is very helpful to me as I am an absolute novice

    in Web Forms.
    >Here is a small piece of code. It is actually a

    lightweight scenario of what
    >is going to happen in my project.
    >------------------------------------------------
    >namespace bluebook
    >{
    > public class Testpage : Page
    > {
    > protected PlaceHolder ctlPlaceHolder; //ctlPlaceHolder

    also resides on the
    >corresponding aspx page
    >
    > private void Page_Load(object sender, System.EventArgs

    e)
    > {
    > LinkButton aLB = new LinkButton();
    > aLB.CommandName = "Command 1";
    > aLB.Text = "Level 1";
    > aLB.Command += new CommandEventHandler(OnCommand);
    >
    > ctlPlaceHolder.Controls.Add(aLB);
    > this.SaveViewState();
    > }
    >
    > private void OnCommand(object sender, CommandEventArgs

    args)
    > {
    > LinkButton aLB = (LinkButton) sender;
    > int a = Int32.Parse( aLB.CommandName[8].ToString() );
    > a++;
    >
    > LinkButton aNewLB = new LinkButton();
    > aNewLB.CommandName = "Command " + a.ToString();
    > aNewLB.Text = "Level " + a.ToString();
    >
    > ctlPlaceHolder.Controls.Clear();
    > ctlPlaceHolder.Controls.Add(aNewLB);
    > }
    >
    >
    >
    > #region Web Form Designer generated code
    > override protected void OnInit(EventArgs e)
    > {
    > InitializeComponent();
    > base.OnInit(e);
    > }
    > private void InitializeComponent()
    > {
    > this.Load += new System.EventHandler(this.Page_Load);
    > }
    > #endregion
    > }
    >}
    >------------------------------------------------
    >
    >What I was expecting is that every time I click the

    hyperlink, the form is
    >regenerated and the text inside the hyperlink is

    incremented. So first it
    >displays Level 1, then Level 2, then Level 3, ..., Level

    10, Level 2, ...
    >and so on. But instead it only increments once, and then

    goes back to the
    >initial state.
    >I realize that changing the hyperlink's text can be

    achieved without
    >creating a new control each time an event is raised. But

    as I have already
    >said, this is just a sample, and in the real project I

    am working on I DO
    >NEED to create the control dynamically each time I

    process the event.
    >Does anyone know a workaround? Or, maybe, it is a bug in

    the technology?
    >Regards,
    >Kirill Osipov
    >
    >
    >.
    >
     
    Alex, Sep 12, 2003
    #2
    1. Advertising

  3. Yes, I did, but it didn't help

    "Alex" <> wrote in message
    news:084c01c378db$266dd910$...
    > Have you tried adding the
    > if(!IsPostBack){
    > //put pageload code here
    > }
    > to your Page_Load function?
    >
    >
    > >-----Original Message-----
    > >Hello everybody!
    > >
    > >Sorry if it's not the right newsgroup to post my message

    > to. Anyways, any
    > >advice is very helpful to me as I am an absolute novice

    > in Web Forms.
    > >Here is a small piece of code. It is actually a

    > lightweight scenario of what
    > >is going to happen in my project.
    > >------------------------------------------------
    > >namespace bluebook
    > >{
    > > public class Testpage : Page
    > > {
    > > protected PlaceHolder ctlPlaceHolder; //ctlPlaceHolder

    > also resides on the
    > >corresponding aspx page
    > >
    > > private void Page_Load(object sender, System.EventArgs

    > e)
    > > {
    > > LinkButton aLB = new LinkButton();
    > > aLB.CommandName = "Command 1";
    > > aLB.Text = "Level 1";
    > > aLB.Command += new CommandEventHandler(OnCommand);
    > >
    > > ctlPlaceHolder.Controls.Add(aLB);
    > > this.SaveViewState();
    > > }
    > >
    > > private void OnCommand(object sender, CommandEventArgs

    > args)
    > > {
    > > LinkButton aLB = (LinkButton) sender;
    > > int a = Int32.Parse( aLB.CommandName[8].ToString() );
    > > a++;
    > >
    > > LinkButton aNewLB = new LinkButton();
    > > aNewLB.CommandName = "Command " + a.ToString();
    > > aNewLB.Text = "Level " + a.ToString();
    > >
    > > ctlPlaceHolder.Controls.Clear();
    > > ctlPlaceHolder.Controls.Add(aNewLB);
    > > }
    > >
    > >
    > >
    > > #region Web Form Designer generated code
    > > override protected void OnInit(EventArgs e)
    > > {
    > > InitializeComponent();
    > > base.OnInit(e);
    > > }
    > > private void InitializeComponent()
    > > {
    > > this.Load += new System.EventHandler(this.Page_Load);
    > > }
    > > #endregion
    > > }
    > >}
    > >------------------------------------------------
    > >
    > >What I was expecting is that every time I click the

    > hyperlink, the form is
    > >regenerated and the text inside the hyperlink is

    > incremented. So first it
    > >displays Level 1, then Level 2, then Level 3, ..., Level

    > 10, Level 2, ...
    > >and so on. But instead it only increments once, and then

    > goes back to the
    > >initial state.
    > >I realize that changing the hyperlink's text can be

    > achieved without
    > >creating a new control each time an event is raised. But

    > as I have already
    > >said, this is just a sample, and in the real project I

    > am working on I DO
    > >NEED to create the control dynamically each time I

    > process the event.
    > >Does anyone know a workaround? Or, maybe, it is a bug in

    > the technology?
    > >Regards,
    > >Kirill Osipov
    > >
    > >
    > >.
    > >
     
    Kirill Osipov, Sep 12, 2003
    #3
  4. Kirill Osipov

    Ezra Epstein Guest

    I do know that the one of the short-comings of ASP.NET is that it doesn't
    handle non-declarative (i.e., dynamic) stuff very well.

    The key problem seems to be the way that ViewState was implemented.

    For your control to work several things need to happen:

    1. The control must be added before the call to SaveViewState in the
    parent control (e.g., the page). Doing this at Page_Load as you have done
    should work.
    2. on post-back the control MUST be re-added before LoadViewState is
    called. I.e., way before Page_Load. You can override OnInit() to handle
    this. However, the VS designer likes to override this method so you might
    not be able to use VS's dynamic layout tools on the page in question.
    3. You must ensure that the control participates in the ViewState
    methods. I'm still looking for documentation on that one.
    4. Finally, when the control is rendered it must generate the call to
    postback on the client (do a view page source in your browser to see how a
    regular control does this). You *can* do this manually, but there should be
    a way to have the control do this.

    The ASP,Net model is great for the visual version of a "power" developer:
    drag & drop whooo-wheee. It's actually really great so long as you're
    clearly within the bounds that the MS engineers defined as being the problem
    space. The trouble comes when you step across those bounds. (a) it seems
    they didn't design it to handle truly dynamic content creation (the real
    power developer) and they didn't document it too well either.... It's a
    bummer. As you know the .aspx or .ascx is a sub-class of the code-behind.
    This subclass is hooked up to the page rendering mechanism ... trouble is
    figuring out the non-documented parts of that mechanism.

    Good luck, hope this starts. Whatever else you discover, please share.

    Ezra e.


    "Kirill Osipov" <> wrote in message
    news:%...
    > Hello everybody!
    >
    > Sorry if it's not the right newsgroup to post my message to. Anyways, any
    > advice is very helpful to me as I am an absolute novice in Web Forms.
    > Here is a small piece of code. It is actually a lightweight scenario of

    what
    > is going to happen in my project.
    > ------------------------------------------------
    > namespace bluebook
    > {
    > public class Testpage : Page
    > {
    > protected PlaceHolder ctlPlaceHolder; //ctlPlaceHolder also resides on

    the
    > corresponding aspx page
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > LinkButton aLB = new LinkButton();
    > aLB.CommandName = "Command 1";
    > aLB.Text = "Level 1";
    > aLB.Command += new CommandEventHandler(OnCommand);
    >
    > ctlPlaceHolder.Controls.Add(aLB);
    > this.SaveViewState();
    > }
    >
    > private void OnCommand(object sender, CommandEventArgs args)
    > {
    > LinkButton aLB = (LinkButton) sender;
    > int a = Int32.Parse( aLB.CommandName[8].ToString() );
    > a++;
    >
    > LinkButton aNewLB = new LinkButton();
    > aNewLB.CommandName = "Command " + a.ToString();
    > aNewLB.Text = "Level " + a.ToString();
    >
    > ctlPlaceHolder.Controls.Clear();
    > ctlPlaceHolder.Controls.Add(aNewLB);
    > }
    >
    >
    >
    > #region Web Form Designer generated code
    > override protected void OnInit(EventArgs e)
    > {
    > InitializeComponent();
    > base.OnInit(e);
    > }
    > private void InitializeComponent()
    > {
    > this.Load += new System.EventHandler(this.Page_Load);
    > }
    > #endregion
    > }
    > }
    > ------------------------------------------------
    >
    > What I was expecting is that every time I click the hyperlink, the form is
    > regenerated and the text inside the hyperlink is incremented. So first it
    > displays Level 1, then Level 2, then Level 3, ..., Level 10, Level 2, ...
    > and so on. But instead it only increments once, and then goes back to the
    > initial state.
    > I realize that changing the hyperlink's text can be achieved without
    > creating a new control each time an event is raised. But as I have already
    > said, this is just a sample, and in the real project I am working on I DO
    > NEED to create the control dynamically each time I process the event.
    > Does anyone know a workaround? Or, maybe, it is a bug in the technology?
    > Regards,
    > Kirill Osipov
    >
    >
     
    Ezra Epstein, Sep 13, 2003
    #4
    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. Jeff Voigt

    Dynamic Control Creation w/Events?

    Jeff Voigt, Jul 8, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    371
    Natty Gur
    Jul 8, 2003
  2. PABruceFan
    Replies:
    3
    Views:
    4,131
    John Saunders
    Jul 31, 2004
  3. =?Utf-8?B?UGF1bA==?=

    Dynamic control creation design issue

    =?Utf-8?B?UGF1bA==?=, Feb 21, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    429
    =?Utf-8?B?UGF1bA==?=
    Feb 21, 2006
  4. Hugo Flores
    Replies:
    1
    Views:
    501
    daniel #
    Sep 6, 2006
  5. Replies:
    1
    Views:
    353
    Andrew Thompson
    Sep 14, 2006
Loading...

Share This Page