This is a migrated thread and some comments may be shown as answers.
User control disappear after postback
1 Answer 130 Views
This is a migrated thread and some comments may be shown as answers.
Wei
Top achievements
Rank 1
Wei asked on 31 Dec 2008, 03:18 PM

Hello,


In my webpage I try to add a user control into RadPanelBar during
ItemDataBound event. The user control showed up correctly after the 1st page load, but if user do post back the adding control is disappeared. I figured it could be related to viewstate mismatch, so I try give control a unique ID but problem still persist.

 

Any help is very appreciated!

 

Here is my test page code: http://209.200.250.235/Dynamic_panelbar_test.zip

 

Thanks.

(test.aspx)

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="test" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" style="height: 100%; width: 100%; margin: 0px;  
padding: 0px; overflow: hidden;"> 
<head runat="server">  
    <title></title>  
</head> 
<body style="height: 100%; width: 100%; margin: 0px; padding: 0px; overflow: hidden;">  
    <form runat="server">  
    <telerik:RadScriptManager ID="uxRadScriptManager" runat="server">  
    </telerik:RadScriptManager> 
      
    <telerik:RadAjaxManager ID="uxRadAjaxManager" runat="server">  
    </telerik:RadAjaxManager> 
      
    <telerik:RadPanelBar ID="uxRadMenuBarML" runat="server" BorderStyle="None" BorderWidth="0px" Height="372px" 
        PersistStateInCookie="True" Width="400px" OnItemDataBound="uxRadMenuBarML_ItemDataBound" 
        OnItemClick="uxRadMenuBarML_ItemClick" Style="" Skin="Office2007" EnableEmbeddedSkins="true">  
    </telerik:RadPanelBar> 
    </form> 
</body> 
</html> 
 

(test.aspx.cs)
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using Telerik.Web.UI;  
using System.Globalization;  
 
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming""CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "test")]  
public partial class test : System.Web.UI.Page  
{  
    private List<LFMenuBarDataItem> _menuBarDataItem;  
     
    protected void Page_Load(object sender, EventArgs e)  
    {  
        //enable AJAX update on PanelBar  
        RadAjaxManager.GetCurrent(Page).AjaxSettings.AddAjaxSetting(this.uxRadMenuBarML, this.uxRadMenuBarML);  
    }  
 
    protected override void OnInit(EventArgs e)  
    {  
        base.OnInit(e);  
 
        _menuBarDataItem = new List<LFMenuBarDataItem>();  
 
        //-----------  
 
        AddMenuBarItem(0, "Search""~/UCSearch.ascx");  
        AddMenuBarItem(1, null"Home"""""falsefalse);  
        AddMenuBarItem(2, null"My Inbox"""""falsefalse);  
 
        AddMenuBarItem(3, 1, "dummy item 1"""""falsefalse);  
        AddMenuBarItem(4, 2, "dummy item 2"""""falsefalse);  
 
        //-----------  
 
        uxRadMenuBarML.DataTextField = "Title";  
        uxRadMenuBarML.DataFieldID = "ID";  
        uxRadMenuBarML.DataFieldParentID = "ParentID";  
        uxRadMenuBarML.DataSource = _menuBarDataItem;  
        uxRadMenuBarML.DataBind();  
 
    }  
 
 
    /// <summary>  
    /// Adds the menu bar item. (used by presenter to set menu items)  
    /// </summary>  
    /// <param name="id">The id.</param>  
    /// <param name="parentId">The parent id.</param>  
    /// <param name="title">The title.</param>  
    /// <param name="userControlPath">The user control path.</param>  
    /// <param name="imagePath">The left side image path.</param>  
    /// <param name="isPinDisplayed">if set to <c>true</c> [current pin is displayed].</param>  
    /// <param name="isMenuExpanded">if set to <c>true</c> [current menu is expanded].</param>  
    public void AddMenuBarItem(int id, int? parentId, string title, string userControlPath, string imagePath, bool isPinDisplayed, bool isMenuExpanded)  
    {  
        LFMenuBarDataItem menuBarDataItem = new LFMenuBarDataItem  
        {  
            Id = id,  
            ParentId = parentId,  
            Title = title,  
            PagePath = userControlPath,  
            ImagePath = imagePath,  
            IsPinDisplayed = isPinDisplayed,  
            IsMenuExpanded = isMenuExpanded,  
            IsNestedUserControl = false 
        };  
 
        _menuBarDataItem.Add(menuBarDataItem);  
    }  
 
    /// <summary>  
    /// Adds the menu bar item. (used by presenter to set menu items)  
    /// -this overload is used to install nested user control menu item  
    /// </summary>  
    /// <param name="id">The id.</param>  
    /// <param name="nestUserControlPath">The nest user control path.</param>  
    public void AddMenuBarItem(int id, string title, string nestUserControlPath)  
    {  
        LFMenuBarDataItem menuBarDataItem = new LFMenuBarDataItem  
        {  
            Id = id,  
            Title = title,  
            NestedUserControlPath = nestUserControlPath,  
            IsNestedUserControl = true 
        };  
 
        _menuBarDataItem.Add(menuBarDataItem);  
    }  
 
 
    /// <summary>  
    /// Handles the ItemClick event of the uxRadMenuBarML control.  
    /// </summary>  
    /// <param name="sender">The source of the event.</param>  
    /// <param name="e">The <see cref="Telerik.Web.UI.RadPanelBarEventArgs"/> instance containing the event data.</param>  
    protected void uxRadMenuBarML_ItemClick(object sender, RadPanelBarEventArgs e)  
    {  
          
    }  
 
