Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Custom WebForm inheritance issue (C#)

Custom WebForm inheritance issue (C#)

Scheduled Pinned Locked Moved ASP.NET
csharpjavascriptdesignsysadmintools
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ray Williams II
    wrote on last edited by
    #1

    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 Classpublic 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

    R 1 Reply Last reply
    0
    • R Ray Williams II

      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 Classpublic 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

      R Offline
      R Offline
      Ray Williams II
      wrote on last edited by
      #2

      I 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 Codepublic 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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups