国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET動態添加控件一例

2020-01-18 00:33:26
字體:
來源:轉載
供稿:網友
第一次單擊頁面中有3個Label,第二次單擊有6個,第三次單擊有9個,也就是每次單擊要在上次的狀態下再添加3個。
我的方法是,可以通過Session來保存上次的狀態,一種解法如下:
Test.aspx關鍵代碼:
復制代碼 代碼如下:

<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
<asp:Button ID="Button2" runat="server" Text="最后一個Button" />
</form>

Test.aspx.cs關鍵代碼:
復制代碼 代碼如下:

protected void Page_Load(object sender, EventArgs e)
{
if (Session["Panel1"] != null)
{
int index = this.Form.Controls.IndexOf(Panel1);
this.Form.Controls.RemoveAt(index);
Panel1 = Session["Panel1"] as Panel;
this.Form.Controls.AddAt(index, Panel1);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 3; i++)
{
Label label = new Label();
DropDownList ddl = this.FindControl("DropDownList" + (i + 1).ToString()) as DropDownList;
label.Text = ddl.SelectedValue;
Panel1.Controls.Add(label);
}
Literal br = new Literal();
br.Text = "<br/>";
Panel1.Controls.Add(br);
Session["Panel1"] = Panel1;
}

當頁面回發時,先記下Panel1在控件樹中的位置,并移除它,然后從Session變量獲取上次添加后的Panel1,并添加到控件樹中原來的位置,在這基礎上繼續添加新的Label控件。最后的那個Button是為了測試之用,作用有二:一是幫助查看添加的位置是否正確,二是用于檢測空回發時是否能維持上次的狀態。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿鲁科尔沁旗| 岳普湖县| 东兰县| 揭西县| 改则县| 遂溪县| 延吉市| 托克逊县| 商城县| 宜兰市| 绥滨县| 承德市| 涡阳县| 汉川市| 饶平县| 城固县| 合阳县| 武隆县| 团风县| 辽中县| 正定县| 于田县| 鱼台县| 青岛市| 镇坪县| 互助| 周口市| 黔江区| 景谷| 山阳县| 水城县| 西贡区| 大厂| 乐山市| 六安市| 博野县| 分宜县| 周至县| 银川市| 建平县| 泉州市|