Dynamically Creating Controls at Runtime

Discussion in 'ASP .Net Web Controls' started by Mike Towle, Apr 14, 2004.

  1. Mike Towle

    Mike Towle Guest

    Trying to dynamically create and bind (to a form) controls at runtime in the button_click event, not in Page_Init. If I create and bind the controls in Page_Init, they work fine. If I try doing this in the button_click event several things happen
    1. The control.count property of the container that the control was added to (usually a form) does not change
    2. I loose control of that control. I cannot clear the control or dispose of it on subsiquent calls to the webform. For instance if I dynamically create a dropdownlist control, I cannot get rid of it and I cannot clear the items list on the next call to the webform

    How can I dynamically create and dispose controls in runtime that I can control on subsiquent calls to the webform? This application makes calls to a database and the control typs that are created depend upon what the values are in the database. One time I might use a textbox control because the value in the database is 'T' while another time I might use a dropdownlist if the value is 'D'.
     
    Mike Towle, Apr 14, 2004
    #1
    1. Advertising

  2. try this beauty: customize to your liking. this snippet will not compile

    Type type = Type.GetType("System.Web.UI.WebControls." +
    row.ObjectType + ", System.Web,Version=1.0.3300.0, Culture=neutral,
    PublicKeyToken=b03f5f7f11d50a3a");
    System.Web.UI.Control temp =
    (System.Web.UI.Control)Activator.CreateInstance(type);


    --
    Regards,
    Alvin Bruney [ASP.NET MVP]
    Got tidbits? Get it here...
    http://tinyurl.com/27cok
    "Mike Towle" <> wrote in message
    news:D...
    > Trying to dynamically create and bind (to a form) controls at runtime in
    > the button_click event, not in Page_Init. If I create and bind the
    > controls in Page_Init, they work fine. If I try doing this in the
    > button_click event several things happen:
    > 1. The control.count property of the container that the control was added
    > to (usually a form) does not change.
    > 2. I loose control of that control. I cannot clear the control or
    > dispose of it on subsiquent calls to the webform. For instance if I
    > dynamically create a dropdownlist control, I cannot get rid of it and I
    > cannot clear the items list on the next call to the webform.
    >
    > How can I dynamically create and dispose controls in runtime that I can
    > control on subsiquent calls to the webform? This application makes calls
    > to a database and the control typs that are created depend upon what the
    > values are in the database. One time I might use a textbox control
    > because the value in the database is 'T' while another time I might use a
    > dropdownlist if the value is 'D'.
     
    Alvin Bruney [MVP], Apr 16, 2004
    #2
    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. Uzi Baruch
    Replies:
    0
    Views:
    456
    Uzi Baruch
    Dec 17, 2003
  2. Mark Siffer

    adding controls dynamically at runtime

    Mark Siffer, Jun 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    552
    Mark Siffer
    Jun 21, 2004
  3. Bart Schelkens

    Creating controls at runtime

    Bart Schelkens, Sep 29, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    469
    Lucas Tam
    Sep 29, 2004
  4. Varangian
    Replies:
    7
    Views:
    591
    Varangian
    Apr 27, 2006
  5. The Crow

    Dynamically placing controls/ page items at runtime

    The Crow, Oct 26, 2005, in forum: ASP .Net Building Controls
    Replies:
    7
    Views:
    181
    Grant
    Oct 31, 2005
Loading...

Share This Page