Extaspnet application tips (6)-viewstate and dynamic control creation

Source: Internet
Author: User
Article-better Layout

Problem description

In the previous article, a toolbar button is defined on the page as declared, and another button is defined in the background through programming.

1. Asp.net page declaration:

 
<Ext: Panel showborder = "false" showheader = "false" runat = "server"> <Ext: toolbar id = "toolbar1" runat = "server"> <Ext: button id = "button1" enablepostback = "false" onclientclick = "window. open ('default. aspx ',' _ blank '); "text =" page declaration button "runat =" server "> </ext: button> </ext: toolbar> </ext: panel>

2. BackgroundCode:

 
Protected void page_load (Object sender, eventargs e) {extaspnet. button BTN = new button (); BTN. TEXT = "Get toolbar buttons"; BTN. click + = new eventhandler (btn_click); toolbar1.items. add (BTN);} private void btn_click (Object sender, eventargs e) {alert. show ("toolbar buttons:" + toolbar1.items. count );}

3. Generate a page like this:

In the background, I tried to place the dynamically added toolbar button in the first position, as shown below:

 
Toolbar1.items. insert (0, BTN );

But when I click this button to send back, an error is returned:

Solution

First, I think there may be an internal problem with extaspnet. After debugging, I found that the view State (viewstate) of the two toolbar buttons was lost during sending back.

So why is it possible to use the items. Add method, while the viewstate of the items. insert method will be lost?

So we found a small tool for parsing viewstate viewstatedecoder to view the viewstate on the page:


From the above, I found that the access to the viewstate of the two buttons is in order, so will the view order change be caused by the items. insert method, and there will be errors in reading the background?

So I found this article online.Article: Http://msdn.microsoft.com/en-us/library/ms972976.aspx

I intercepted a section about view status and dynamically add controls:

In this section, we can draw the following conclusions:

    • Page_load is called after the view is loaded on the page, while page_init is called before the view is loaded. Therefore, the best time to dynamically add controls is in page_init.
    • Even if the control is dynamically added in page_load, its view status is maintained because the view status is recursively loaded when controls. Add is called.
    • Controls dynamically added after page_load do not change the order of existing controls; otherwise, the view status cannot be correctly loaded (because the view status is marked in the order of its parent control, as shown in ).

Solution

With the above instructions, the solution is easy. There are three possible solutions:

1. The first one is mentioned at the beginning. Add controls dynamically in page_load without changing the order of existing controls.

2. Add controls dynamically in page_init. You can change the order of existing controls as needed.

 
<Ext: Panel showborder = "false" showheader = "false" runat = "server"> <Ext: toolbar id = "toolbar1" runat = "server"> <Ext: button id = "button1" enablepostback = "false" onclientclick = "window. open ('default. aspx ',' _ blank '); "text =" page declaration button "runat =" server "> </ext: button> </ext: toolbar> </ext: panel>
 
Protected void page_init (Object sender, eventargs e) {extaspnet. button BTN = new button (); BTN. TEXT = "Get toolbar buttons"; BTN. click + = new eventhandler (btn_click); toolbar1.items. insert (0, BTN);} private void btn_click (Object sender, eventargs e) {alert. show ("toolbar buttons:" + toolbar1.items. count );}

3. dynamically create all controls. This is more suitable for dynamically create controls during sending back (for example, click a button to add a control ).

<Ext: Panel showborder = "false" showheader = "false" runat = "server"> <Ext: toolbar id = "toolbar1" runat = "server"> </ext: toolbar> </ext: Panel>
 
Protected void page_load (Object sender, eventargs e) {extaspnet. button BTN = NULL; BTN = new button (); BTN. TEXT = "button declared on the page"; BTN. enablepostback = false; BTN. onclientclick = "window. open ('default. aspx ',' _ blank '); "; toolbar1.items. add (BTN); BTN = new button (); BTN. TEXT = "Get toolbar buttons"; BTN. click + = new eventhandler (btn_click); toolbar1.items. add (BTN);} private void btn_click (Object sender, eventargs e) {alert. show ("toolbar buttons:" + toolbar1.items. count );}

Source codeOn the other \ menu_dynamic2_run.aspx located in the http://extaspnet.codeplex.com/

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.