用户控件动态加载
能够解决Postback后的问题,但是俺有点小笨,所以浪费的资源有点多,但是基本的解决思路还是有的
可以提供一个思路,一个很笨的思路!
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Collections.Generic;
12
13public partial class AddControlTest1 : System.Web.UI.Page
14{
15 private static IList<TableRow> arrayTableRow = new List<TableRow>();
16
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 if (arrayTableRow != null)
20 {
21 foreach (TableRow tr in arrayTableRow)
22 {
23 Table1.Rows.Add(tr);
24 }
25 }
26 AddButton();
27 }
28
29 protected void Button1_Click(object sender, EventArgs e)
30 {
31 AddTextBoxs();
32 }
33 private void AddTextBoxs()
34 {
35 TableRow tr = new TableRow();
36
37 TableCell tc1 = new TableCell();
38 TextBox t = new TextBox();
39 t.ID = "tb" + Table1.Rows.Count;
40
41 tc1.Controls.Add(t);
42
43 TableCell tc2 = new TableCell();
44 DropDownList dpl = new DropDownList();
45 dpl.ID = "dpl" + Table1.Rows.Count;
46 for (int i = 0; i < 10; i++) dpl.Items.Add(i.ToString());
47 tc2.Controls.Add(dpl);
48
49 TableCell tc3 = new TableCell();
50 Label lb1 = new Label();
51 lb1.ID = "lb" + Table1.Rows.Count;
52 lb1.Style.Value = "color:#FF0000";
53 tc3.Controls.Add(lb1);
54
55 tr.Cells.Add(tc1);
56 tr.Cells.Add(tc2);
57 tr.Cells.Add(tc3);
58 arrayTableRow.Add(tr);
59 Table1.Rows.Add(tr);
60 }
61
62 private void AddButton()
63 {
64 Button b = new Button();
65 b.ID = "btn";
66 b.Text = "按钮";
67 b.Click += new System.EventHandler(btn_Click);
68 PlaceHolder1.Controls.Add(b);
69 }
70 private void btn_Click(object sender, System.EventArgs e)
71 {
72 for (int i = 0; i < Table1.Rows.Count; i++)
73 {
74 //Response.Write(((TextBox)Table1.Rows[i].FindControl("tb" + i)).Text + ((DropDownList)Table1.Rows[i].FindControl("dpl" + i)).SelectedValue + "<br>");
75 ((Label)Table1.Rows[i].FindControl("lb" + i)).Text = ((DropDownList)Table1.Rows[i].FindControl("dpl" + i)).SelectedValue;
76 }
77 }
78}
79
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Collections.Generic;
12
13public partial class AddControlTest1 : System.Web.UI.Page
14{
15 private static IList<TableRow> arrayTableRow = new List<TableRow>();
16
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 if (arrayTableRow != null)
20 {
21 foreach (TableRow tr in arrayTableRow)
22 {
23 Table1.Rows.Add(tr);
24 }
25 }
26 AddButton();
27 }
28
29 protected void Button1_Click(object sender, EventArgs e)
30 {
31 AddTextBoxs();
32 }
33 private void AddTextBoxs()
34 {
35 TableRow tr = new TableRow();
36
37 TableCell tc1 = new TableCell();
38 TextBox t = new TextBox();
39 t.ID = "tb" + Table1.Rows.Count;
40
41 tc1.Controls.Add(t);
42
43 TableCell tc2 = new TableCell();
44 DropDownList dpl = new DropDownList();
45 dpl.ID = "dpl" + Table1.Rows.Count;
46 for (int i = 0; i < 10; i++) dpl.Items.Add(i.ToString());
47 tc2.Controls.Add(dpl);
48
49 TableCell tc3 = new TableCell();
50 Label lb1 = new Label();
51 lb1.ID = "lb" + Table1.Rows.Count;
52 lb1.Style.Value = "color:#FF0000";
53 tc3.Controls.Add(lb1);
54
55 tr.Cells.Add(tc1);
56 tr.Cells.Add(tc2);
57 tr.Cells.Add(tc3);
58 arrayTableRow.Add(tr);
59 Table1.Rows.Add(tr);
60 }
61
62 private void AddButton()
63 {
64 Button b = new Button();
65 b.ID = "btn";
66 b.Text = "按钮";
67 b.Click += new System.EventHandler(btn_Click);
68 PlaceHolder1.Controls.Add(b);
69 }
70 private void btn_Click(object sender, System.EventArgs e)
71 {
72 for (int i = 0; i < Table1.Rows.Count; i++)
73 {
74 //Response.Write(((TextBox)Table1.Rows[i].FindControl("tb" + i)).Text + ((DropDownList)Table1.Rows[i].FindControl("dpl" + i)).SelectedValue + "<br>");
75 ((Label)Table1.Rows[i].FindControl("lb" + i)).Text = ((DropDownList)Table1.Rows[i].FindControl("dpl" + i)).SelectedValue;
76 }
77 }
78}
79
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddControlTest1.aspx.cs" Inherits="AddControlTest1" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>无标题页</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:Table id="Table1" runat="server"></asp:Table>
13 <asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder><br /><br />
14 <asp:Button id="Button1" runat="server" Text="添加一行" OnClick="Button1_Click"></asp:Button>
15 </div>
16 </form>
17</body>
18</html>
19
就那么多,可以解决一次多插入的问题.2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>无标题页</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:Table id="Table1" runat="server"></asp:Table>
13 <asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder><br /><br />
14 <asp:Button id="Button1" runat="server" Text="添加一行" OnClick="Button1_Click"></asp:Button>
15 </div>
16 </form>
17</body>
18</html>
19
可以提供一个思路,一个很笨的思路!