Click here to Skip to main content
15,897,371 members
Articles / Web Development / HTML

Dynamic Loading of ASP.NET User Controls

Rate me:
Please Sign up or sign in to vote.
3.87/5 (35 votes)
4 Jun 2010CPOL3 min read 369.8K   9.8K   63  
Demonstrates how to dynamically add User Controls to pages at runtime using the LoadControl method
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@ Register src="~/usercontrols/SimpleControl.ascx" tagname="SimpleControl" tagprefix="SimpleControl" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>This is example of how to add usercontrol dynamically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <asp:Button ID="btnAddControl" runat="server" Text="Click to add SimpleControl" onclick="btnAddControl_Click" />
            <br />
            <asp:PlaceHolder runat="server" ID="Placeholder1" ></asp:PlaceHolder>
            <br />
            <asp:Label ID="lblUser" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions