OpenSource:DynamicControlsPlaceholder

类别:.NET开发 点击:0 评论:0 推荐:

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):

The sourcecode is available in C# as a VS.NET project.

A ready-to-use assembly (including help file).

A demo shows the usage of this control. Sourcecode for the Demo (C#) or Sourcecode for the Demo (VB)


 

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.

本文地址:http://com.8s8s.com/it/it42181.htm