I've created a custom webcontrol that inherits from LinkButton. I needed it to perisist its properties between PostBacks so I implemented ControlState with it.
Problem I'm having is that it is VERY persistent and isn't letting me change anything during a PostBack in the codebehind.
These LinkButtons have some custom properties that are set in an XSLT template during an XSLTransform. They are created dynamically and my codebehind needs to read the properties of the clicked LinkButton in Page_Init and Page_Load after every PostBack.
Once I've read the properties I am ready to remove those dynamically created controls and create new ones with new links, new images and new properties.
I've tried doing a Controls.Clear on the PlaceHolder the dynamic controls were created in, that doesn't work - ControlState still knows their properties and somehow ControlState applies those properties to the new controls that are created. I've also tried
using PlaceHolder.Remove("ControlsContainer"), but that has no effect either. Was just hoping that by removing the controls that ControlState would be aware and also remove the properties pertaining to those controls. Apparently that doesn't work.
Can anyone tell me what's involved in removing ControlState data for dynamically created controls that I'm done with in the codebehind?
Or is there a custom method I can add to my webcontrol to cause itself to be removed from ControlState?
rwkiii
Member
55 Points
43 Posts
Using ControlState with a Custom WebControl
Mar 21, 2012 03:40 AM|LINK
I've created a custom webcontrol that inherits from LinkButton. I needed it to perisist its properties between PostBacks so I implemented ControlState with it.
Problem I'm having is that it is VERY persistent and isn't letting me change anything during a PostBack in the codebehind.
These LinkButtons have some custom properties that are set in an XSLT template during an XSLTransform. They are created dynamically and my codebehind needs to read the properties of the clicked LinkButton in Page_Init and Page_Load after every PostBack. Once I've read the properties I am ready to remove those dynamically created controls and create new ones with new links, new images and new properties.
I've tried doing a Controls.Clear on the PlaceHolder the dynamic controls were created in, that doesn't work - ControlState still knows their properties and somehow ControlState applies those properties to the new controls that are created. I've also tried using PlaceHolder.Remove("ControlsContainer"), but that has no effect either. Was just hoping that by removing the controls that ControlState would be aware and also remove the properties pertaining to those controls. Apparently that doesn't work.
Can anyone tell me what's involved in removing ControlState data for dynamically created controls that I'm done with in the codebehind?
Or is there a custom method I can add to my webcontrol to cause itself to be removed from ControlState?
Thx.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Using ControlState with a Custom WebControl
Mar 23, 2012 01:38 AM|LINK
Hello:)
As far as I see,you should do changing to the ControlState when overriding the SaveControl State,and then save the modified controlstate into controlstate:http://www.bipinjoshi.net/articles/3380d5ce-b84e-443f-8c26-e97ec071292d.aspx
namespace BinaryIntellect.UI { public class HyperLinkGroupWithControlState : Control { private string strSourceFile; public string SourceFile { get { return strSourceFile; } set { strSourceFile = value; } } private HyperLinkGroupDirection enumDir; public HyperLinkGroupDirection Direction { get { return enumDir; } set { enumDir = value; } } protected override void OnInit(EventArgs e) { base.OnInit(e); Page.RegisterRequiresControlState(this); } protected override object SaveControlState() { object state= base.SaveControlState(); //In SaveControlState,you can do changing…… p.First = xxx; //Do changing…… Pair p = new Pair(state, strSourceFile); return p; } protected override void LoadControlState(object savedState) { Pair p = (Pair)savedState; if (p != null) { base.LoadControlState(p.First); strSourceFile = (string)p.Second; } } protected override void Render(HtmlTextWriter writer) { DataSet ds = new DataSet(); ds.ReadXml(HttpContext.Current.Server.MapPath(SourceFile)); if (enumDir == HyperLinkGroupDirection.Horizontal) { writer.WriteFullBeginTag("table"); writer.WriteFullBeginTag("tr"); foreach (DataRow row in ds.Tables[0].Rows) { writer.WriteFullBeginTag("td"); writer.WriteBeginTag("a"); writer.WriteAttribute("href", row["url"].ToString()); writer.Write(">"); writer.Write(row["title"].ToString()); writer.WriteEndTag("a"); writer.WriteEndTag("td"); } writer.WriteEndTag("tr"); writer.WriteEndTag("table"); } else { writer.WriteFullBeginTag("table"); foreach (DataRow row in ds.Tables[0].Rows) { writer.WriteFullBeginTag("tr"); writer.WriteFullBeginTag("td"); writer.WriteBeginTag("a"); writer.WriteAttribute("href", row["url"].ToString()); writer.Write(">"); writer.Write(row["title"].ToString()); writer.WriteEndTag("a"); writer.WriteEndTag("td"); writer.WriteEndTag("tr"); } writer.WriteEndTag("table"); } } } }