Dynamically added usercontrols not persisting.

3 posts, 0 answers
  1. Dave Isackson
    Dave Isackson avatar
    2 posts
    Member since:
    May 2010

    Posted 21 May 2010 Link to this post

    Hi,
    I am new to telerik. I have a TabStrip control in a web content page. I need to add several user controls dynamically to each tab. They need to be added after the pageview is created so I can't use the dynamic control load example in the demos as this assumes you are only adding the control at the time the pageview is created. I can add the user controls at page_load and it works fine and postback is ok. However, when I add any controls via my "NEXT" button the control only persists until the next postback.

    How can I persist these user controls through a postback?

    My ASPX snippet:

    <

     

    telerik:RadScriptManager ID="RadScriptManager1" runat="server"/>

     

     

     

     

    <div id="RadTabDiv">
    <telerik:RadTabStrip Style="position: absolute; top: 84px;" ID="RadTabStrip1" SelectedIndex="0" runat="server" MultiPageID="RadMultiPage1" Height="100%" Width="100%">

     

     

     

    <Tabs>

     

     

     

    </Tabs>

     

     

     

    </telerik:RadTabStrip>

     

     

     

    <telerik:RadMultiPage Style="position: absolute; top: 134px;" ID="RadMultiPage1" runat="server" SelectedIndex="0">

     

     

    <telerik:RadTextBox Runat="server">

     

     

    </telerik:RadTextBox>

     

     

    </telerik:RadMultiPage>

     

     

    </div>

     

     

    <div id="Next" style="padding: 20px; float:right">

     

     

    <asp:Button ID="btnNext" runat="server" Text="Next" CssClass="Button" />

     

     

    </div>

     

     

    One of the user controls I am adding:

    <%

    @ Control Language="vb" AutoEventWireup="false" CodeBehind="QuestionTextbox.ascx.vb" Inherits="CSS.QuestionTextbox" %>

     

    <%

     

    @ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

     

     

     

    <div style="height:50px">

     

     

     

    <asp:Label ID="lblQuestion" runat="server" Text="Question:"/>

     

     

    <asp:Label ID="lblQuestionText" runat="server" Text="??????"/>

     

     

    <br />

     

     

    <telerik:RadTextBox ID="txtAnswer2" runat="server" Width="200"/>

     

     

    <telerik:RadComboBox Runat="server"></telerik:RadComboBox>

    </

     

     

    div>
    My Code behind (if it helps). In this example I am hardcoding the Tabs and usercontrols but in the final app these will be dynamically generated via a list held in external data.

     

     

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not Page.IsPostBack Then

            AddTab(

    "General")

     

     

     

            AddTab(

    "Check Nearby SCNs")

     

     

        End If

             AddControls()

     

    End Sub

     

     

     

     

     

     

    Private Sub AddTab(ByVal tabName As String)

        Dim tab As New RadTab()

        tab.Text = tabName

        RadTabStrip1.Tabs.Add(tab)

     

     

     

        Dim pageView As New RadPageView()

        pageView.ID = tabName

        RadMultiPage1.PageViews.Add(pageView)

     

     

     

    End Sub

     

    Private Sub AddControls()

        Dim ProcessID As Integer = 20

        Dim TaskID As Integer = 10

        Dim Pageview As RadPageView = FindControl("ctl00$ContentPlaceHolder1$General")

        Dim ucQuestion As UserControl = Page.LoadControl("UserControls/QuestionTextbox.ascx")

     

        ucQuestion.ID =

     

    "uc" & ProcessID & TaskID

     

     

     

     

     

        Dim lblQuestionText As Label = ucQuestion.FindControl("lblQuestionText")

        lblQuestionText.Text =

     

    "What is your name?"

     

        Pageview.Controls.Add(ucQuestion)

        TaskID = 20

     

        Dim ucQuestion2 As UserControl = Page.LoadControl("UserControls/Statement.ascx")

        ucQuestion2.ID =

     

    "uc" & ProcessID & TaskID

     

     

        Pageview.Controls.Add(ucQuestion2)

        TaskID = 30

     

     

     

        Dim ucQuestion3 As UserControl = Page.LoadControl("UserControls/QuestionDropdown.ascx")

        ucQuestion3.ID =

     

    "uc" & ProcessID & TaskID

     

     

        Pageview.Controls.Add(ucQuestion3)

        TaskID = 50

     

     

     

        Dim txt1 As New TextBox

        txt1.Text =

     

    "THIS IS THE TEXT"

     

        Pageview.Controls.Add(txt1)

     

        Dim Pageview2 As RadPageView = FindControl("ctl00$ContentPlaceHolder1$Check Nearby SCNs")

        TaskID = 40

     

     

     

     

        Dim ucQuestion4 As UserControl = Page.LoadControl("UserControls/QuestionTextbox.ascx")

        ucQuestion4.ID =

     

    "uc" & ProcessID & TaskID

     

     

        lblQuestionText = ucQuestion4.FindControl(

     

    "lblQuestionText")

     

     

        lblQuestionText.Text =

     

    "What is your name?"

     

     

     

        Pageview2.Controls.Add(ucQuestion4) 

     

     

        Dim btnNext As Button = FindControl("ctl00$ContentPlaceHolder1$btnNext")  

        btnNext.Enabled = 

     

    True

     

     

     

    End Sub

     

     

     

     

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNext.Click

     

     

     

        Dim Pageview As RadPageView = FindControl("ctl00$ContentPlaceHolder1$General")

     

     

     

        Dim ucQuestion As New UserControl

     

        ucQuestion = Page.LoadControl(

     

    "UserControls/QuestionTextbox.ascx")

     

        ucQuestion.ID =

     

    "uc" & Now

     

     

     

        Dim lblQuestionText As Label = ucQuestion.FindControl("lblQuestionText")

     

        lblQuestionText.Text =

     

    "Time is: " & Now

     

        Pageview.Controls.Add(ucQuestion)

     

    End Sub

     

     

     

     

     

     



    Thanks
    Dave
  2. Yana
    Admin
    Yana avatar
    5043 posts

    Posted 26 May 2010 Link to this post

    Hi Dave,

    Dynamically added user controls should be added on every postback with the same ID. I've attached a simple page to demonstrate how to achieve this, download it and examine it.

    Greetings,
    Yana
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Dave Isackson
    Dave Isackson avatar
    2 posts
    Member since:
    May 2010

    Posted 26 May 2010 Link to this post

    Thanks Yana,

    I will have a look at the code.

    regards

    Dave

Back to Top