User Control's Viewstate - asp.net

In some book I've seen that they save custom properties of user control like this:
private int id = 0;
public int ID
{
get { return id; }
set { id = value; }
}
protected void Page_Init(object sender, EventArgs e)
{
this.Page.RegisterRequiresControlState(this);
}
protected override void LoadControlState(object savedState)
{
object[] ctlState = (object[])savedState;
base.LoadControlState(ctlState[0]);
this.ID = (int)ctlState[1];
}
protected override object SaveControlState()
{
object[] ctlState = new object[2];
ctlState[0] = base.SaveControlState();
ctlState[1] = this.ID;
return ctlState;
}
My question is why can I simply store it (in setter) in viewstate like: Vistate["ID"]=id;
and then retrieve it form there?

There is a difference between ViewState (what you are talking about in your question) and ControlState (what is shown in the sample code):
ViewState can be turned off by the user of your UserControl, by setting EnableViewState="false". In that case, you wouldn't be able to restore your property's value during the next request/postback (because there is no ViewState).
ControlState cannot be turned off. This means, that whatever you store in ControlState will be available during the next postback and you should therefore use ControlState for data that you absolutely need to be able to retrieve during the next request/postback.
See also these pages in MSDN: ASP.NET ViewState Overview and ControlState vs. ViewState
Excerpt from the first page:
In addition to view state, ASP.NET
supports control state. The page uses
control state to persist control
information that must be retained
between postbacks, even if view state
is disabled for the page or for a
control. Like view state, control
state is stored in one or more hidden
fields.

Related

Is ViewState totally incompatible with Fragment OutputCache?

I have a custom control in a page which uses full and partial output caching. If I simply refresh the page, the control is loaded from cache (I've added a timestamp to verify that). But if I do an action in the control that causes PostBack, the second time I do it the ViewState "loses" a value that was added in Page_Load when !IsPostBack. I wonder if there is some way to have both ViewState and OutputCache working in this way.
public partial class MyUserControl : MyBaseUserControl
{
private string MyProperty
{
get { return (string)ViewState["_myProperty"]; }
set { ViewState["_myProperty"] = value; }
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
var prop = this.MyProperty;
}
protected void Page_Load(object sender, EventArgs e)
{
// Do something
if (IsPostBack)
{
var value = this.MyProperty; // sometimes gets null
}
else
{
this.MyProperty = GetNewValue(); // never returns empty or null
}
}
}
Edit: After posting the question, SO showed other related questions (like this), so I've reviewed Page Life Cycle and overriden LoadViewState method, and set breakpoints. When page (and control) is first loaded, Page_Load is called, but LoadViewState not; in first PostBack, LoadViewState is called and MyProperty has the correct content; in second PostBack, again Page_Load is called, but LoadViewState not, and MyProperty has null value.
Edit 2: I've debugged a little more (by comparing execution with Output Cache disabled) and found out that control postbacks (an asp:GridView's OnSelectedIndexChanging event) force the page to refresh by injecting a window.location=window.location; JavaScript in the page. But with cache enabled, gridview's event is executed only in the first postback, but not in the second one, preventing the page (and, subsequently, the control) to be reloaded to complete its task. But it's not the only problem, because of the property stored in ViewState.

How to implement observer pattern to work with user controls in asp.net

