用户控件动态加载

能够解决Postback后的问题,但是俺有点小笨,所以浪费的资源有点多,但是基本的解决思路还是有的
 1 None.gif using  System;
 2 None.gif using  System.Data;
 3 None.gif using  System.Configuration;
 4 None.gif using  System.Collections;
 5 None.gif using  System.Web;
 6 None.gif using  System.Web.Security;
 7 None.gif using  System.Web.UI;
 8 None.gif using  System.Web.UI.WebControls;
 9 None.gif using  System.Web.UI.WebControls.WebParts;
10 None.gif using  System.Web.UI.HtmlControls;
11 None.gif using  System.Collections.Generic;
12 None.gif
13 None.gif public  partial  class  AddControlTest1 : System.Web.UI.Page
14 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
15InBlock.gif    private static IList<TableRow> arrayTableRow = new List<TableRow>();
16InBlock.gif
17InBlock.gif    protected void Page_Load(object sender, EventArgs e)
18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
19InBlock.gif        if (arrayTableRow != null)
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif            foreach (TableRow tr in arrayTableRow)
22ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
23InBlock.gif                Table1.Rows.Add(tr);
24ExpandedSubBlockEnd.gif            }

25ExpandedSubBlockEnd.gif        }

26InBlock.gif        AddButton();
27ExpandedSubBlockEnd.gif    }

28InBlock.gif
29InBlock.gif    protected void Button1_Click(object sender, EventArgs e)
30ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
31InBlock.gif        AddTextBoxs();
32ExpandedSubBlockEnd.gif    }

33InBlock.gif    private void AddTextBoxs()
34ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
35InBlock.gif        TableRow tr = new TableRow();
36InBlock.gif
37InBlock.gif        TableCell tc1 = new TableCell();
38InBlock.gif        TextBox t = new TextBox();
39InBlock.gif        t.ID = "tb" + Table1.Rows.Count;
40InBlock.gif        
41InBlock.gif        tc1.Controls.Add(t);
42InBlock.gif
43InBlock.gif        TableCell tc2 = new TableCell();
44InBlock.gif        DropDownList dpl = new DropDownList();
45InBlock.gif        dpl.ID = "dpl" + Table1.Rows.Count;
46InBlock.gif        for (int i = 0; i < 10; i++) dpl.Items.Add(i.ToString());
47InBlock.gif        tc2.Controls.Add(dpl);
48InBlock.gif
49InBlock.gif        TableCell tc3 = new TableCell();
50InBlock.gif        Label lb1 = new Label();
51InBlock.gif        lb1.ID = "lb" + Table1.Rows.Count;
52InBlock.gif        lb1.Style.Value = "color:#FF0000";
53InBlock.gif        tc3.Controls.Add(lb1);
54InBlock.gif
55InBlock.gif        tr.Cells.Add(tc1);
56InBlock.gif        tr.Cells.Add(tc2);
57InBlock.gif        tr.Cells.Add(tc3);
58InBlock.gif        arrayTableRow.Add(tr);
59InBlock.gif        Table1.Rows.Add(tr);
60ExpandedSubBlockEnd.gif    }

61InBlock.gif
62InBlock.gif    private void AddButton()
63ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
64InBlock.gif        Button b = new Button();
65InBlock.gif        b.ID = "btn";
66InBlock.gif        b.Text = "按钮";
67InBlock.gif        b.Click += new System.EventHandler(btn_Click);
68InBlock.gif        PlaceHolder1.Controls.Add(b);
69ExpandedSubBlockEnd.gif    }

70InBlock.gif    private void btn_Click(object sender, System.EventArgs e)
71ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
72InBlock.gif        for (int i = 0; i < Table1.Rows.Count; i++)
73ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
74InBlock.gif            //Response.Write(((TextBox)Table1.Rows[i].FindControl("tb" + i)).Text + ((DropDownList)Table1.Rows[i].FindControl("dpl" + i)).SelectedValue + "<br>");
75InBlock.gif            ((Label)Table1.Rows[i].FindControl("lb" + i)).Text = ((DropDownList)Table1.Rows[i].FindControl("dpl" + i)).SelectedValue;
76ExpandedSubBlockEnd.gif        }

77ExpandedSubBlockEnd.gif    }

78ExpandedBlockEnd.gif}

79 None.gif
 1 ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page Language="C#" AutoEventWireup="true" CodeFile="AddControlTest1.aspx.cs" Inherits="AddControlTest1"  %>
 2 None.gif
 3 None.gif <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
 4 None.gif
 5 None.gif < html  xmlns ="http://www.w3.org/1999/xhtml"   >
 6 None.gif < head  runat ="server" >
 7 None.gif     < title > 无标题页 </ title >
 8 None.gif </ head >
 9 None.gif < body >
10 None.gif     < form  id ="form1"  runat ="server" >
11 None.gif     < div >
12 None.gif         < asp:Table  id ="Table1"  runat ="server" ></ asp:Table >
13 None.gif         < asp:PlaceHolder  id ="PlaceHolder1"  runat ="server" ></ asp:PlaceHolder >< br  />< br  />
14 None.gif         < asp:Button  id ="Button1"  runat ="server"  Text ="添加一行"  OnClick ="Button1_Click" ></ asp:Button >
15 None.gif     </ div >
16 None.gif     </ form >
17 None.gif </ body >
18 None.gif </ html >
19 None.gif
就那么多,可以解决一次多插入的问题.
可以提供一个思路,一个很笨的思路!

转载于:https://www.cnblogs.com/xiexing1986/archive/2006/10/14/528685.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值