开发者

Loading different controls in a Conditional UpdatePanel after Postback

I have markup in this format:-

 <asp:UpdatePanel runat="server" id="UpdatePanelList" UpdateMode="conditional">
     <ContentTemplate>   
         <asp:ListView></asp:ListView> 
     </ContentTemplate> 
 </asp:UpdatePanel>

 <asp:UpdatePanel runat="server" ID="UpdatePanelMain" UpdateMode="Conditional">
    <ContentTemplate />
 </asp:UpdatePanel> 

In this update panel, if the record from the database can be found a DataGrid control (with label and dropdown controls) should be displayed. If not a HtmlGenericCont开发者_如何学Crol DIV Control should be displayed with a relevant message asking the user to create the record. Both these controls are dynamically created which are populated with custom-built classes using "output" functions like "ToString".

If I test the operation with several successive DataGrid controls populating the UpdatePanel, this works fine. Similarily, the HtmlGenericControl DIV populates as expected also in multiple succession.

However, when I test the other control type to populate the UpdatePanel, this works on the first instance, but any additional postbacks to populate the UpdatePanel fail and a dialogue box prompts:

Failed to load viewstate.  The control tree into which viewstate is being 
loaded must match the control tree that was used to save viewstate during
the previous request.  For example, when adding controls dynamically, the 
controls added during a post-back must match the type and position of the 
controls added during the initial request.

I was led to believe the UpdatePanel Control takes control of the viewstate in this instance for dynamic controls.

How do I restore the viewstate to allow multiple types of controls to be displayed in the same UpdatePanel?

Thanks.


Restore all the dynamic controls (save the information to do so to the ViewState, or whatever is determined appropriate) in the same hierarchy order as the last page was generated in the "Init" or "Load" life-cycle events. This is the same requirement for normal and asynchronous post-backs in ASP.NET WebForms.

The controls must be added -- to match the control hierarchy -- and then removed after the "Load" event.

Refer to:

  1. DynamicControlsPlaceholder, which is a pre-made control to help with this. It also discusses how it works.
  2. Dynamic Loading of ASP NET User Controls.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