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.

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...