This is a migrated thread and some comments may be shown as answers.

Dynamically created TabStrip losing PageView on postback

3 Answers 242 Views
TabStrip
This is a migrated thread and some comments may be shown as answers.
Dave Miller
Top achievements
Rank 2
Dave Miller asked on 12 Oct 2012, 08:19 PM
I have a dynamically added usercontrol that dynamically creates a TabStrip and associated PageViews from a database. For SEO purposes what I want this to do is create the PageViews for some of the tabs initially but also have some of the Tabs that will dynamically add thier PageViews on TabClick. Some of these also get additional user controls added to the PageView.

The problem is that the tabs with PageViews work fine until I click one that loads another page OnPostBack. Once the new PageView is dynamacally added OnPostBack I loose all of the ones that were originally created.

I believe I need to use the multipage PageViewCreated event to persist the dynamically created controls inside a RadPageView such as what is shown on this demo http://demos.telerik.com/aspnet-ajax/tabstrip/examples/applicationscenarios/loadondemand/defaultcs.aspx.

My problem is I am adding the content to the initially created PageViews by placing it into a Literal using Controls.Add which works fine initially. The problem is I can't seem to find it using e.PageView.FindControl to persit it in the PageViewCreated event below which always shows "PageView not found" in the code below.

Any help would be greatly appreciated!

Thanks in advance,
Dave

protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {
               LoadPageTabs(intControlID);
 
               if (Convert.ToInt32(SessionHandler.UserLevel) > 9)
               {
                   RadMenu1.Visible = true;
                   ScriptBlock1.Visible = true;
                   RadTabStrip1.OnClientContextMenu = "ShowRadMenu";
                   RadTabStrip1.OnClientTabSelected = "OnClientTabSelected";
               }
 
               ConfigureRadEditor();
           }
 
       }
 
       protected void LoadPageTabs(int intPageID)
       {
           PageLogic pl = new PageLogic();
           DataTable dt = pl.GetPageTabs(0, intPageID, 0);
           if (dt.Rows.Count > 0)
           {
 
               if (!Page.IsPostBack)
               {
                   lblPgTabID.Text = dt.Rows[0]["PgTabID"].ToString();   
               }
 
               foreach (DataRow dr in dt.Rows)
               {
                   try
                   {
                       AddTab(Convert.ToInt32(dr["PgTabID"].ToString()), Convert.ToInt32(dr["ParentID"].ToString()), dr["TabName"].ToString(), Convert.ToInt32(dr["ShowPv"].ToString()), dr["PvContent"].ToString(), dr["PvControl"].ToString(), Convert.ToInt32(dr["PvControlID"].ToString()));
                   }
                   catch (Exception)
                   {
                       throw;
                   }
               }
           }
           else
           {
               phAddTab.Visible = true;
               phTabs.Visible = false;
           }
       }
 
 
       private void AddTab(int intPgTabID, int intParentID, string tabName, int intShowPv, string pvContent, string pvControl, int pvControlID)
       {
           RadTab ParentTab = new RadTab();
           //tab.ID = "tb" + tabName;
           ParentTab.Text = tabName;
           ParentTab.Value = intPgTabID.ToString();
           ParentTab.Attributes.Add("TabID", intPgTabID.ToString());
           RadTabStrip1.Tabs.Add(ParentTab);
 
           if (intParentID < 0)
           {
               PageLogic pl = new PageLogic();
               DataTable dt = pl.GetPageTabs(0, 0, intPgTabID);
               if (dt.Rows.Count > 0)
               {
                   int rowcnt = 0;
                   foreach (DataRow dr in dt.Rows)
                   {
                       try
                       {
                           RadTab ChildTab = new RadTab();
                           ChildTab.Text = dr["TabName"].ToString();
                           ChildTab.Value = intPgTabID.ToString();                         
                           ChildTab.Attributes.Add("TabID", dr["PgTabID"].ToString());
                           ParentTab.Tabs.Add(ChildTab);
                           if (intShowPv > 0)
                           {
                               AddPageView(ChildTab, Convert.ToInt32(dr["PgTabID"].ToString()), dr["PvContent"].ToString(), dr["PvControl"].ToString(), Convert.ToInt32(dr["PvControlID"].ToString()));
                           }
                           if (rowcnt == 0)
                           {
                               ChildTab.Selected = true;
                           }
 
                           rowcnt++;
                            
                       }
                       catch (Exception)
                       {
                           throw;
                       }
                   }
               }
 
           }
 
           if (intParentID < 1)
           {
               if (intShowPv > 0)
               {
                   AddPageView(ParentTab, intPgTabID, pvContent, pvControl, pvControlID);
               }
           }
 
       }
 
 
       private void AddPageView(RadTab tab, int intPgTabID, string pvContent, string pvControl, int pvControlID)
       {
           Literal litPvContent = new Literal();
           litPvContent.Text = pvContent;
           litPvContent.ID = "litPv" + intPgTabID.ToString();
 
           RadPageView pageView = new RadPageView();
           // pageView.ID = tabName;
            
           pageView.ID = intPgTabID.ToString();
           pageView.Controls.Add(litPvContent);
            
           if (pvControl.Length > 0)
           {
               UserControl userControl = (UserControl)this.LoadControl(pvControl);
               ((CustomControl)userControl).ID = "Cntl" + intPgTabID.ToString();
               ((CustomControl)userControl).intControlID = pvControlID;
               ((CustomControl)userControl).intPgMode = 2;
               pageView.Controls.Add(userControl);
           }
 
           RadMultiPage1.PageViews.Add(pageView);
           tab.PageViewID = pageView.ID;
       }
 
 
       private void AddHiddenPageView(RadTab tab)
       {
           int intPgTabID = Convert.ToInt32(tab.Value);
           PageLogic pl = new PageLogic();
           DataTable dt = pl.GetPageTabs(intPgTabID, 0, 0);
           if (dt.Rows.Count > 0)
           {
               Literal litPvContent = new Literal();
               litPvContent.Text = dt.Rows[0]["pvContent"].ToString();
 
               RadPageView pageView = new RadPageView();
               // pageView.ID = tabName;
 
               pageView.ID = "pv" + intPgTabID.ToString();
               pageView.Controls.Add(litPvContent);
               //pageView.Visible = false;
 
 
               if (dt.Rows[0]["pvControl"].ToString().Length > 0)
               {
                   UserControl userControl = (UserControl)this.LoadControl(dt.Rows[0]["pvControl"].ToString());
                   ((CustomControl)userControl).ID = "Cntl" + intPgTabID.ToString();
                   ((CustomControl)userControl).intControlID = Convert.ToInt32(dt.Rows[0]["pvControlID"].ToString());
                   ((CustomControl)userControl).intPgMode = 2;
                   pageView.Controls.Add(userControl);
               }
 
               RadMultiPage1.PageViews.Add(pageView);
               tab.PageViewID = pageView.ID;
           }
       }
 
       protected void RadMultiPage1_PageViewCreated(object sender, RadMultiPageEventArgs e)
       {
           Literal litPvContent = new Literal();
           litPvContent.ID = "litPv1" + e.PageView.ID.ToString();
 
           Literal litPvContent1 = (Literal)e.PageView.FindControl("litPv" + e.PageView.ID.ToString());
 
           if (litPvContent1 != null)
           {
               litPvContent.Text = litPvContent1.Text;
               e.PageView.Controls.Add(litPvContent);
           }
           else
           {
               litPvContent.Text = "PageView Not Found";
           }
 
           e.PageView.Controls.Add(litPvContent);
 
           //string userControlName = e.PageView.ID + "CS.ascx";
 
           //Control userControl = Page.LoadControl(userControlName);
           //userControl.ID = e.PageView.ID + "_userControl";
 
           //e.PageView.Controls.Add(userControl);
 
       }
 
       protected void RadTabStrip1_TabClick(object sender, RadTabStripEventArgs e)
       {       
           AddHiddenPageView(e.Tab);
           e.Tab.PageView.Selected = true;
       }

3 Answers, 1 is accepted

Sort by
0
Nencho
Telerik team
answered on 17 Oct 2012, 11:01 AM
Hello Dave,

I was unable to replicate the experienced problem with the provided snippet of code, since there are classes and methods missing. Could you open a support ticket and provide us a runnable sample which demonstrates the problem? Thus, we would be able to provide you a suitable solution for the faced issue.


Greetings,
Nencho
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Dave Miller
Top achievements
Rank 2
answered on 17 Oct 2012, 05:54 PM
Hello Nencho,

Thanks for the reply. I realize there were things missing here. All I was really trying to figure out is using the PageViewCreated event to to find the dynamically created literals and persist them or the correct way to do this if this is not where it should be done.

