Dynamically added controls are not available after postback.

6 posts, 0 answers
  1. Karl
    Karl avatar
    172 posts
    Member since:
    Sep 2010

    Posted 09 Dec 2010 Link to this post

    I have an empty RadPanelBar on my webpage. When I say empty, I mean that just the <telerik:RadPanelBar></telerik:RadPanelBar> definition exists with no RadPanelItems in it.

    On page load, I check to see if the page has been posted back, and if not, I create and add a dynamic number of RadPanelItems to the RadPanelBar. Each of these RadPanelItems has a control in it (for testing I'm using a textbox, but eventually it will be a RadGrid).

    I run my page for the first time and up comes my RadPanelBar with the right number of RadPanelItems, and in each one is a textbox. I enter a value into a textbox and click a submit button and the page postsback to itself.

    Now when my page loads, a strange thing happens...
    The event that loads the RadPanelItems does not fire but the RadPanelBar still has the dynamically added RadPanelItems, but these items have no controls in them so now my textboxes have disappeared and I have no access to the values that were entered into them.

    If I skip the check and load the RadPanelItems everytime the page is hit, the I get more strangeness...

    The original RadPanelItems that were created when the page was first loaded are still there, along with newly created RadPanelItems after postback, the new RadPanelItems have the empty textboxes in them, but the existing RadPanelItems again have no textboxes and again I've lost my values.

    How on earth do I get around this? I need to dynamically add RadPanelItems and controls to a RadPanelBar, but then need access to these and the controls in them so I can access their values after postback!
  2. Yana
    Admin
    Yana avatar
    4554 posts

    Posted 14 Dec 2010 Link to this post

    Hello Karl,

    This is expected behavior - dynamically added controls (as textboxes) are not preserved and should be added on every postback. So the panelbaritems should be added on the first page load only and the textboxes    - on every postback, like this:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RadPanelBar1.Items.Add(new RadPanelItem("item1"));
            RadPanelBar1.Items.Add(new RadPanelItem("item2"));
        }
     
        RadPanelBar1.Items[0].Controls.Add(new TextBox());
        RadPanelBar1.Items[1].Controls.Add(new TextBox());
    }


    All the best,
    Yana
    the Telerik team
    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Josh
    Josh avatar
    61 posts
    Member since:
    Dec 2009

    Posted 14 Dec 2010 Link to this post

    Hello Yana,

    I am using the same setup as Karl (dynamically adding items with user controls to panel bar). Some of the items have RadComboBoxes which cause postback events. I tried implementing your solution, but it cannot pick up anything changed in the user control (such as the combo box's selected index), and any data has to be rebound because it is a new instance of the control.

    If i use FindControl, I can still find the control nested in the panel item in the postback; it just does not appear client-side.

    Also, I noticed that when doing this the expand/collapse buttons disappear. (I am using Q3 and IE7)

    Josh
  5. Karl
    Karl avatar
    172 posts
    Member since:
    Sep 2010

    Posted 15 Dec 2010 Link to this post

    Josh,

    After reading what Yana had written I had a look back at my code and the penny dropped. Of course controls dont exist on postback if they're created dynamically.

    The way I've got around this is to have the page_load event check to see if its a postback. If it isn't (the first time the page is hit) call a method that creates my PanelItems. In this method have another call to apply the templates to these panel items and then bind the data to them (in my templates I have RadGrids so these need their data to display)

    If the page is a postback, then the RadPanelItems exist, but the templates havent been applied. All I've done is applied the templates to the RadPanelItems in the page_load event after postback and voila, all my RadPanelItems and their grids work perfectly. Because its only the templates that werent being added to the panels correctly all my updated data exists too.

    In your case, you'll need to add event handlers to your comboboxes in your templates so these get fired correctly.
  6. Brian Hallkvist
    Brian Hallkvist avatar
    2 posts
    Member since:
    May 2006

    Posted 23 Dec 2010 Link to this post

    Hi Karl,

    I am working on similar application and having problem in persisting the changes in RadPanelItems. Can I have some working code example of how you are applying the Templates on PostBacks?
  7. Josh
    Josh avatar
    61 posts
    Member since:
    Dec 2009

    Posted 23 Dec 2010 Link to this post

    This is is how I got it to work. First, I defined my panelbar's item template. In my case I used a user control.

    <telerik:RadPanelBar runat="server" ID="rpbEvent" AllowCollapseAllItems="true" ExpandMode="MultipleExpandedItems" Width="100%">
        <ItemTemplate>
            <me:CustomControl runat="server" ID="pilPanelItem" />
        </ItemTemplate>
    </telerik:RadPanelBar>

    I then created the RadPanelItems in the OnInit event. The number of panel items and the header text for each one comes from a database. Each one has its content template set to the item template of the panel bar.

    protected override void OnInit(EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataView panelItemsView = GetPanelItemView();
     
            foreach (DataRowView row in panelItemsView)
            {
                RadPanelItem item = new RadPanelItem(row["text"].ToString().Trim());
                item.ContentTemplate = rpbEvent.ItemTemplate;
     
                item.Expanded = true;
     
                rpbEvent.Items.Add(item);
            }
        }
        base.OnInit(e);
    }

    Finally, I reassign the template and tell the panel items to rebind in each postback.

    protected void Page_Load(object sender, EventArgs e)
    {
        foreach (RadPanelItem item in rpbEvent.Items)
        {
            item.ContentTemplate = rpbEvent.ItemTemplate;
            CustomControl panelControl = (CustomControl)item.FindControl("pilPanelItem");
            panelControl.DataBind();
        }
    }

    Hope this helps.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017