Custom WebForm inheritance issue (C#)
-
I have created a custom webform class the implements some commonly used functionality that I use on most of my web pages. This webform class (SmartWebForm) is based on the System.Web.UI.Page class. Mainly it registers client script blocks and hidden fields based on properties set within the web form that inherits from it. The issue that I am experiencing pertains to the handling of the hidden fields. Within the base class I have implemented a ViewState/HiddenField combo for persisting client-side changes to values. Here is an example: Custom Property within SmartWebForm.cs Base Class
public bool Locked { get { object savedState; object clientState; try { if (Request.Params["__SmartWebFormLocked"].ToString().Trim() != "") { clientState = Request.Params["__SmartWebFormLocked"].ToString().Trim(); this.ViewState["__SmartWebFormLocked"] = clientState.ToString(); } } catch{} savedState = this.ViewState["__SmartWebFormLocked"]; if (savedState != null) return bool.Parse(savedState.ToString()); else return false; } set{this.ViewState["__SmartWebFormLocked"] = value;} }
OnPreRender Segment within SmartWebForm.cs base classprotected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (!Page.IsClientScriptBlockRegistered["SmartWebFormLockHandler"]) { .... CODE TO BUILD JAVASCRIPT .... Page.RegisterClientScriptBlock("SmartWebFormLockHandler",script.ToString()); } // Register Hidden Field to be Used by Client Javascript to control locking // mechanism from the client-side. Page.RegisterHiddenField("__SmartWebFormLocked",this.Locked.ToString()); }
Here is where the issue occurs. I create a new WebForm, Page1.aspx, that inherits from by SmartWebForm Class. The [Locked] custom property defaults to "False" and is registered in the rendered page as:
Within the code-behind for Page1.aspx, I add a server-side event to switch the [Locked] property from "False" to "True". This is done by setting the inherited base class property.private void SomeEvent() { this.Locked = true; }
When the page is rendered however, the value recorded for the hidden field is stil -
I have created a custom webform class the implements some commonly used functionality that I use on most of my web pages. This webform class (SmartWebForm) is based on the System.Web.UI.Page class. Mainly it registers client script blocks and hidden fields based on properties set within the web form that inherits from it. The issue that I am experiencing pertains to the handling of the hidden fields. Within the base class I have implemented a ViewState/HiddenField combo for persisting client-side changes to values. Here is an example: Custom Property within SmartWebForm.cs Base Class
public bool Locked { get { object savedState; object clientState; try { if (Request.Params["__SmartWebFormLocked"].ToString().Trim() != "") { clientState = Request.Params["__SmartWebFormLocked"].ToString().Trim(); this.ViewState["__SmartWebFormLocked"] = clientState.ToString(); } } catch{} savedState = this.ViewState["__SmartWebFormLocked"]; if (savedState != null) return bool.Parse(savedState.ToString()); else return false; } set{this.ViewState["__SmartWebFormLocked"] = value;} }
OnPreRender Segment within SmartWebForm.cs base classprotected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (!Page.IsClientScriptBlockRegistered["SmartWebFormLockHandler"]) { .... CODE TO BUILD JAVASCRIPT .... Page.RegisterClientScriptBlock("SmartWebFormLockHandler",script.ToString()); } // Register Hidden Field to be Used by Client Javascript to control locking // mechanism from the client-side. Page.RegisterHiddenField("__SmartWebFormLocked",this.Locked.ToString()); }
Here is where the issue occurs. I create a new WebForm, Page1.aspx, that inherits from by SmartWebForm Class. The [Locked] custom property defaults to "False" and is registered in the rendered page as:
Within the code-behind for Page1.aspx, I add a server-side event to switch the [Locked] property from "False" to "True". This is done by setting the inherited base class property.private void SomeEvent() { this.Locked = true; }
When the page is rendered however, the value recorded for the hidden field is stilI solved the problem myself but thought I would share the solution with the rest of the community. To restate the problem: The custom webform class that I created needed to accept property value changes for client-side manipulated hidden values, as well as server-side events. The first version of the code worked fine for client-side and onLoad events, but not for PostBack events. Original Code
public bool Locked { get { object savedState; object clientState; try { if (Request.Params["__SmartWebFormLocked"].ToString().Trim() != "") { clientState = Request.Params["__SmartWebFormLocked"].ToString().Trim(); this.ViewState["__SmartWebFormLocked"] = clientState.ToString(); } } catch{} savedState = this.ViewState["__SmartWebFormLocked"]; if (savedState != null) return bool.Parse(savedState.ToString()); else return false; } set{this.ViewState["__SmartWebFormLocked"] = value;} }
Modified Codeprivate string _Locked = ""; public bool Locked { get { if (_Locked != "") this.ViewState["__SmartWebFormLocked"] = bool.Parse(_Locked.ToString()); else { object savedState; object clientState; try { if (Request.Params["__SmartWebFormLocked"].ToString().Trim() != "") { clientState = Request.Params["__SmartWebFormLocked"].ToString().Trim(); this.ViewState["__SmartWebFormLocked"] = bool.Parse(clientState.ToString()); } } catch{} savedState = this.ViewState["__SmartWebFormLocked"]; if (savedState == null) this.ViewState["__SmartWebFormLocked"] = false; } return (bool)this.ViewState["__SmartWebFormLocked"]; } set { _Locked = value.ToString(); this.ViewState["__SmartWebFormLocked"] = value; } }
With the updated code, server-side property value changes take priority of the stored or client-side property value. If no server-side event 'sets' the private string associated with the public property, then the get event returns either the client-side value from postback events or the viewstate value assigned to the property. Hope this helps someone