Here’s a basic question for control development. If I have a control that has maybe a single POST value that it needs to retrieve, is it safe to use code like this:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//// *** Read our custom client managed Form Var that holds the tab selection
if (this.Page.IsPostBack)
{
string TabSelection = this.Context.Request.Form[HIDDEN_FORMVAR_PREFIX + this.ID];
if (TabSelection != "")
this._SelectedTab = TabSelection;
}
}
As opposed to implementing IPostbackDataHandler:
public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
if (postDataKey == HIDDEN_FORMVAR_PREFIX + this.ID )
this.SelectedTab = postCollection(HIDDEN_FORMVAR_PREFIX + this.ID)
}
public void RaisePostDataChangedEvent()
{
}
protected override void OnPreRender(EventArgs e)
{
// *** Must make sure we call for custom form var name
this.Page.RegisterRequiresPostBack(this);
base.OnPreRender(e);
}
There are definitely advantages to using IPostbackDataHandler which include being hooked into the pipeline in the right place after ViewState has loaded but before OnLoad() has fired which is necessary in many situations if ViewState is also used.
However, in this situation the control has a single value that’s stored in a hidden variable because the value is set from the client side. Basically it’s a client side based tab control and the variable holds the last Tab selection which is stored in the hidden variable, then posted back to the server and reassigned to the control on Postback automatically. In this case it’s a single value and it’s not going to be affected by ViewState or ControlState, so it seems kinda silly to implement this interface and override OnPreRender() just to retrieve a single value.
The question really is, whether it’s safe to access the Page object in OnInit(). In theory at least, the Page object gets initialized after all the controls are, so its OnInit fires after the controls. But from what I’ve seen so far, base functionality like IsPostBack and the Request object are all available so for basic operations its safe to access the Page object.
Any thoughts?
Other Posts you might also like