Extaspnet application tips (7)-viewstate Optimization

Source: Internet
Author: User
Introduction

In extasp. Net v2.0.6, there is a major update:
------ Optimize the internal implementation of Ajax. The viewstate saved on each page is now reduced by about 1/3 (important updates ).
If you are interested in comparing the viewstate size of some pages in v2.0beta5 and v2.0.6, you can refer to the previous blog.

Ajax in extaspnet
In the product description of extaspnet, there is such a saying "Native Ajax support", which is actually a feature we highly recommend.
The so-called "native" means that developers do not need to make any settings. The PostBack in extaspnet is an Ajax process by default.
Consider a simple example: < Ext: pagemanager ID = "Pagemanager1" Runat = "Server"   />
< Ext: button ID = "Button1" Runat = "Server" Onclick = "Button#click" Text = "Button" >
</ Ext: button >

Protected VoidButton#click (ObjectSender, eventargs E)
{
Button1.text= "Click:" +Datetime. Now. tolongtimestring ();
}

When we click the button, the value of the button is changed to "click: 20: 34: 42". If you carefully observe that this PostBack is not like clicking a common Asp.net button,
The page is not refreshed, and a red prompt box "loading..." is displayed in the upper right corner of the page during the sending-back process ....":

Through httpwatch, we can see that the returned value is:
Box. $0. Enable ();
Box. util. setdisabledcontrolbeforepostback ('');
Box. $0. settext ('click: 20: 34: 42 ');
Box. util. updateviewstate ('ehgruzxh0bq5dbgljazoymdozndo0mh4djelebqikmgrkcbyzbkq + ki5ltj9gviiyaapdfxm = ', 39 );

The returned result is a string containing the Javascript script for modifying the button value: box. $0. settext ('click: 20: 34: 42 ');

Implementation of Ajax in extaspnet v2.0beta5
In fact, it is very easy to implement Ajax. We only need to know which controls have changed attributes in this PostBack, and then generate a Javascript script for these changed attributes.
In extaspnet v2.0beta5, it is also based on this simple consideration.
1. First, when the page is loaded for the first time, we save the text attribute of the button (save it to viewstate ). // If it is not extaspnetajax, save the attribute to viewstate.
If ( ! Isextaspnetajaxpostback)
{
Viewstate [ " Text_hashcode " ] = Text. gethashcode (). tostring ( " X8 " );
}

So you can see this in this article:

The reason why the attribute value is stringized and then the hash value is used to save the viewstate size (if you save the text of the multi-line text box in viewstate, The viewstate will change greatly ).

2. When Ajax is sent back, we compare the property value with the value in viewstate. If the value changes, the Javascript script is output. String Currenttext = Text. gethashcode (). tostring ( " X8 " );
If (Currenttext ! = Viewstate [ " Text_hashcode " ]. Tostring ())
{
// Output box. $0. settext ('click: 20: 34: 42 ');
Viewstate [ " Text_hashcode " ] = Currenttext;
}

Is it easy.

the implementation of Ajax in extaspnet v2.0.6
why do I think about improvement? This is because I was upset when I wrote the previous article and looked at the hashcode on the screen. Can we get rid of this annoying viewstate?
it's still the old question: How do you know that a property has changed during this sending back.
In fact, when the page is sent back, the viewstate of all controls is reloaded, therefore, we only need to compare the value of viewstate loaded by the control and the value of viewstate when the page is to be rendered.
we already know that there is an event in the control called onprerender , which indicates the stage before the control is rendered to the page, we have used it multiple times.
you only need to find the time when the control has just been loaded with viewstate .
let's take a look at the lifecycle of the custom control:
this time should be before onload and after loadviewstate (or after loadpostdata ), unfortunately, there is no onpreload event !!
some netizens may say that you cannot check viewstate during onload?
----- actually, when the onload of the control is executed, the page_load of the page has been executed and the developer may have changed the property value of the control in page_load.

In fact, there are still some solutions. We just need to register the onpreload event of page for the control, as shown below:CodeAs shown in: Protected Override VoidOninit (eventargs E)
{
Base. Oninit (E );

If (Page ! =   Null )
{
// The page. preload event is registered only when extaspnet Ajax is returned.
If (Isextaspnetajaxpostback)
{
Page. Preload + =   New Eventhandler (onpreload );
}
}
}

Finally, we only need to compare the attribute values of the control at the onpreload time and the attribute values at the onprerender time to know whether this attribute has been changed by the user in this PostBack.

Postscript
There are often multiple solutions to a problem. It is always the right choice to first implement the function and then consider optimization.

Happy coding.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.