    /// <summary>  
    /// Handles the ItemDataBound event of the uxRadMenuBarML control.  
    /// </summary>  
    /// <param name="sender">The source of the event.</param>  
    /// <param name="e">The <see cref="Telerik.Web.UI.RadPanelBarEventArgs"/> instance containing the event data.</param>  
    protected void uxRadMenuBarML_ItemDataBound(object sender, RadPanelBarEventArgs e)  
    {  
        LFMenuBarDataItem item = (LFMenuBarDataItem)e.Item.DataItem;  
 
        if (item.IsNestedUserControl) // nested user control  
        {  
            //Control ctl = Page.LoadControl(item.NestedUserControlPath); // load user control  
            Button ctl = new Button(); // here we create a dummy button control for test  
            ctl.Text = "I am a test button. Click me and I should not disappear";  
            ctl.ID = "TestButton" + Guid.NewGuid().ToString();   
              
              
            RadPanelItem radPanelItem = new RadPanelItem();  
            radPanelItem.Controls.Add(ctl);  
 
            e.Item.Items.Clear();  
            e.Item.Items.Add(radPanelItem);  
        }  
        else // normal menubar item  
        {  
            e.Item.ImageUrl = item.ImagePath;  
        }  
 
        e.Item.Value = item.Id.ToString(CultureInfo.InvariantCulture);  
        e.Item.Expanded = item.IsMenuExpanded;  
 
    }  
 
    /// <summary>  
    /// Data class stores menu bar data item  
    /// </summary>  
    internal class LFMenuBarDataItem  
    {  
        public bool IsNestedUserControl  
        {  
            get;  
            set;  
        }  
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance""CA1811:AvoidUncalledPrivateCode")]  
        public bool IsPinDisplayed  
        {  
            get;  
            set;  
        }  
        public bool IsMenuExpanded  
        {  
            get;  
            set;  
        }  
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance""CA1811:AvoidUncalledPrivateCode")]  
        public string Title  
        {  
            get;  
            set;  
        }  
        public int Id  
        {  
            get;  
            set;  
        }  
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance""CA1811:AvoidUncalledPrivateCode")]  
        public int? ParentId  
        {  
            get;  
            set;  
        }  
        public string ImagePath  
        {  
            get;  
            set;  
        }  
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance""CA1811:AvoidUncalledPrivateCode")]  
        public string PagePath  
        {  
            get;  
            set;  
        }  
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance""CA1811:AvoidUncalledPrivateCode")]  
        public string NestedUserControlPath  
        {  
            get;  
            set;  
        }  
    }  
      
}  
 
 

(test.aspx.designer.cs)
//------------------------------------------------------------------------------  
// <auto-generated>  
//     This code was generated by a tool.  
//     Runtime Version:2.0.50727.3053  
//  
//     Changes to this file may cause incorrect behavior and will be lost if  
//     the code is regenerated.  
// </auto-generated>  
//------------------------------------------------------------------------------  
 
 
 
public partial class test {  
      
    /// <summary>  
    /// uxRadScriptManager control.  
    /// </summary>  
    /// <remarks>  
    /// Auto-generated field.  
    /// To modify move field declaration from designer file to code-behind file.  
    /// </remarks>  
    protected global::Telerik.Web.UI.RadScriptManager uxRadScriptManager;  
      
    /// <summary>  
    /// uxRadAjaxManager control.  
    /// </summary>  
    /// <remarks>  
    /// Auto-generated field.  
    /// To modify move field declaration from designer file to code-behind file.  
    /// </remarks>  
    protected global::Telerik.Web.UI.RadAjaxManager uxRadAjaxManager;  
      
    /// <summary>  
    /// uxRadMenuBarML control.  
    /// </summary>  
    /// <remarks>  
    /// Auto-generated field.  
    /// To modify move field declaration from designer file to code-behind file.  
    /// </remarks>  
    protected global::Telerik.Web.UI.RadPanelBar uxRadMenuBarML;  
}  
 

(test.aspx.cs)(test.aspx.designer.cs)

1 Answer, 1 is accepted

Sort by
0
Wei
Top achievements
Rank 1
answered on 19 Jan 2009, 05:16 PM
Resolved. Thanks Paul from Telerik team.

My final changes are:
1. Changed Page_Load:
protected void Page_Load(object sender, EventArgs e)    
{    
    //enable AJAX update on PanelBar    
    RadAjaxManager.GetCurrent(Page).AjaxSettings.AddAjaxSetting(this.uxRadMenuBarML, this.uxRadMenuBarML);    
    
    if (!IsPostBack)    
    {    
        RadPanelItem radPanelItem = new RadPanelItem();    
        uxRadMenuBarML.Items[0].Items.Add(radPanelItem);    
    }    
    
    //Control ctl = Page.LoadControl(item.NestedUserControlPath); // load user control    
    Button ctl = new Button(); // here we create a dummy button control for test    
    ctl.Text = "I am a test button. Click me and I should not disappear";    
    ctl.ID = "TestButton" + Guid.NewGuid().ToString();    
    uxRadMenuBarML.Items[_menuBarDataItem[i].Id].Items[0].Controls.Add(ctl);    
}    
 

2. Changed uxRadMenuBarML_ItemDataBound
 
    protected void uxRadMenuBarML_ItemDataBound(object sender, RadPanelBarEventArgs e)  
    {  
        LFMenuBarDataItem item = (LFMenuBarDataItem)e.Item.DataItem;  
 
        if (!item.IsNestedUserControl) // nested user control  
        {  
            e.Item.ImageUrl = item.ImagePath;  
        }  
 
        e.Item.Value = item.Id.ToString(CultureInfo.InvariantCulture);  
        e.Item.Expanded = item.IsMenuExpanded;  
 
    } 

Asked by
Wei
Top achievements
Rank 1
Answers by
Wei
Top achievements
Rank 1
Share this question
or