Library code snippets

Adding controls to PlaceHolders dynamically

A very powerful and flexible way to program sites in ASP.NET is to one .aspx which contains a PlaceHolder control for each area of your screen. You then dynamically fill these controls during each hit of your page. This code will get you started. Notice the "FindControl" method with which you identify each control. If you want to use JavaScript on your controls, I think you have to use ClientId to access them, check it out.

<%@ Page Language="C#" %>
<script runat="server">
        private void Page_Load(object sender, System.EventArgs e)
        {
            //build placeholder
            for(int x = 0; x <= 10; x++) {
                Label title = new Label();
                title.Text = "Item " + x.ToString();
                title.ID = "Item" + x.ToString();
                Area1.Controls.Add(title);
                Area1.Controls.Add(new LiteralControl("<br>"));
            }    
        }        
       
        private void ButtonChange_Click(object sender, System.EventArgs e)
        {
            ((Label)(Area1.FindControl("Item" + txtId.Text.ToString()))).Text = txtName.Text.ToString();
        }
</script>
<html>
    <head>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <P>
                <asp:PlaceHolder id="Area1" runat="server"></asp:PlaceHolder></P>
            <P> </P>
            <P>ID:
                <asp:TextBox id="txtId" runat="server" Width="51px"></asp:TextBox></P>
            <P>New Name:
                <asp:TextBox id="txtName" runat="server"></asp:TextBox></P>
            <P> 
                <asp:Button id="ButtonChange" onclick="ButtonChange_Click" runat="server" Text="Change"></asp:Button></P>
            <P> </P>
        </form>
    </body>
</html>

Comments

  1. 07 Aug 2003 at 19:53

    Hi have your tried loading usercontrols dynamically? Does the state persist during postbacks? I have noticed that user controls were not loaded during postbacks. We  need to load them in every post back

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Adding controls to PlaceHolders dynamically.

Leave a comment

Sign in or Join us (it's free).

Edward Tanguay Edward Tanguay updates his personal web site tanguay.info weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.

Related podcasts

Events coming up

  • Mar 15

    DevWeek 2010

    London, United Kingdom

    DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.

We'd love to hear what you think! Submit ideas or give us feedback