VB.NET - ViewState vs Session

Asked By gagan kopparam
14-Feb-08 03:28 PM

i see this using vs 2005, master page and its content page, the view state is not updated automatically as expected on post back.

at what stage in the life sycly can i access the controls on the master page from the child page

thanks

Both master pages and content pages can contain event handlers for controls.  Both master pages and content pages can contain event handlers for controls.

14-Feb-08 03:38 PM

 For controls, events are handled locally—a control in a content page raises an event in the content page, and a control in the master page raises an event in the master page. Controls events are not sent from the content page to the master page. Similarly, you cannot handle an event from a master page control in a content page.

In some cases, the same event is raised in both the content and the master page. For example, both pages raise Init and Load events. The general rule for how events are raised is that the initialization events are raised from the innermost control to the outermost one, and all other events are raised from the outermost control to the innermost one. It is helpful to remember that the master page is merged into the content page and treated as a control in the content page, which you can access via the Page's Master property.

The following is the order in which events occur when the master page is merged with a content page:

  1. Master page controls Init event.

  2. Content controls Init event.

  3. Master page Init event.

  4. Content page Init event.

  5. Content page Load event.

  6. Master page Load event.

  7. Content controls Load event.

  8. Content page PreRender event.

  9. Master page PreRender event.

  10. Master page controls PreRender event.

  11. Content controls PreRender event.

So, to answer your question, you can access the controls on the MasterPage from the child page safely once the MasterPage Load event has fired.

You can write code in content pages that references properties, methods, and controls in the master page, with some restrictions. The rule for properties and methods is that you can reference them if they are declared as public members of the master page. This includes public properties and public methods. You can reference controls on the master page independently of referencing public members.

To do this:

1. Add a Mastertype:

<%@ MasterType virtualpath="~/Masters/Master1.master" %>
2.Write code that uses the master page's public member as a member of the http://msdn2.microsoft.com/en-us/library/tcxdbe62.aspx property, as in this example, which assigns the value of a public property named CompanyName from the master page to a text box on the content page:
CompanyName.Text = Master.CompanyName;

ViewState vs Session  ViewState vs Session

01-Mar-08 11:25 AM
Understanding the http://www.csharper.net/blog/adding_controls_dynamically_to_a_page_in_asp_net.aspx was paramount to writing good, clean code - not to mention the steep decline in frustrating programming sessions. If you do not learn the page lifecycle, including what the events do, as well as the sequence in which they occur, you will indeed get mad and sad. Crying may occur.

In ASP.NET 1+, there were about 8 methods/events that were important to understand (about 12+ if you are a control developer). In ASP.NET 2.0, this has been increased significantly to give the developer more opportunities to inject logic at different points of the page's life.

As I attempted to locate a good source for the new page lifecycle, I found incomplete and beta lists, which were typically out of date. I've compiled a list of events/methods that occur during a typical page request with as much data as I could find. Most of the method descriptions are from MSDN2.

Method Postback Control
Constructor Always All

Construct Always Page

TestDeviceFilter
Page
Used to determine which device filter is in place, and use this information to decide how to display the page.

AddParsedSubObject Always All
Notifies the server control that an element, either XML or HTML, was parsed, and adds the element to the server control's ControlCollection object.

DeterminePostBackMode Always Page
Returns a NameValueCollection object that contains the data posted back to the page. The presence of the page hidden fields VIEWSTATE and EVENTTARGET is used to help determine whether a postback event has occurred. The IsPostBack property is set when the DeterminePostBackMode method is called.

OnPreInit Always Page
Called at the beginning of the page initialization stage. After the OnPreInit method is called, personalization information is loaded and the page theme, if any, is initialized. This is also the preferred stage to dynamically define a PageTheme or MasterPage for the Page.

OnInit Always All
Performs the initialization and setup steps required to create a Page instance. In this stage of the page's life cycle, declared server controls on the page are initialized to their default state; however, the view state of each control is not yet populated. A control on the page cannot access other server controls on the page during the Page_Init phase, regardless of whether the other controls are child or parent controls. Other server controls are not guaranteed to be created and ready for access.

OnInitComplete Always Page
Called after page initialization is complete. In this stage of the page's life cycle, all declared controls on the page are initialized, but the page's view state is not yet populated. You can access server controls, but they will not yet contain information returned from the user.

LoadPageStateFromPersistenceMedium Postback Page
Uses the Load method of the System.Web.UI.PageStatePersister object referenced by the PageStatePersister property to load any saved view-state information for the Page object.

LoadControlState Postback All
Restores control-state information from a previous page request that was saved by the SaveControlState method.

LoadViewState Postback All
Restores view-state information from a previous page request that was saved by the SaveViewState method.

OnPreLoad Always Page
Called after all postback data returned from the user is loaded. At this stage in the page's life cycle, view-state information and postback data for declared controls and controls created during the initialization stage are loaded into the page's controls. Controls created in the OnPreLoad method will also be loaded with view-state and postback data.

