.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
OpenSource:DynamicControlsPlaceholder

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

source:

http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

begin:

Problem:

ASP.NET gives a developer the opportunity to programmatically add controls to a web form using ParentControl.Controls.Add(new Control());
However, these controls are not persisted in any way thus having to be recreated for each subsequent request.


 

Goal:

To create a control that behaves like a placeholder but additionally handles recreating dynamic controls on subsequent requests.


 

Procedure:

I have created a custom control called DynamicControlsPlaceholder that derives from Placeholder and overrides Load- and SaveViewState.
In SaveViewState, the control hierarchy is recursively traversed and the control type and ID persisted to a string
In LoadViewState the persisted information is used to recreate the control tree to the state before.


 

Download (V1.1):


 

History:

V1.1.0.0 (published 2003-09-24)

- Bugfix: excluded child control persistance for CheckBoxList (ListItems are automatically persisted in statebag)

History:

V1.0.2.0 (published 2003-05-29)

- Bugfix: problem with UserControls whose filename includes "_"

History:

V1.0.1.0 (published 2003-02-02)

- added events: ControlRestored, PreRestore, PostRestore
- added property: ControlsWithoutIDs (specifies how to handle controls without ID)

V1.0.0.0 (published 2003-01-11)

Initial version


 

Known Problems:

1. Property restoration

Please note that properties, that are set before TrackViewState is called, are lost. As an example, imagine you create a Listbox and add Listitems before actually adding it to the control tree. In the following roundtrip, the Listitems are not recreated from ViewState as they are not stored in it.

Example:

		DropDownList ddl = new DropDownList();
		ddl.ID = "DDL1";
		ddl.Items.Add("Test1");
		ddl.Items.Add("Test2");
		dph1.Controls.Add(ddl);
			

will result in an empty Listbox after a roundtrip, whereas

		DropDownList ddl = new DropDownList();
		ddl.ID = "DDL1";
		dph1.Controls.Add(ddl);
		ddl.Items.Add("Test1");
		ddl.Items.Add("Test2");
			

will work, as TrackViewState is called in Controls.Add and changes to properties are persisted from then on.

2. Delegate restoration

Please note that delegates are currently not persisted. This means that when you are connecting an event handler with a control's event, the event handler may not be called after a postback. Example:

		TextBox tb = new TextBox();
		ddl.ID = "TB1";
		
		//this is not automatically restored
		tb.TextChanged += new EventHandler(tb_OnTextChanged);
		dph1.Controls.Add(tb);
			

Workaround:
DynamicControlsPlaceholder exposes two events that can be used to manually reattach delegates.

		private void DCP_ControlRestored(object sender, DBauer.Web.UI.WebControls.DynamicControlEventArgs e)
		{
			if(e.DynamicControl.ID == "TextBox1")
				((TextBox) e.DynamicControl).TextChanged += new EventHandler(tb_OnTextChanged);
		}
			

The above example shows how to use the ControlRestored event that is raised for each control on the Placeholder. Another option would be to use the PostRestore event that is raised after all controls on the Placeholder have been restored

		private void DCP_PostRestore(object sender, System.EventArgs e)
		{
			TextBox tb = (TextBox) DCP.FindControl("TextBox1");
			tb.TextChanged += new EventHandler(tb_OnTextChanged);
		}
			




 

Feedback:

If you have any comments, suggestions or questions to one of the samples, feel free to drop me a line or post your question on the ASP.NET Forums if you want to share your thoughts with other ASP.NET experts.




相关文章

相关软件