I've 2 user controls named UCCreateProfile.ascx (used for creating/editing profile data) and UCProfileList.ascx (used to display profile data in GridView). Now whenever a new profile created I want to update my UCProfileList control to show new entry.
The best solution against above problem I've to go for Observer Pattern. In my case UCCreatedProfile is a Subject/Observable and UCProfileList is a Observer and as per pattern definition when observer initialized it knows who is my Subject/Observable and add itself into Subject/Observable list. So whenever a change occurred in Subject/Observable it will be notified.
This pattern best fit my requirements but I'm getting few problems to implement this describe as follows.
I'm working under CMS (Umbraco) and I don't have any physical container page (.aspx). What I've to do is find UCCreateProfile (Subject/Observable) in UCProfileList (Observer) onLoad event using following code.
private Control FindCreateProfileControl()
{
Control control = null;
Control frm = GetFormInstance();
control = GetControlRecursive(frm.Controls);
return control;
}
where GetFormInstance() method is
private Control GetFormInstance()
{
Control ctrl = this.Parent;
while (true)
{
ctrl = ctrl.Parent;
if (ctrl is HtmlForm)
{
break;
}
}
return ctrl;
}
and GetControlRecursive() method is
private Control GetControlRecursive(ControlCollection ctrls)
{
Control result = null;
foreach (Control item in ctrls)
{
if (result != null) break;
if (item is UCCreateProfile)
{
result = item;
return result;
}
if (item.Controls != null)
result = GetControlRecursive(item.Controls);
}
return result;
}
this way I can find the UCCreateProfile (Subject/Observable) user control in UCProfileList (Observer) but the way to find out the (Subject/Observable) is not so fast. As you can see I need to loop through all controls and first find the HtmlForm control and then loop through all child controls under HtmlForm control and find the appropriate control we're looking for.
Secondly, placement of the user controls in container if very important my code will only work if UCCreatedProfile.ascx (Subject/Observable) placed before UCProfileList.ascx (Observer) because this way UCCreateProfile will load first and find in UCProfileList. But if someone changed the position of these 2 controls my code will not work.
So to get rid of these problems I need some solution which works faster and independent of the position of the controls.
I've figured out some solution as described below. Please do let me know if it is a good way of doing this. If there is an alternative, please let me know.
I've a session level variable (a dictionary with Dictionary<ISubject, List<Observer>>) . No matter which user control initialized/loaded first, User Control will add itself into this dictionary.
If Subject/Observable added first, the corresponding observers will be found in this dictionary.
If Observer added first it will added to the dictionary with a null entry. When the Subject added, the association is made.
Regards,
/Rizwan
The Observer pattern is best implemented in .NET via events and delegates. If you use events and delegates, the Dictionary you mention becomes completely unnecessary. See for example this code below (only important pieces shown):
public partial class UserProfile : System.Web.UI.UserControl
{
//This is the event handler for when a user is updated on the UserProfile Control
public event EventHandler<UserUpdatedEventArgs> UserUpdated;
protected void btnUpdate_Click(object sender, EventArgs e)
{
//Do whatever you need above and then see who's subscribed to this event
var userUpdated = UserUpdated;
if (userUpdated != null)
{
//Initialize UserUpdatedEventArgs as you want. You can, for example,
//pass a "User" object if you have one
userUpdated(this,new UserUpdatedEventArgs({....}));
}
}
}
public class UserUpdatedEventArgs : EventArgs
{
public User UserUpdated {get;set;}
public UserUpdatedEventArgs (User u)
{
UserUpdated=u;
}
}
Now subscribing to the UserUpdated event from the UserProfile control on the UserListControl is as easy as this:
public partial class UserList : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
//Find the UserProfile control in the page. It seems that you already have a
//recursive function that finds it. I wouldn't do that but that's for another topic...
UserProfile up = this.Parent.FindControl("UserProfile1") as UserProfile;
if(up!=null)
//Register for the event
up.UserUpdated += new EventHandler<UserUpdatedEventArgs>(up_UserUpdated);
}
//This will be called automatically every time a user is updated on the UserProfile control
protected void up_UserUpdated(object sender, UserUpdatedEventArgs e)
{
User u = e.UserUpdated;
//Do something with u...
}
}

How to pass multiple parameters to a crystal report?

I have a crystal report in my asp.net web application which has a lot of report parameters (about 15). Previously I was using querystring to pass them but it was being unsecure.
Now I am mapping all parameters to a Hashtable, storing them in session & passing to the report viewer. Now If user opens multiple instance of reports in different browser tabs, the session values get messed up. When I navigate pages, wrong reports are displayed.
Please advise me a good method to pass my parameters to report.
Damien.
How about creating a simple DTO for storing all report parameters. Save this DTO in session and access in report viewer page where your viewer control resides.
To cross over the session issue when user opens multiple instances in browser, you can do a simple trick. When user provides parameter, and clicks on "Show Report", at that point create a guid, store value inside the session using key as this guid, and pass this guid as query string parameter to your report viewer page. This way each instance of report viewer page is aware which session value to be brought out.
Something like this
public class AttendanceDTO
{
public int EmployeeId {get;set;}
public string Month {get;set;}
}
And then in your "Report parameter page"
/* JUST NOTEPAD CODE, SYNTAX MIGHT VARY */
protected override ShowReport_Click(object sender, EventArgs e)
{
string guid = new Guid();
AttendanceDTO dto = new AttendanceDTO()
{ EmployeeId = txtEmployee.Text;
Month = txtMonth.text
};
session[guid] = dto;
Response.Redirect("ReportViewer.aspx?Guid=" + guid);
}
And then inside your Report Viewer Page
string guid = Request.QueryString["Guid"]; //Null check etc..
AttendanceDTO dto = (AttendanceDTO) session[guid];
//Provide dto values as parameters to your report viewer control and then clean the session
[You would need to make sure that you clear out the respective session value after you get it used.]
[Above code is just from notepad to give you an idea. On top of this you can bring more innovation]
Session Objects are shared among multiple tabs of same browser since server stores only one session cookie for a browser. So it is pretty obvious for the values to messup in several tabs on single browser.
One of the possible solution:
Use ViewStates instead of session to store HashTable.
Refer the code below for a demo, I have used ArrayList instead is HashTable for the sake of simplicity.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["ht"] = new ArrayList();
//initialize viewstate with arraylist on first pageload.
}
}
protected void btnAddElement_Click(object sender, EventArgs e)
{//adding new elements to the arraylist
if (ViewState["ht"] != null)
{
ArrayList ht = (ArrayList)ViewState["ht"];
ht.Add(TextBox1.Text);
ViewState["ht"] = ht;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if (ViewState["ht"] != null)
{
ArrayList ht=(ArrayList)ViewState["ht"];
foreach (object a in ht)
{//write code to pass parameters to the crystal report
Response.Write(a);
}
}
}
}
Since Viewstates work at page level, therefor the values in the collection will not mess up this time.
Hope this helps.

