今天被问到如何在ASP.NET 页面中动态创建一批控件,并且希望在后续代码中能访问到这些动态创建的控件。我用下面的例子来解释这个问题
=================================================================================
页面文件:
<html xmlns="http://www.w3.org/1999/xhtml%22 >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="createbutton" runat="server" Text="批量创建按钮"
onclick="createbutton_Click" /><asp:Button ID="displaybutton"
runat="server" Text="显示动态按钮的信息" onclick="displaybutton_Click"/>
<asp:Table ID="HolderTable" runat="server"></asp:Table>
</div>
</form>
</body>
</html>
----上面的Table是用来存放动态控件的,用Table是因为它有行和列的概念,更易于布局设计。除了Table之外,还可以使用PlaceHolder控件或者Panel控件
代码文件:
public partial class _Default : System.Web.UI.Page
{
protected override void OnLoad(EventArgs e)
{
CreateControl();
}
protected void createbutton_Click(object sender, EventArgs e)
{
if (ViewState["CreateControl"] == null)
{
ViewState["CreateControl"] = true;
CreateControl();
}
}
void CreateControl() {
///批量创建100个按钮
///
if (ViewState["CreateControl"]==null) return; //第一次的时候应该不要创建这些控件
for (int x = 0; x < 10; x++)
{
TableRow row = new TableRow();
for (int y = 0; y < 10; y++)
{
TableCell cell = new TableCell();
Button bt = new Button();
bt.Text = string.Format(" x={0},y={1} ", x, y);
bt.Click += new EventHandler(bt_Click);
cell.Controls.Add(bt);
row.Cells.Add(cell);
}
HolderTable.Rows.Add(row);
}
}
void bt_Click(object sender, EventArgs e)
{
Trace.Write("控件动态事件");
((Button)sender).BackColor = System.Drawing.Color.Red;
Response.Write(string.Format("你点击了该按钮:{0}", ((Button)sender).Text));
}
/// <summary>
/// 显示动态创建的控件的信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void displaybutton_Click(object sender, EventArgs e)
{
for (int x = 0; x < 10; x++)
{
TableRow row = HolderTable.Rows[x];
for (int y = 0; y < 10; y++)
{
Button bt = (Button)row.Cells[y].Controls[0];
Response.Write(bt.Text);
}
}
}
}
----这里的关键点在于,必须在Page_Load事件中每次都调用创建控件的代码。这与我们一般的理解是不一样的。
1. 首先讲为什么要?因为这些控件是动态添加的,在页面的源文件中并没有。所以PostBack回来之后重新构建页面时,非得再次创建。
2. 这样做难道不会重复创建吗?不会,因为每次都重新构建了,其实原先的控件都消失了。
3. 那么,重新构建之后控件的状态怎么保存的?这是通过ViewState自动保存的。
--在动态创建控件的页面上,如果禁用ViewState,将使得页面失去作用
测试效果:
1. 第一次打开页面时
2。点击“批量创建按钮”后
3. 点击“显示动态按钮的信息”后
4. 点击下面100个按钮中任意一个之后
【注意】
动态创建控件的行为也可以移动到用户控件里面。上述代码不需要任何改动即可实现。
2048110
这段代码:
protected void displaybutton_Click(object sender, EventArgs e)
{
for (int x = 0; x < 10; x++)
{
TableRow row = HolderTable.Rows[x];
for (int y = 0; y < 10; y++)
{
Button bt = (Button)row.Cells[y].Controls[0];
Response.Write(bt.Text);
}
}
}
貌似这样可以实现:
protected voiddisplaybutton_Click(object sender, EventArgs e)
{
Button buttons = (Button)sender;
Response.Write(buttons.Text);
}
分享到:
相关推荐
.NET中反射机制的使用与分析_-_focus_on_.NET_-_博客园
4.页面加载时执行以下代码,从数据库中获取数据并加入缓存中 5.申明缓存失效后所执行的函数 6.DataCache类 7.执行步骤为从数据库查询数据→加入缓存(同时设置过期策略以及过期回调函数)→过期时→执行过期回调函数 ...
SQL_Server_存储过程_-_hoojo_-_博客园
注入 - 代码注入之远程线程篇 - C_C++_Python_Java - 博客园.pdf
万一_-_博客园.2.rar
Ubuntu18_04 VMwareTools安装方法 - Angel_Q - 博客园.htm
安卓开发-Android自定义控件知识 - 博客园
C# 绘制统计图(柱状图, 折线图, 扇形图) - steven_2005 - 博客园
【转】c# xml添加 删除 修改数据 - badnewfish - 博客园.htm【转】c# xml添加 删除 修改数据 - badnewfish - 博客园.htm【转】c# xml添加 删除 修改数据 - badnewfish - 博客园.htm【转】c# xml添加 删除 修改数据 -...
javascript小技巧&&JavaScript[对象_属性]集锦 [转载了多篇] - 天轰穿_net-vs2005-ajax入门 - 博客园
C_C++代码静态检查工具PC-lint在VS2008开发环境中的安装配置和使用 - 段传涛 - 博客园
签名下载ELF检测
开源的搜索引擎工具包和web搜索引擎系统 - austin lius fashion - 博客园.docx开源的搜索引擎工具包和web搜索引擎系统 - austin lius fashion - 博客园.docx开源的搜索引擎工具包和web搜索引擎系统 - austin lius ...
开源的搜索引擎工具包和web搜索引擎系统 - austin lius fashion - 博客园.pdf开源的搜索引擎工具包和web搜索引擎系统 - austin lius fashion - 博客园.pdf开源的搜索引擎工具包和web搜索引擎系统 - austin lius ...
C#_基类库大全2012-12-09 注苏飞的代码库 转载 博客园牛人 苏飞.
关于Mantle使用个人的一些见解 - 陈taotao - 博客园.pdf
Entity Framework 6 Recipes 中文翻译系列-By博客园-china_fucan
Linux静态库和动态库 - feisky - 博客园.mht
转ibatis动态sql - phoebus0501 - 博客园.mht
Mysql主从同步(1) - 概念和原理介绍 以及 主从_主主模式 部署记录 - 散尽浮华 - 博客园1