dinamically created webparts disappear after postback

Last post 05-08-2008 5:41 AM by nilart. 6 replies.

Sort Posts:

  • dinamically created webparts disappear after postback

    05-07-2008, 2:33 PM
    • Loading...
    • nilart
    • Joined on 09-12-2007, 3:50 AM
    • Posts 46

    I know this issue has been asked some times already since i did some research, however i couldn't still find a fix for my problem...

     I have a page with dinamically created WebParts using an UpdatePanel. I create 2 WebPartZones on Page_Init and add a WebPart inside one of them... everything seems to work perfectly but when i move the WebPart from one zone to another everthing disappears so looks like the page is not saving the state of the controls added even tho i have tried enabling it on web.config and page header, i have all this on a ascx control, dont know if it has something to do:

     
    ascx:

     

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <asp:WebPartManager ID="wpmPrincipal" runat="server">         
                </asp:WebPartManager>
                
    </ContentTemplate>
    </asp:UpdatePanel>

     codebehind:

     

    protected void Page_Init(object sender, EventArgs e)
            {
                if (this.Page.IsPostBack == false){
                    this.wpmPrincipal.DisplayMode = WebPartManager.DesignDisplayMode;
    
                    WebPartZone wpz1 = configurarWebPart(new WebPartZone(), 200, 500);
                    wpz1.ID = "wpz1";
                    TextBox txtbox1 = new TextBox();
                    txtbox1.ID = "txtBox1";
    
                    WebPart wp1 = wpmPrincipal.CreateWebPart(txtbox1);
                    this.UpdatePanel1.ContentTemplateContainer.Controls.Add(wpz1);
                    wpmPrincipal.AddWebPart(wp1, wpz1, 0);
    
                    WebPartZone wpz2 = configurarWebPart(new WebPartZone(), 400, 300);
                    wpz2.ID = "wpz2";
                    this.UpdatePanel1.ContentTemplateContainer.Controls.Add(wpz2);
                }
                
            }

     
    web.config pages section:

    <pages enableViewState="true">
                <controls>
                    <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                    <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                </controls>
            </pages>
     

    i know it must be an easy problem or something i have missed up :S

     

    thanks in advance 

  • Re: dinamically created webparts disappear after postback

    05-08-2008, 1:02 AM
    • Loading...
    • vinz
    • Joined on 10-05-2007, 11:47 AM
    • Cebu Philippines
    • Posts 6,390

    Try to remove this line below and see what happens

    if (this.Page.IsPostBack == false){}

     

    Cheers,
    Vincent Maverick Durano


  • Re: dinamically created webparts disappear after postback

    05-08-2008, 1:50 AM
    • Loading...
    • nilart
    • Joined on 09-12-2007, 3:50 AM
    • Posts 46

    Without that line i get duplicated Web Parts on every postback :S

    Everytime i move the webpart from one zone to another one more webpart is added to the zone 1 :S 

  • Re: dinamically created webparts disappear after postback

    05-08-2008, 2:00 AM
    • Loading...
    • vinz
    • Joined on 10-05-2007, 11:47 AM
    • Cebu Philippines
    • Posts 6,390

    I think its because of UpdatePanel, try to set the attribute UpdateMode = "Conditional" in your update panel control... Dynamically added controls should persist on every postbacks so that it will not disappear when the page is posted back to the server...  

    Cheers,
    Vincent Maverick Durano


  • Re: dinamically created webparts disappear after postback

    05-08-2008, 3:58 AM
    • Loading...
    • nilart
    • Joined on 09-12-2007, 3:50 AM
    • Posts 46

     its still not working :S

    I tried some setups with no luck, the last was the following:

     
    WebPartZone wpz1;
            WebPartZone wpz2;
            WebPart wp1;
            TextBox txtbox1;
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (this.Page.IsPostBack == false)
                {
    
                    this.wpmPrincipal.DisplayMode = WebPartManager.DesignDisplayMode;
                    wpmPrincipal.AddWebPart(wp1, wpz1, 0);
                    this.UpdatePanel1.UpdateMode = UpdatePanelUpdateMode.Conditional;
                }
                
            }
            protected void Page_Init(object sender, EventArgs e)
            {
                txtbox1 = new TextBox();
                txtbox1.ID = "txtBox1";
                ///if (this.Page.IsPostBack == false){
                
                
                wp1 = wpmPrincipal.CreateWebPart(txtbox1);
                wp1.ID = "wp1";
    
    
                wpz1 = configurarWebPart(new WebPartZone(), 200, 500);
                wpz1.ID = "wpz1";
    
                this.UpdatePanel1.ContentTemplateContainer.Controls.Add(wpz1);
    
    
                wpz2 = configurarWebPart(new WebPartZone(), 400, 300);
                wpz2.ID = "wpz2";
                this.UpdatePanel1.ContentTemplateContainer.Controls.Add(wpz2);
                //}
                
            }

      
    This way the state doesnt clear but when i move the web part from one zone to another it just disappears, but at least the zones stay :S

  • Re: dinamically created webparts disappear after postback

    05-08-2008, 4:24 AM
    • Loading...
    • nilart
    • Joined on 09-12-2007, 3:50 AM
    • Posts 46

     i have tried removing the updatePanel and see what happens, it nows refresh the page as expected but the problem is still there, so basically is a webpart problem :S

  • Re: dinamically created webparts disappear after postback

    05-08-2008, 5:41 AM
    Answer
    • Loading...
    • nilart
    • Joined on 09-12-2007, 3:50 AM
    • Posts 46

     woooot, fixed it... searching through google found this article: http://leedale.wordpress.com/2008/01/10/dynamically-adding-webparts-using-webpartmanagerinternals-class

    it explains how this problem is due to the fact AddWebPart method gives the WebPart a random id and therefore it enters in conflict with page viewstate... the fix was to create a class inherited from WebPartManager class using an alternative AddWebPart method and using it instead...

     

     

    public class DynamicWebPartManager : WebPartManager
    {
    public void AddDynamicWebPart(WebPart webPart, WebPartZone zone)
    {
    Internals.AddWebPart(webPart);
    Internals.SetZoneID(webPart, zone.ID);
    }
    }

      

    Then create the WebPartManager from the code using this class and its method, the webpart is added correctly and works perfect!

    Thanks a lot for help tho :D

     

    Best regards,

    Guillermo

Page 1 of 1 (7 items)