Wednesday, June 16, 2010

How to create a asp.net control using string text - Asp.net

This article demonstrate how to create a asp.net control and add it to the controls collection based on string text value.
Please note the original reference form asp.net forums


<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title></title>
    <script runat="server">
        protected void Button1_Click(object sender, EventArgs e)
        {
            string controlName = TextBox1.Text;
            string systemweblib = @"C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Web.dll";
            System.Reflection.Assembly ass = System.Reflection.Assembly.LoadFile(systemweblib);
            Type type = ass.GetType("System.Web.UI.WebControls." + controlName);
            Control c = (Control)Activator.CreateInstance(type);
            Panel1.Controls.Add(c);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Create Control" 
            onclick="Button1_Click" />
        <asp:Panel ID="Panel1" runat="server">
        </asp:Panel>
    </div>
    </form>
</body>
</html>

3 comments:

.NET Development said...

Great job. Because i'm a newbie in this area this topic was the most useful for me.

Joshua Smith said...

Thank you for saying about useful info. It necessary to know that iphone app development could help in your industry by installing customized software products.

Amethyst Walton said...

Good to see the example of related ASP .NET.example is the way of learning and understanding of each thing than the content.

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...