Delaying DataBinding

3 posts, 0 answers
  1. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 13 May 2009 Link to this post

    I have a PanelBar that has a treeview in a template on each PanelBarItem.

    At the moment, all of the TreeViews are databound when the PanelBarItems are databound.

    I'd like to delay the binding of the TreeViews 'til the relevant PanelBarItem has been expanded. I thought that the most obvious way to do this would have been in the ItemClicked event but I'm struggling to get it to work.

    So, there are 2 questions:
    1. Is this possible and,
    2. how?

    TIA
    --
    Stuart
  2. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 14 May 2009 Link to this post

    OK, I shouldn't post right at the end of the day, 'cos, well as a description of the problem, that was pants.

    Let me try again.

    I've a very simple UserControl defined like this...
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="wc1.ascx.cs" Inherits="WebApplication1.wc1" %> 
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
    <telerik:RadPanelBar ID="RadPanelBar1" runat="server" Skin="Telerik"  
        OnItemDataBound="RadPanelBar1_ItemDataBound"
    </telerik:RadPanelBar> 

    The code behind looks like this ...
    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; 
     
    namespace WebApplication1 
        public partial class wc1 : System.Web.UI.UserControl 
        { 
            Binding _BindData; 
            ITemplate _ItemTemplate; 
     
            public Binding BindData 
            { 
                get { return _BindData; } 
                set { _BindData = value; } 
            } 
            public RadPanelBar PanelBar 
            { 
                get { return RadPanelBar1; } 
            } 
            public ITemplate ItemTemplate 
            { 
                get { return _ItemTemplate; } 
                set { _ItemTemplate = value; } 
            } 
     
            protected override void OnInit(EventArgs e) 
            { 
                _BindData = Binding.Late; 
                base.OnInit(e); 
            } 
            protected void RadPanelBar1_ItemDataBound(object sender, Telerik.Web.UI.RadPanelBarEventArgs e) 
            { 
                RadPanelItem i = (RadPanelItem)e.Item; 
                RadPanelItem j = new RadPanelItem(); 
                if (_ItemTemplate != null) j.ItemTemplate = _ItemTemplate; 
                i.Items.Add(j); 
                j.DataBind(); 
            } 
        } 
    Nothing particularly clever here. All the control does is display a PanelBar in such a way as to allow the DataBound items to expand and collapse even if they contain a templated item. An example of a calling page looks like this...
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> 
     
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
     
    <%@ Register src="wc1.ascx" tagname="wc1" tagprefix="uc1" %> 
     
    <!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" > 
    <head runat="server"
        <title></title
    </head> 
    <body> 
        <form id="form1" runat="server"
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server"
        </telerik:RadScriptManager> 
        <div> 
         
            <uc1:wc1 ID="wc11" runat="server" /> 
         
        </div> 
        </form> 
    </body> 
    </html> 

    With this codebehind...
    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; 
     
    namespace WebApplication1 
     
     
        public partial class WebForm1 : System.Web.UI.Page 
        { 
            MyTreeViewTemplate template = new MyTreeViewTemplate(); 
            protected override void OnInit(EventArgs e) 
            { 
                template = new MyTreeViewTemplate(); 
                base.OnInit(e); 
            } 
            protected void Page_Load(object sender, EventArgs e) 
            { 
                wc11.PanelBar.DataSource = SiteData.Data; 
                wc11.ItemTemplate = template; 
                wc11.BindData = Binding.Late; 
                wc11.PanelBar.DataTextField = "Text"
                wc11.PanelBar.DataValueField = "ParentID"
                wc11.PanelBar.DataBind(); 
            } 
        } 
     
        public class MyTreeViewTemplate : ITemplate 
        { 
            #region ITemplate Members 
     
            void ITemplate.InstantiateIn(Control container) 
            { 
                RadTreeView view = new RadTreeView(); 
                RadPanelItem item = (RadPanelItem)container; 
                view.ID = "ItemTreeView"
                view.Skin = "Telerik"
                view.DataSource = SiteData.GetFilteredData(Convert.ToInt32(DataBinder.Eval(item.Owner, "Value"))); 
                view.DataTextField = "Text"
     
                container.Controls.Add(view); 
            } 
            #endregion 
        } 



    Now, this code databinds the TreeView for each PanelBarItem template as the PanelBar is loaded. What I wanted to do was have the TreeView for any given item databound on expanding the relevant PanelBar item.

    I tried to do this by wiring up the Panelbar's OnItemClick event and moving the code to databind the TreeView into that event's handler. The problem is that clicking on a Panelbar item seems to break the whole thing in that the template shows very briefly and then the PanelBar item collapses again.

    I think that this has something to do with the fact that the PanelBar is posting back when I click on it ('cos that's what it's supposed to do). I can't quite get my head around what I would need to do get it to work as it should, that is, binding the treeview and showing the selected panelbar item as expanded.

    Can anyone offer any suggestions?

    --
    Stuart



  3. Paul
    Admin
    Paul avatar
    4281 posts

    Posted 18 May 2009 Link to this post

    Hi Stuart,

    Please refer to this forum post for tips on the matter.

    Regards,
    Paul
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top