ASP.NET動(dòng)態(tài)添加控件一例
更新時(shí)間:2012年01月09日 21:10:27 作者:
本例需求來自CSDN論壇,有人問:3個(gè)DropDownList和1個(gè)按鈕,每次單擊按鈕動(dòng)態(tài)創(chuàng)建3個(gè)Label控件并從DropDownList獲得值,然后添加到頁面
第一次單擊頁面中有3個(gè)Label,第二次單擊有6個(gè),第三次單擊有9個(gè),也就是每次單擊要在上次的狀態(tài)下再添加3個(gè)。
我的方法是,可以通過Session來保存上次的狀態(tài),一種解法如下:
Test.aspx關(guān)鍵代碼:
<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="最后一個(gè)Button" />
</form>
Test.aspx.cs關(guān)鍵代碼:
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;
}
當(dāng)頁面回發(fā)時(shí),先記下Panel1在控件樹中的位置,并移除它,然后從Session變量獲取上次添加后的Panel1,并添加到控件樹中原來的位置,在這基礎(chǔ)上繼續(xù)添加新的Label控件。最后的那個(gè)Button是為了測(cè)試之用,作用有二:一是幫助查看添加的位置是否正確,二是用于檢測(cè)空回發(fā)時(shí)是否能維持上次的狀態(tài)。
我的方法是,可以通過Session來保存上次的狀態(tài),一種解法如下:
Test.aspx關(guān)鍵代碼:
復(fù)制代碼 代碼如下:
<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="最后一個(gè)Button" />
</form>
Test.aspx.cs關(guān)鍵代碼:
復(fù)制代碼 代碼如下:
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;
}
當(dāng)頁面回發(fā)時(shí),先記下Panel1在控件樹中的位置,并移除它,然后從Session變量獲取上次添加后的Panel1,并添加到控件樹中原來的位置,在這基礎(chǔ)上繼續(xù)添加新的Label控件。最后的那個(gè)Button是為了測(cè)試之用,作用有二:一是幫助查看添加的位置是否正確,二是用于檢測(cè)空回發(fā)時(shí)是否能維持上次的狀態(tài)。
您可能感興趣的文章:
- ASP.NET服務(wù)器端控件RadioButtonList,DropDownList,CheckBoxList的取值、賦值用法
- asp.net 用戶控件讀取以及賦值
- asp.net GridView控件中模板列CheckBox全選、反選、取消
- asp.net+jquery滾動(dòng)滾動(dòng)條加載數(shù)據(jù)的下拉控件
- Asp.net 菜單控件簡(jiǎn)潔版
- ASP.NET 頁面中動(dòng)態(tài)增加的控件、添加事件
- asp.net Datalist控件實(shí)現(xiàn)分頁功能
- asp.net fileupload控件上傳文件與多文件上傳
- asp.net分頁控件AspNetPager的樣式美化
- asp.net Menu控件+SQLServer實(shí)現(xiàn)動(dòng)態(tài)多級(jí)菜單
- asp.net 動(dòng)態(tài)生成控件并獲取其值
- asp.net動(dòng)態(tài)加載用戶控件,關(guān)于后臺(tái)添加、修改的思考
- asp.net顯示圖片到指定的Image控件中 具體實(shí)現(xiàn)
- asp.net簡(jiǎn)單頁面控件賦值實(shí)現(xiàn)方法
相關(guān)文章
asp.net下用js實(shí)現(xiàn)鼠標(biāo)移至小圖,自動(dòng)顯示相應(yīng)大圖
asp.net下用js實(shí)現(xiàn)鼠標(biāo)移至小圖,自動(dòng)顯示相應(yīng)大圖...2007-03-03
ASP.Net Post方式獲取數(shù)據(jù)流的一種簡(jiǎn)單寫法
這篇文章主要介紹了ASP.Net Post方式獲取數(shù)據(jù)流的一種簡(jiǎn)單寫法,本文直接給出代碼實(shí)例,需要的朋友可以參考下2015-05-05
微信公眾平臺(tái)開發(fā)之自定義菜單.Net代碼解析
這篇文章主要為大家詳細(xì)解析了微信公眾平臺(tái)開發(fā)之自定義菜單.Net代碼,感興趣的小伙伴們可以參考一下2016-06-06
asp.net實(shí)現(xiàn)的群發(fā)郵件功能詳解
這篇文章主要介紹了asp.net實(shí)現(xiàn)的群發(fā)郵件功能,結(jié)合具體實(shí)例形式分析了asp.net基于SMTP服務(wù)群發(fā)QQ郵件的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-05-05
Asp.net基于ajax和jquery-ui實(shí)現(xiàn)進(jìn)度條
這篇文章主要介紹了Asp.net基于ajax和jquery-ui實(shí)現(xiàn)進(jìn)度條,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
asp.net結(jié)合Ajax驗(yàn)證用戶名是否存在的代碼
關(guān)于Ajax的操作簡(jiǎn)單總結(jié),結(jié)合Ajax驗(yàn)證用戶名是否存在的代碼2010-06-06
在應(yīng)用程序級(jí)別之外使用注冊(cè)為allowDefinition=''MachineToApplication''的節(jié)是錯(cuò)誤的
在應(yīng)用程序級(jí)別之外使用注冊(cè)為 allowDefinition='MachineToApplication' 的節(jié)是錯(cuò)誤的2009-03-03

