Add controls dynamically in the ASP.net page

Source: Internet
Author: User

Asked today how to dynamically create a batch of controls on a ASP.net page, and you want to have access to these dynamically created controls in subsequent code. I use the following example to explain this problem

Paging File:

     <title></title>
<body>
     <form id="form1" runat="server">
     <div>
     <asp:Button ID="createbutton" runat="server" Text="批量创建按 钮"
             onclick="createbutton_Click" /><asp:Button  ID="displaybutton"
             runat="server"  Text="显示动态按钮的信息"  onclick="displaybutton_Click"/>
     <asp:Table ID="HolderTable"   runat="server"></asp:Table>
     </div>
     </form>
</body>

The table above----is used to hold the dynamic control, with the table because it has the concept of row and column, easier to layout design. In addition to table, you can also use the placeholder control or the Panel control

Code files:

public partial class _default:system.web.ui.page


{


protected override void OnLoad (EventArgs e)


     {


CreateControl ();


     }


protected void Createbutton_click (object sender, EventArgs e)


     {


if (viewstate["createcontrol"] = = null)


         {


viewstate["CreateControl"] = true;


CreateControl ();


         }


     }


void CreateControl () {


///Batch Create 100 buttons


         /// 


if (viewstate["CreateControl"]==null) return; The first time you should not create these controls


for (int x = 0; x &lt; x. x + +)


         {


TableRow row = new TableRow ();


for (int y = 0; y &lt; y++)


             {


TableCell cell = new TableCell ();


button BT = New button ();


BT. Text = string. Format ("x= {0},y={1}", X, y);


BT. Click + + new EventHandler (Bt_click);


cell. Controls.Add (BT);


row. Cells.add (cell);


             }


HolderTable.Rows.Add (row);


         }


     }


void Bt_click (object sender, EventArgs e)


     {


Trace.Write ("Control dynamic event");


(Button) sender). BackColor = System.Drawing.Color.Red;


Response.Write (String. Format ("You clicked the button: {0}", (button) sender). Text));


     }


///&lt;summary&gt;


///displays information for dynamically created controls


///&lt;/summary&gt;


&lt;param name= "Sender" &gt;&lt;/param&gt;


///&lt;param name= "E" &gt;&lt;/param&gt;


protected void Displaybutton_click (object sender, EventArgs e)


     {


for (int x = 0; x &lt; x. x + +)


         {


TableRow row = holdertable.rows[x];


for (int y = 0; y &lt; y++)


             {


button BT = (button) row. Cells [y]. Controls[0];


Response.Write (BT. Text);


             }


         }


     }


}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.