.net ViewState in page lifecycle

后端 未结 4 798
遇见更好的自我
遇见更好的自我 2020-12-21 02:30

I have a page containing a control called PhoneInfo.ascx. PhoneInfo is dynamically created using LoadControl() and then the initControl() function is called passing in an i

4条回答
  •  别那么骄傲
    2020-12-21 03:01

    Based on the comment responses, I'm adding another answer here that explains a part of the Page Lifecycle and what it means for post and viewstate data. Below is a more in-English (to a dev) and less about events version of the beginning of the page lifecycle:

    1. User requests a page (request 1)
    2. ASP.NET builds the page with default values in fields (request 1)
    3. On your Page_Load, you add this special control with your default values (request 1)
    4. You send this page to the user (request 1)
    5. The user posts data to the page (request 2)
    6. ASP.NET builds the page from your ASPX file (request 2)
    7. ASP.NET adds the ViewState data to the page (at this time, there is none) (request 2)
    8. ASP.NET adds the Post data to the page (request 2)
    9. ASP.NET runs reaches the Load step of the Page Lifecycle and adds your special control with your default values (request 2)
    10. You are now in the problematic state that you are seeing.

    So the problem here is that your dynamically-loaded control does not exist when post data is added to the page. When you add it, you put your default values in. ASP.NET no longer has a chance to populate it with the proper data.

提交回复
热议问题