OnLoad Always All
Notifies the server control that it should perform actions common to each HTTP request for the page it is associated with, such as setting up a database query. At this stage in the page lifecycle, server controls in the hierarchy are created and initialized, view state is restored, and form controls reflect client-side data.

RaisePostBackEvent Postback All
Notifies the server control that caused the postback that it should handle an incoming postback event.

OnLoadComplete Always Page
At this point in the page life cycle, all postback data and view-state data is loaded into controls on the page.

OnPreRender Always All
Notifies the server control to perform any necessary prerendering steps prior to saving view state and rendering content.

OnPreRenderComplete Always Page
At this stage of the page life cycle, all controls are created and the page is ready to render the output. This is the last event called before the page's view state is saved.

SaveControlState Always All
Saves any server control state changes that have occurred since the time the page was posted back to the server. If there is no state associated with the control, this method returns a null reference. Custom controls using control state must call the RegisterRequiresControlState method on the Page before saving control state.

SaveViewState Always All
Saves any server control view-state changes that have occurred since the time the page was posted back to the server. If there is no view state associated with the control, this method returns a null reference.

SavePageStateToPersistenceMedium Always Page
Saves any view-state and control-state information for the page. The SavePageStateToPersistenceMedium method uses the Save method of the System.Web.UI.PageStatePersister object referenced by the PageStatePersister property to store view-state and control-state information for the page.

Render Always All
Initializes the HtmlTextWriter object and calls on the child controls of the Page to render. The Render method is responsible for creating the text and markup that is sent to the client browser. The default Render method calls RenderChildren to write the text and markup for the controls contained on the page.

