For those who do not have the concept of dynamically adding controls, see ASP. NET Dynamic Loading controls Basics
This example shows how to dynamically Add a page control and set a return event to obtain the value entered in the dynamically added control in the event.
Note that you need to add the dynamic control in the Init or Load event so that the ViewState of ASP. NET can correctly set the parameters of the relative control.
1 <% @ Page Language = "C #" AutoEventWireup = "True" %>
2
3 <script language = "C #" runat = server>
4 public void Page_Init (object sender, System. EventArgs e)
5 {
6 Label message = new Label ();
7 message. ID = "Enter your words"; www.2cto.com
8 sourceTag. Controls. Add (message );
9
10 TextBox input = new TextBox ();
11 input. ID = "input ";
12 sourceTag. Controls. Add (input );
13
14 Button btnSayHello = new Button ();
15 btnSayHello. ID = "btnSayHello ";
16 btnSayHello. Text = "SayHello ";
17 btnSayHello. Click + = new EventHandler (SubmitBtn_Click );
18 sourceTag. Controls. Add (btnSayHello );
19}
20
21 void SubmitBtn_Click (Object sender, EventArgs e)
22 {
23 TextBox input = (TextBox) sourceTag. FindControl ("input ");
24 LiteralControl lc;
25 lc = new LiteralControl ("<H3>" + input. Text + "</H3> ");
26 sourceTag. Controls. Add (lc );
27}
28
29 </script>
30
31
32 <meta http-equiv = "content-type" content = "text/html; charset = UTF-8"/>
33
34 <body>
35 <form runat = "server">
36
37
38
39 <p/>
40 <div id = "sourceTag" runat = "server">
41 </div>
42 <p/>
43 Xiaolong
44 </form>
45 </body>
46
~~~ A dragon (babydragoner )~~~