Here is a very basic page based off the Load on Demand RadPageView example that shows the problem I am having.

Once a PageView is dynamacally created with the content added in a dynamacally created Literal all previously created ones are lost.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TabTest.aspx.cs" Inherits="Lasertech.TabTest" %>
<%@ 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">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:radscriptblock id="ScriptBlock2" runat="server" visible="true">
    <script language="javascript" type="text/javascript">
    function onTabSelecting(sender, args) {
        if (args.get_tab().get_pageViewID()) {
            args.get_tab().set_postBack(false);
        }
    }
    </script>
</telerik:radscriptblock>
<telerik:RadScriptManager  ID="ScriptManager1" runat="server" EnableViewState="true" EnableHandlerDetection="false" />
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" EnableAJAX="true"  >
         <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadTabStrip1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadMultiPage1" />
                <telerik:AjaxUpdatedControl ControlID="RadTabStrip1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
    </telerik:RadAjaxManager>
    <div>
        <div id="TabsDiv" style="padding: 0px 0px 0px 0px;">
            <table border="0" cellpadding="0" cellspacing="0" width="100%">
                <tr>
                    <td>
                        <telerik:radtabstrip id="RadTabStrip1" selectedindex="0" runat="server" multipageid="RadMultiPage1"
                            skin="Windows7" width="738px" onclienttabselecting="onTabSelecting"
                            ontabclick="RadTabStrip1_TabClick">
                </telerik:radtabstrip>
                    </td>
                </tr>
                <tr>
                    <td>
                        <telerik:radmultipage id="RadMultiPage1" runat="server" selectedindex="0" cssclass="MpTabs"
                            backcolor="#FFFFFF" onpageviewcreated="RadMultiPage1_PageViewCreated">
                </telerik:radmultipage>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    </form>
</body>
</html>

Code Behind

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 Lasertech
{
    public partial class TabTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                AddTab("Customers");
                AddPageView(RadTabStrip1.FindTabByText("Customers"));
                AddTab("Products");
                AddTab("Orders");
            }
        }
 
        private void AddTab(string tabName)
        {
            RadTab tab = new RadTab();
            tab.Text = tabName;
            RadTabStrip1.Tabs.Add(tab);
        }
 
        protected void RadMultiPage1_PageViewCreated(object sender, RadMultiPageEventArgs e)
        {
 
 
            Literal litPvContent1 = (Literal)e.PageView.FindControl("litPv" + e.PageView.ID.ToString());
 
            if (litPvContent1 != null)
            {
                e.PageView.Controls.Add(litPvContent1);
            }
 
 
 
            //string userControlName = e.PageView.ID + "CS.ascx";
 
            //Control userControl = Page.LoadControl(userControlName);
            //userControl.ID = e.PageView.ID + "_userControl";
 
            //e.PageView.Controls.Add(userControl);
        }
 
        private void AddPageView(RadTab tab)
        {
            RadPageView pageView = new RadPageView();
            pageView.ID = tab.Text;
 
            Literal litPvContent = new Literal();
            litPvContent.Text = tab.Text + " Literal";
            litPvContent.ID = "litPv" + tab.Text;
 
            pageView.Controls.Add(litPvContent);
 
            RadMultiPage1.PageViews.Add(pageView);
 
            tab.PageViewID = pageView.ID;
        }
 
        protected void RadTabStrip1_TabClick(object sender, RadTabStripEventArgs e)
        {
            AddPageView(e.Tab);
            e.Tab.PageView.Selected = true;
        }
    }
}


Thanks again.

Best regards,
Dave
0
Accepted
Nencho
Telerik team
answered on 18 Oct 2012, 03:43 PM
Hello Dave,

I have inspected the problem you had faced. What really happens is that the corresponding PageView is selected, but since you create the PageViews dynamically, you should add the Literal control at the PageViewCreated event handler in order to be correctly added each time when the Tab is selected. Please consider the following approach:

protected void RadMultiPage1_PageViewCreated(object sender, RadMultiPageEventArgs e)
        {
 
            Literal litPvContent = new Literal();
            litPvContent.Text = e.PageView.ID + " Literal";
            litPvContent.ID = "litPv" + e.PageView.ID;
 
            e.PageView.Controls.Add(litPvContent);
        }



All the best,
Nencho
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
TabStrip
Asked by
Dave Miller
Top achievements
Rank 2
Answers by
Nencho
Telerik team
Dave Miller
Top achievements
Rank 2
Share this question
or