OnUnload Always All
Used to do target-specific processing in the Unload stage of the control lifecycle. Typically, these are cleanup functions that precede disposition of the control.
Create New Account
help
IParserAccessor)(@_ _ctrl)); Line 290: Line 291: #line 8 "E: \ project \ station_info.aspx" Line 292: @_ _parser.AddParsedSubObject(new System.Web.UI.LiteralControl("Station Information")); Line 293: Line 294: #line default Line 295 IParserAccessor)(@_ _ctrl)); Line 316: Line 317: #line 7 "E: \ project \ station_info.aspx" Line 318: @_ _parser.AddParsedSubObject(@_ _ctrl1); Line 319: Line 320: #line default Line 321: #line hidden Line 322: return @_ _ctrl IParserAccessor)(@_ _ctrl)); Line 774: Line 775: #line 11 "E: \ project \ station_info.aspx" Line 776: @_ _parser.AddParsedSubObject(new System.Web.UI.LiteralControl(" \ r \ n <div> \ r \ n ")); Line 777: Line 778: #line line hidden Line 787: Line 788: #line 11 "E: \ project \ station_info.aspx" Line 789: @_ _parser.AddParsedSubObject(@_ _ctrl1); Line 790: Line 791: #line default Line 792: #line hidden Line 793: Line 794: #line 11 "E: \ project \ station_info.aspx" Line 795: @_ _parser.AddParsedSubObject(new System.Web.UI.LiteralControl(" \ r \ n <span lang = \ "en-us \ "> :")); Line 796: Line 797 line hidden Line 806: Line 807: #line 11 "E: \ project \ station_info.aspx" Line 808: @_ _parser.AddParsedSubObject(@_ _ctrl2); Line 809: Line 810: #line default Line 811: #line hidden Line 812: Line 813: #line 11 "E: \ project \ station_info.aspx" Line 814: @_ _parser.AddParsedSubObject(new System.Web.UI.LiteralControl(" \ r \ n <br / > \ r \ n <br / > \ r \ n ")); Line 815 line hidden Line 825: Line 826: #line 11 "E: \ project \ station_info.aspx" Line 827: @_ _parser.AddParsedSubObject(@_ _ctrl3); Line 828: Line 829: #line default Line 830: #line hidden Line 831: Line 832
Line 255: #line 27 "C: \ Users \ m7md \ Desktop \ electronix \ electronix \ MasterPage.master" Line 256: @_ _parser.AddParsedSubObject(new System.Web.UI.LiteralControl("كمبيوتر وانيت")); Line 257: Line 258: #line default Line 259 Line 348: #line 25 "C: \ Users \ m7md \ Desktop \ electronix \ electronix \ MasterPage.master" Line 349: @_ _parser.AddParsedSubObject(@_ _ctrl1); Line 350: Line 351: #line default Line 352: #line hidden Line 353: global::System Line 361: #line 25 "C: \ Users \ m7md \ Desktop \ electronix \ electronix \ MasterPage.master" Line 362: @_ _parser.AddParsedSubObject(@_ _ctrl2); Line 363: Line 364: #line default Line 365: #line hidden Line 366: global::System Line 374: #line 25 "C: \ Users \ m7md \ Desktop \ electronix \ electronix \ MasterPage.master" Line 375: @_ _parser.AddParsedSubObject(@_ _ctrl3); Line 376: Line 377: #line default Line 378: #line hidden Line 379: Line 380: #line 25 "C: \ Users \ m7md \ Desktop \ electronix \ electronix \ MasterPage.master" Line 381: @_ _parser.AddParsedSubObject(new System.Web.UI.LiteralControl(" \ n<!- -[if IE 6]> \ n")); Line 382: Line 383: #line Line 393: #line 25 "C: \ Users \ m7md \ Desktop \ electronix \ electronix \ MasterPage.master" Line 394: @_ _parser.AddParsedSubObject(@_ _ctrl4); Line 395: Line 396: #line default Line 397: #line hidden Line 398: Line 399: #line 25 "C: \ Users \ m7md \ Desktop \ electronix \ electronix \ MasterPage.master" Line 400: @_ _parser.AddParsedSubObject(new System.Web.UI.LiteralControl(" \ n<![endif]- -> \ n<script type = \ "text / javascript \ " src = \ "js / boxOver Line 452: #line 129 "C: \ Users \ m7md \ Desktop \ electronix \ electronix \ MasterPage.master" Line 453: @_ _parser.AddParsedSubObject(new System.Web.UI.LiteralControl(" \ r \ n ")); Line 454: Line 455: #line default Line 456
Line 183: #line 8 "C: \ Help-Desk \ Crystal-Reports \ CrystalReportsWebSite1 \ Default.aspx" Line 184: @_ _parser.AddParsedSubObject(new System.Web.UI.LiteralControl("Untitled Page")); Line 185: Line 186: #line default Line 187 Line 208: #line 7 "C: \ Help-Desk \ Crystal-Reports \ CrystalReportsWebSite1 \ Default.aspx" Line 209: @_ _parser.AddParsedSubObject(@_ _ctrl1); Line 210: Line 211: #line default Line 212: #line hidden Line 213: return @_ _ctrl Line 324: #line 11 "C: \ Help-Desk \ Crystal-Reports \ CrystalReportsWebSite1 \ Default.aspx" Line 325: @_ _parser.AddParsedSubObject(new System.Web.UI.LiteralControl(" \ r \ n <div> \ r \ n ")); Line 326: Line 327: #line Line 337: #line 11 "C: \ Help-Desk \ Crystal-Reports \ CrystalReportsWebSite1 \ Default.aspx" Line 338: @_ _parser.AddParsedSubObject(@_ _ctrl1); Line 339: Line 340: #line default Line 341: #line hidden Line 342: Line 343: #line 11 "C: \ Help-Desk \ Crystal-Reports \ CrystalReportsWebSite1 \ Default.aspx" Line 344: @_ _parser.AddParsedSubObject(new System.Web.UI.LiteralControl(" \ r \ n ")); Line 345: Line 346: #line default Line 347 Line 356: #line 11 "C: \ Help-Desk \ Crystal-Reports \ CrystalReportsWebSite1 \ Default.aspx" Line 357: @_ _parser.AddParsedSubObject(@_ _ctrl2); Line 358: Line 359: #line default Line 360: #line hidden Line 361: Line 362: #line 11 "C: \ Help-Desk \ Crystal-Reports \ CrystalReportsWebSite1 \ Default.aspx" Line 363: @_ _parser.AddParsedSubObject(new System.Web.UI.LiteralControl(" \ r \ n \ r \ n < / div> \ r \ n ")); Line 364: Line Line 379: #line 1 "C: \ Help-Desk \ Crystal-Reports \ CrystalReportsWebSite1 \ Default.aspx" Line 380: @_ _parser.AddParsedSubObject(new System.Web.UI.LiteralControl(" \ r \ n \ r \ n<!DOCTYPE html PUBLIC \ "- / / W3C / / DTD XHTML
the ASP.NET web development framework. Almost all examples show one way of using this NameValueCollection, but there are faster and more logical ways. This short information sheet has some examples may avoid it. Query string usage example Advanced example The Request.QueryString collection is a NameValueCollection internally. QueryString is implemented as a property getter for an internal NameValueCollection. The NameValueCollection is a specialized collection that has a somewhat unusual implementation. MSDN describes it as a the QueryString collection. My testing indicates that using the index to access a value in NameValueCollection is far faster than using the ["string"] syntax. - -- Page that uses HasKeys() on QueryString [C public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { / / 1 / / Get collection NameValueCollection n = Request.QueryString; / / 2 / / See if any query string exists if (n.HasKeys()) { / / 3 / / Get string keys available on the URL. In step four, we do two lookups on the NameValueCollection to get the very first key and the very first value. Because we only access and value, this code doesn't work for more than one key value pair. Review NameValueCollection This article is really more about NameValueCollection than QueryString, as QueryString is simply an instance