Get state of ASP.NET page life cycle

I need the following functionality in my method: if the method is called before OnLoad event of ASP.NET life cycle throw an exception else continue execution of the method.
I was thinking of something like this:
if (Page.LifeCycleState < LifeCycleState.OnLoad) {
throw new InvalidPageStateException();
}
Is it possible to retrieve the state of ASP.NET page life cycle?
One approach would be to use a Basepage that you always use in your site. This would contain a variable called PageLoadComplete, which you would set at the end of your PageLoad event. Then you could check the state of this variable from within your method.
public abstract class BasePage : System.Web.UI.Page
{
public bool PageLoadComplete { get; private set; }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
PageLoadComplete = true;
}
}
If you want to access the variable from code external to your page such as a UserControl, you would have to make it public and cast your page as BasePage.
public partial class MyUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
BasePage basePage = this.Page as BasePage;
if (basePage != null && !basePage.PageLoadComplete)
{
throw new InvalidPageStateException();
}
}
}
There is property in a realization of System.Web.UI.Control class(realization):
internal ControlState ControlState {
get { return _controlState; }
set { _controlState = value; }
}
Where ControlState is enum that contains members such as: Initialized, ViewStateLoaded, Loaded etc. here declaration
But as you can see this property is internal. So only way to get control state is proposed by Daniel Dyson.
You maybe able to find what you are looking for, by looking at the CurrentHandler and PreviousHandler properties of the current HttpContext.
if the method is called before OnLoad event of ASP.NET life cycle
throw an exception else continue execution of the method.
It is not clear which Onload event is meant, nor where the "method" resides. Is it the Page's Onload or a Control's OnLoad? Is it a Page's "method" or a Control's "method"?
Anyway, one can store sort of flag in the Context.Items Dictionary, which all controls (including Page) have access to during a request. This eliminates the need to use a general base page like suggested obove.
In the OnLoad method (no matter whether it is a Page's OnLoad or a Control's OnLoad):
Context.Items[UniqueID] = this;
In the "method":
if (Context.Items[UniqueID] != null)
{
throw new InvalidPageStateException();
}

User control event or method override where custom properties are valid?

I have an ASP.NET user control that is used in another use control. The parent user control uses data-binding to bind to a custom property of the child user control.
What method can I override or page event where I am ensured that the property state is set?
I think in a page it is PageLoaded versus the Page_Load override? I am looking for this in the user control because my property is always null even though it is set.
Thanks.
Example. This is in my user control. FilterEntryId is being bound from inside another user control:
protected int _filterEntry = -1;
public int FilterEntryId
{
get
{
return _filterEntry;
}
set
{
_filterEntry = value;
}
}
protected void Page_Load(dobject sender, EventArgs e)
{
FilterEntry always -1!!
}
The property is being set but never has value when Page_Load. The Page_LoadComplete may be the proper place but does not seem to be an option in user control. I've also tried Page_DataBind.
My hypothesis is that this is a page lifecycle issue but it may be something else.
Not sure what you need to do with that property but since you can't be sure when will be set. Can't you add your logic on the set of the property?
Another option would be a later event as PreRender.
public int FilterEntryId
{
get
{
return _filterEntry;
}
set
{
_filterEntry = value;
//HERE YOUR LOGIC
}
}

Resources