惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
Recorded Future
Recorded Future
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
Y
Y Combinator Blog
N
News | PayPal Newsroom
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Help Net Security
Help Net Security
博客园 - Franky
SecWiki News
SecWiki News
Recent Announcements
Recent Announcements
T
Troy Hunt's Blog
The Register - Security
The Register - Security
The Last Watchdog
The Last Watchdog
Webroot Blog
Webroot Blog
S
Security Affairs
博客园 - 司徒正美
S
Schneier on Security
I
InfoQ
博客园_首页
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Threat Research - Cisco Blogs
Forbes - Security
Forbes - Security
腾讯CDC
N
Netflix TechBlog - Medium
N
News and Events Feed by Topic
Cloudbric
Cloudbric
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta
Recent Commits to openclaw:main
Recent Commits to openclaw:main
B
Blog
V
Vulnerabilities – Threatpost
C
Check Point Blog
Google DeepMind News
Google DeepMind News
Google Online Security Blog
Google Online Security Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cisco Blogs
Schneier on Security
Schneier on Security
O
OpenAI News
K
Kaspersky official blog

博客园 - jerreychen

常用的JavaScript验证正则表达式 用CSS设置打印换页 使用Form认证实现用户登录 (LoginView的使用) C#.Net中WinForm采用Active Directory进行身份认证 Windows XP 如何设置系统自动关机任务 - jerreychen DataList嵌套GridView实现文章分类列表显示(2) visual studio 2008 sp1中如何让WebBrowser控件可编辑 - jerreychen SQLite 数据库初学 学习笔记 - jerreychen - 博客园 如何将.xsd文件自动生成对象 ASP.NET页面事件:顺序与回传详解 读取自定义的config文件 - jerreychen - 博客园 Umbraco 设置Document Types 的默认值 格式化数据 Net支持gzip 压缩格式 压缩与解压 .net 序列化数据对象 .net 下,日期的格式化 太久太久没来了,久违了-博客园 showModelDialog 关闭后改变GridView某个单元格的值
向容器(PlaceHolder)中动态添加多个用户控件(UserControl)
jerreychen · 2009-03-17 · via 博客园 - jerreychen

问题是这样的:

我在页面里有一个PlaceHolder容器,然后有一个Button;

    <form id="form1" runat="server">
    
<div>
    
<asp:PlaceHolder ID="ucHolder" runat="server"></asp:PlaceHolder>
    
</div>
    
<div>
        
<asp:Button ID="btnAddUC" runat="server" Text="Add User Control" />
    
</div>
    
</form>

当我点击Button的时候,PlaceHolder中动态加载一个UserControl,每点击一次按钮,多加载一个UserControl,如果已经加载的UserControl有值,需要保留原来的值。

好了,下面开始具体的实现

首先,我新建一个Aspx的页面和一个Ascx的用户控件,然后在Aspx页面的Page_Load事件中为Button添加Click事件;

Code

还需要定义一个变量变量 Count,保存到 ViewState 中,记录 UserControl 的个数

Code

按钮点击方法如下

Code

加载用户控件的方法

Code

这里注意,因为Asp.Net 的页面有一个Element Tree结构,会保存控件的状态的,所以加载的UserControl的ID要固定,就向上面 ctl.ID = string.Format("userControl_{0}", index); 每次加载控件的ID 都固定的

然后由于Asp.Net保存控件状态用的是ViewState,所以我们加载控件的实现需要在ViewState之后,我们放在页面的Page_Load事件中

Code

OK,这样就完成了

 

附:源代码