locked
Creating controls dynamically at runtime RRS feed

  • Question

  • User-132834496 posted

    Hello, Can we create controls dynamically at runtime? For example I want a new button( or label or image etc.) to be created and displayed at a particular position depending on my selection of items in the dropdown list. I dont want to create the items already and hide them until required. What I want is to create a new control at runtime depending on my selection. Is it possible in ASP.NET? If so how? I would be glad is someone posts the code for it. Thanks a lot.

    Friday, August 8, 2008 7:29 AM

Answers

  • User316207178 posted

    You can create controls dynamically at runtime.

    Use  the following code:

     

    protected void Page_Load(object sender, EventArgs e)
    {
    Button btn = new Button();
    btn.Text = "Click here";

    //Assume that there's a PlaceHolder in your .ASPX page with ID phSample
    phSample.Controls.Add(btn);

    //the button will be added to the PlaceHolder
    }
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 8, 2008 9:16 AM
  • User-324533553 posted

    yes you can and hope this will get you on road

     
    <%@ Page Language="VB" %>
    <%@ Import Namespace="System.Web.UI.HtmlControls" %>
    
    <script language="VB" runat="server">
        Sub Page_Load(Sender As Object, E As EventArgs)
            Dim clicker As HtmlButton = New HtmlButton()
            clicker.InnerText = "Button 1"
            Bullseye.Controls.Add(clicker)
    
            clicker = New HtmlButton()
            clicker.InnerText = "Button 2"
            Bullseye.Controls.Add(clicker)
    
            clicker = New HtmlButton()
            clicker.InnerText = "Button 3"
            Bullseye.Controls.Add(clicker)
    
            clicker = New HtmlButton()
            clicker.InnerText = "Button 4"
            Bullseye.Controls.Add(clicker)
        End Sub
    </script>
    <html>
    <body>
    
        <h3><font face="Verdana">PlaceHolder Example</font></h3>
    
        <asp:PlaceHolder id="Bullseye" runat="server" />
    
    </body>
    </html>
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 8, 2008 9:41 AM

All replies

  • User316207178 posted

    You can create controls dynamically at runtime.

    Use  the following code:

     

    protected void Page_Load(object sender, EventArgs e)
    {
    Button btn = new Button();
    btn.Text = "Click here";

    //Assume that there's a PlaceHolder in your .ASPX page with ID phSample
    phSample.Controls.Add(btn);

    //the button will be added to the PlaceHolder
    }
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 8, 2008 9:16 AM
  • User-324533553 posted

    yes you can and hope this will get you on road

     
    <%@ Page Language="VB" %>
    <%@ Import Namespace="System.Web.UI.HtmlControls" %>
    
    <script language="VB" runat="server">
        Sub Page_Load(Sender As Object, E As EventArgs)
            Dim clicker As HtmlButton = New HtmlButton()
            clicker.InnerText = "Button 1"
            Bullseye.Controls.Add(clicker)
    
            clicker = New HtmlButton()
            clicker.InnerText = "Button 2"
            Bullseye.Controls.Add(clicker)
    
            clicker = New HtmlButton()
            clicker.InnerText = "Button 3"
            Bullseye.Controls.Add(clicker)
    
            clicker = New HtmlButton()
            clicker.InnerText = "Button 4"
            Bullseye.Controls.Add(clicker)
        End Sub
    </script>
    <html>
    <body>
    
        <h3><font face="Verdana">PlaceHolder Example</font></h3>
    
        <asp:PlaceHolder id="Bullseye" runat="server" />
    
    </body>
    </html>
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 8, 2008 9:41 AM
  • User-132834496 posted

    Thanks a lot for the code.

    It was really helpful to me. [Yes]

    Monday, August 11, 2008 7:12 AM