Diff between LoadViewState and TrackViewState Event in ASP.Net Page Life-cycle - c#

Exactly what happens in TrackViewState Page Event Method in ASP.NET. How it differs from LoadViewState.
Thanks,
Pravin

The ViewState property is of type System.Web.UI.StateBag. The StateBag class provides a means to store name and value pairs, using a System.Collections.Specialized.HybridDictionary behind the scenes.
The reason the StateBag has the TrackViewState() method is to keep the view state as trimmed down as possible. Again, we don't want to store the initial property values in the view state, as they don't need to be persisted across postbacks. Therefore, the TrackViewState() method allows the state management to begin after the instantiation and initialization stages.
for more info follow the subtittle
Timing the Tracking of View State in this link

Related

Using EF Object as member of Master Page within Pages

If I've got a setup where my master page has a member which is an EF generated object, and I want to use this member then within the page itself (but have need for it within the master page as well).
Currently I've got a using loop within the Master Page Page_Init event to establish the member. However if then I try to get any properties of this within the Page I get an error about "The ObjectContext instance has been disposed...", which is fair enough (It is within it's own using loop, however that's a different OC instance, so it's throwing this error.)
What's the best/preferred way around this situation? Is it to open the Object Context within the MasterPage pre_init event, and then dispose of it during the Page_Dispose event of the Master Page, or use a second OC Instance in the Page, and just pull a local version of the EF object by checking it's ID against the Master Page's object ID?
Thanks, Psy
Can't you create a POCO when you load the master page and make it a protected property which your derived pages can get access to. Results in 1 trip to the database.
It's best practice not to use the Object Context in your UI layer to begin with. You should a business/data access layer to broker the communication with the database.

MultiViewControl issues

I'm presently in the process of reworking a MultiViewControl based wizard process for our web application. I am having an rough time trying to make sense of the order that events are happening (Page_Load, Init, prerender, etc). Does anyone out there on the interwebs have details on dealing with one of these controls? Please don't just say 'google' it. I've done that and have yet to find a good, comprehensive site yet.
Admittedly, I haven't really elaborated on the problems I'm having with this control, so I'll try to do that:
Primary problem is the initialization of UserControls that live in different Views. In the existing codebase, the programmer was using a combination of multiviewcontrol.ActiveViewIndex = WHATEVER and Response.Redirect("PageWithMultiView.aspx?nextstep") and it made it all very convoluted. My task is to attempt to remove the Response.Redirect calls and use only the setting of the ActiveViewIndex. Is this even possible? Also, there are some cases where I need to initialize a control in a particular view only on the initial load and not on subsequent postbacks. I can use something like the IsPostBack flag but this is only ever set to false on the initial load. Subsequent reloads IsPostBack == true. I basically want to have IsPostBack set to false for the initial load of each View. Can this be done without doing a Response.Redirect to itself?
Hopefully this will make some sense to someone out there.
Thanks.
Greg.
I am having an rough time trying to
make sense of the order that events
are happening (Page_Load, Init,
prerender, etc).
Here you have all details about ASP.NET page lifecycle and events: http://msdn.microsoft.com/en-us/library/ms178472.aspx .
In terms of MultiView - you should NEVER use Response.Redirect when you work with MultiView.
If user can not switch to previous view then you can check previous ActiveViewIndex value before setting it to the new value, e.g.
if (mv.ActiveViewIndex != newIndex)
{
// this view is displayed for the first time
}
If user can switch to the previous views, I suggest to place an information about already used views in session or by placing hidden field on the form with ids of the views that have already been displayed and to use that information instead of IsPostBack.

asp.net - variable pass between ascx and page

Have any ways to keep a variables (object) in a page scope? I need pass some object from master page to all ascx files. Normally, i passing by using a method or structure with parameters. But now im asking another way to do this. Store it in session is a bad idea because my object should exists in a page scope only.
One way is to set up properties in your user controls. You have access to read and set these from all pages that implement them.
Another alternative is to store the shared object(s) in the HttpContext.Items collection.
You could expose your variables a public properties of the master page:
public string MyVariable { get; set; }
then access them from the user controls by referencing the master page, and casting to its specific type:
((MyMasterPageType)Page.Master).MyVariable
So you have masterpage, user controls and the page itself.
The page is the container for the masterpage and the user controls, hence it's the only one that 'knows' the 2 parties. So I'll suggest (if you haven't) you have the page instance to facilitate the object/variable passing amongst them.
If your variable's value is going to be changed on per page basis then i would recommend you to write that code in base page (or user control) and inherit all the page (or usercontrol),if the values are going to be similar for all pages(or user control) you can use cache object as well.
In a more better approach if you feel you can even create one helper class and call it from your base page (or user control), so you can separate variable assignment code from your page.

ASP.NET catch the submit action

I'm a super beginner ASP.NET developer. I read that on every submit the request parameters are being populated to the controls and instead of reading the "Response.Form[]" parameters I can read the input parameters from the control itself.
Are there any events that I can catch all the submits before and after the magic happens?
Which method on the server side is activated that perform this magic?
Can I override it (for fun)?
Thanks,
Ronny
I believe that you are talking about the function of Viewstate and how control values are persisted.
This is a diagram that will show you the page load order for ASP.NET
For you, if you want to look before viewstate is loaded, you can work inside the Page_Init method.
see few tutorials
http://aspalliance.com/quickstart/aspplus/
http://quickstart.developerfusion.co.uk/QuickStart/
In asp.net all event, result in post-back. So, you can handle them in Page_Load, but it's classic way. For fun you can try it.
ASP.NET maps HTML input field values from the Request.Form collection to server control properties, such as TextBox.Text between the Page.InitComplete and Page.PreLoad events, as detailed in the ever-linkable ASP.NET Page Life Cycle Overview.
The actual mapping takes place in the non-virtual private method Page.ProcessPostData, so there's no real hook for modifying that process. (You can see this by downloading Reflector and reviewing the Page.ProcessRequestMain method.)
If you want to perform custom processing before or after the mapping, you can add a handlers to the appropriate events or override the associated virtual methods (Page.OnInitComplete and Page.OnPreLoad).

Is there way to be notified when a session variable changes?

Is it possible to be notified in code when a session variable changes? Will give an example to make it clearer.
Its a normal ASP.NET site, with a master page and content pages. I want to show an image for the state of a user, ie logged in or not(2 different images). Instead of checking a session variable on every page_load of the master page, is it possible to set the image and only change it when that session variable is changed? Almost like firing a trigger when a change occurs.
The Session Object (HTTPSessionState) doesn't have any OnChanged events as far as I know so you'll have to check each time, however the overhead of doing a check in the MasterPage Page_Load event is miniscule.
You should look at using the built in Membership Provider for doing this though as it has special Login/LoginStatus controls which will change state (you supply the template with your own images/styles etc...) when a user logs in/logs out/is anonymous

Categories

Resources