Creating Form Controls at Run-Time

Discussion in 'ASP .Net' started by Tarang Deshpande, Jul 24, 2003.

  1. I would like to be able to create controls at run-time
    rather than at design time. Is there a function that I
    can override so that I can output the appropriate html
    code between when the <body> and </body> tags are outputed.

    I don't have to have web form controls but could just be
    plain html controls.

    Thanks
    Tarang Deshpande, Jul 24, 2003
    #1
    1. Advertising

  2. Tarang Deshpande

    Natty Gur Guest

    You can create server controls or Html controls at the page_load event.
    you can also attach them to events :

    System.Web.UI.WebControls.ListBox oLst = new
    System.Web.UI.WebControls.ListBox();
    oLst.Enabled = true;
    oLst.EnableViewState = true;
    oLst.ID = "cboDevQueueList";
    oLst.AutoPostBack = true;
    oLst.Items.Add ("a");
    oLst.Items.Add ("b");
    oLst.Visible = true;
    oLst.SelectedIndexChanged += new System.EventHandler(this.SelItem);
    // ad the control inside the form
    ("WebForm6").Controls.Add(oLst);

    Natty Gur, CTO
    Dao2Com Ltd.
    28th Baruch Hirsch st. Bnei-Brak
    Israel , 51114

    Phone Numbers:
    Office: +972-(0)3-5786668
    Fax: +972-(0)3-5703475
    Mobile: +972-(0)58-888377

    Know the overall picture


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Natty Gur, Jul 24, 2003
    #2
    1. Advertising

  3. How about if you want to create html controls rather than
    web controls what do you do? I tried using Response.Write
    during the Page_Load but that only puts the html code
    before everything else from the ASPX page.


    >-----Original Message-----
    >You can create server controls or Html controls at the

    page_load event.
    >you can also attach them to events :
    >
    >System.Web.UI.WebControls.ListBox oLst = new
    >System.Web.UI.WebControls.ListBox();
    >oLst.Enabled = true;
    >oLst.EnableViewState = true;
    >oLst.ID = "cboDevQueueList";
    >oLst.AutoPostBack = true;
    >oLst.Items.Add ("a");
    >oLst.Items.Add ("b");
    >oLst.Visible = true;
    >oLst.SelectedIndexChanged += new System.EventHandler

    (this.SelItem);
    >// ad the control inside the form
    >("WebForm6").Controls.Add(oLst);
    >
    >Natty Gur, CTO
    >Dao2Com Ltd.
    >28th Baruch Hirsch st. Bnei-Brak
    >Israel , 51114
    >
    >Phone Numbers:
    >Office: +972-(0)3-5786668
    >Fax: +972-(0)3-5703475
    >Mobile: +972-(0)58-888377
    >
    >Know the overall picture
    >
    >
    >*** Sent via Developersdex http://www.developersdex.com

    ***
    >Don't just participate in USENET...get rewarded for it!
    >.
    >
    Tarang Deshpande, Jul 24, 2003
    #3
  4. Tarang Deshpande

    Natty Gur Guest

    The same way and location just use Html control classes:

    System.Web.UI.HtmlControls.HtmlInputText o = new
    System.Web.UI.HtmlControls.HtmlInputText ();
    o.Value = "natty";
    o.Visible = true;
    this.FindControl("WebForm6").Controls.Add(o);

    Natty Gur, CTO
    Dao2Com Ltd.
    28th Baruch Hirsch st. Bnei-Brak
    Israel , 51114

    Phone Numbers:
    Office: +972-(0)3-5786668
    Fax: +972-(0)3-5703475
    Mobile: +972-(0)58-888377

    Know the overall picture


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Natty Gur, Jul 24, 2003
    #4
  5. Joao S Cardoso [MVP], Jul 24, 2003
    #5
  6. This doesn't quite work. I added label controls and if
    you look at the gnerated html code at the client the end
    result is rendered after the </HTML> tag and thus outside
    of the form. Also each label was between <span></span>
    tags.


    >-----Original Message-----
    >You can create server controls or Html controls at the

    page_load event.
    >you can also attach them to events :
    >
    >System.Web.UI.WebControls.ListBox oLst = new
    >System.Web.UI.WebControls.ListBox();
    >oLst.Enabled = true;
    >oLst.EnableViewState = true;
    >oLst.ID = "cboDevQueueList";
    >oLst.AutoPostBack = true;
    >oLst.Items.Add ("a");
    >oLst.Items.Add ("b");
    >oLst.Visible = true;
    >oLst.SelectedIndexChanged += new System.EventHandler

    (this.SelItem);
    >// ad the control inside the form
    >("WebForm6").Controls.Add(oLst);
    >
    >Natty Gur, CTO
    >Dao2Com Ltd.
    >28th Baruch Hirsch st. Bnei-Brak
    >Israel , 51114
    >
    >Phone Numbers:
    >Office: +972-(0)3-5786668
    >Fax: +972-(0)3-5703475
    >Mobile: +972-(0)58-888377
    >
    >Know the overall picture
    >
    >
    >*** Sent via Developersdex http://www.developersdex.com

    ***
    >Don't just participate in USENET...get rewarded for it!
    >.
    >
    Tarang Deshpande, Jul 25, 2003
    #6
  7. Tarang Deshpande

    Natty Gur Guest

    Sorry, I have mistake at this line :>("WebForm6").Controls.Add(oLst);

    this.controls["YourFormName"].Controls.Add(oLst);

    you need to use the right Controls collection to add your control to.

    Natty Gur, CTO
    Dao2Com Ltd.
    28th Baruch Hirsch st. Bnei-Brak
    Israel , 51114

    Phone Numbers:
    Office: +972-(0)3-5786668
    Fax: +972-(0)3-5703475
    Mobile: +972-(0)58-888377

    Know the overall picture


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Natty Gur, Jul 25, 2003
    #7
    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. Anand Sagar
    Replies:
    2
    Views:
    1,970
  2. ray well

    creating webform controls at run time

    ray well, Feb 25, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    589
  3. Pierre Yves
    Replies:
    2
    Views:
    473
    Pierre Yves
    Jan 10, 2008
  4. Replies:
    1
    Views:
    173
    Teemu Keiski
    Dec 8, 2005
  5. Replies:
    1
    Views:
    178
    Teemu Keiski
    Dec 8, 2005
Loading...

Share This Page