Karatas, FERHAT
I would love to change the world, but they won't give me the source code!
HOW TO: Dynamically Create Controls in ASP.NET
24.9.2008 10:07:17 - Filed under : Asp.net
This article demonstrates how to dynamically create/handle a control for an .aspx page.

<form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
                <asp:ListItem>1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
                <asp:ListItem>3</asp:ListItem>
                <asp:ListItem>4</asp:ListItem>
                <asp:ListItem>5</asp:ListItem>
                <asp:ListItem>6</asp:ListItem>
                <asp:ListItem></asp:ListItem>
            </asp:DropDownList>
            <asp:Label ID="Label1" runat="server"></asp:Label><br />
            <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
            <br />
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Read" />
        </div>
    </form>

        protected void Page_Load(object sender, EventArgs e)
        {
            CreateTextBox(Int32.Parse(DropDownList1.SelectedItem.Value));
        }

        void CreateTextBox(int Total)
        {
            for (int i = 0; i < Total; i++)
            {
                TextBox TextBox1 = new TextBox();
                TextBox1.ID = "TextBox1" + i.ToString();
                TextBox1.TextChanged += new EventHandler(TextBox_TextChanged);
                PlaceHolder1.Controls.Add(TextBox1);   
            }
        }

        void TextBox_TextChanged(object sender, EventArgs e)
        {
            TextBox txtBoxSender = (TextBox)sender;
            Label1.Text += txtBoxSender.Text +"-";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            // Todo : other operation
        }

Another example : http://support.microsoft.com/kb/317794

Keywords : Create and handle dynamic controls.
with 0 comments

What do you think ?
Security Code 1287
Check S. Code
Name  
Email  
Home Page  
Comments