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

ListView and dynamically loaded controls

7 Answers 263 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Carsten Koster
Top achievements
Rank 2
Carsten Koster asked on 08 Aug 2010, 11:00 AM
Hey Telerik-Team,
i ran into a problem concerning the listview control and dynamically added controls. Here is what I am trying to achieve.

I have a listview-control (LV) that is databound to a datatable. The LV also has a custom template
set for its itemtemplate. This custom template dynamically loads another usercontrol containing a radgrid.
So far so good. To facilitate the binding of postbackeventhandlers(click of a button) in this dynamically added control
the control must be added at the init state of the surrounding page. So in the init event handler I call
"Rebind" on the LV
. The rebind causes the controls to be recreated and the event handler of the custom control, e.g.
a linkbutton, is working.

The problem is the following: After rebinding in the init handler, a second
implicit rebind occurs
, set at the prerender state, see below for the call stack:

    App_Code.zzxj7amw.dll!MotifListItemTemplate.InstantiateIn.AnonymousMethod__0(object sender = {System.Web.UI.WebControls.Panel}, System.EventArgs args = {System.EventArgs}) Line 45   C#
     System.Web.dll!System.Web.UI.Control.OnDataBinding(System.EventArgs e) + 0x5c bytes   
     ...
     Telerik.Web.UI.DLL!Telerik.Web.UI.RadListView.PerformSelect() + 0x32 bytes   
     Telerik.Web.UI.DLL!Telerik.Web.UI.RadListView.Rebind() + 0x28 bytes   
     Telerik.Web.UI.DLL!Telerik.Web.UI.RadListView.OnPreRender(System.EventArgs e = {System.EventArgs}) + 0x39 bytes   
     System.Web.dll!System.Web.UI.Control.PreRenderRecursiveInternal() + 0x68 bytes   
    
This rebind forces the user controls to be created again, and I conclude that it also clears the LV's controls collection.
This however flushes the viewstate so that any changes to the contained radgrid are lost (grouping, sorting, etc).

Is there are way to get rid of this second rebind?

I tried to call rebind in the load stage, which seems to keep the second rebind from happening, but the viewstate is lost due to the
assumed clear of the controls collection.

To sum up, I need to have a ListView Control that dynamically adds controls for each item it has, AND keeps track of the viewstate.

I hope my explanations are clear.

Thank you for your support!

Regards,
Kris

7 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 12 Aug 2010, 08:22 AM
Hello Kris,

I do not quite understand why you need to call Rebind on Init. If you take as base the following demo application:http://demos.telerik.com/aspnet-ajax/listview/examples/definingstructure/programmaticdefinition/defaultcs.aspx

Templates will be loaded properly on Init then ViewState will be loaded properly, event for nested control inside the template and postback event should be triggered properly.

All the best,
Nikolay
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
0
Carsten Koster
Top achievements
Rank 2
answered on 12 Aug 2010, 10:16 AM
Hi again,

I need to call the rebind to make the grid reappear as proven in the attached example ASPX page.

Thank you for your support,

Regards,
Kris

ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test_Viewstate.aspx.cs" Inherits="Test_Test_Viewstate" %>
 
<%@ 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">
     
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
    <asp:PlaceHolder runat="server" ID="pl"></asp:PlaceHolder>
        <telerik:RadListView ID="RadListView1" runat="server">
        </telerik:RadListView>
    </div>
    </form>
</body>
</html>

CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Telerik.Web.UI;
 
public partial class Test_Test_Viewstate : System.Web.UI.Page
{   
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        
        RadListView1.NeedDataSource += new EventHandler<RadListViewNeedDataSourceEventArgs>(RadListView1_NeedDataSource);
        GenericItemTemplate t = new GenericItemTemplate();
        t.NeedControl += new GenericItemTemplate.NeedControlHandler(t_NeedControl);
        RadListView1.ItemTemplate = t;
        // Else the listview will be empty after clicking a column to sort it
        //RadListView1.Rebind();
    }
 
    void RadListView1_NeedDataSource(object sender, RadListViewNeedDataSourceEventArgs e)
    {
        RadListView1.DataSource = new string[] {"test" };
    }
 
    void t_NeedControl(object sender, GenericItemTemplate.NeedControlArgs arg)
    {
        arg.View = (this.CreateDummyGrid());
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {       
    }  
 
    private Control CreateDummyGrid()
    {
        Telerik.Web.UI.RadGrid myRadGrid = new Telerik.Web.UI.RadGrid();
        myRadGrid.ID = "PlacementGrid_";
        myRadGrid.NeedDataSource += new GridNeedDataSourceEventHandler(myRadGrid_NeedDataSource);
 
        myRadGrid.AutoGenerateColumns = true;
        myRadGrid.AllowPaging = false;
        myRadGrid.Visible = true;
        myRadGrid.AllowSorting = true;
        myRadGrid.AllowFilteringByColumn = true;
        myRadGrid.ShowStatusBar = true;
        myRadGrid.ShowFooter = true;
        myRadGrid.Width = new Unit("100%");
        myRadGrid.EnableViewState = true;
        myRadGrid.GroupingEnabled = true;
        myRadGrid.ShowGroupPanel = true;
        myRadGrid.ViewStateMode = System.Web.UI.ViewStateMode.Enabled;
        myRadGrid.ClientSettings.AllowDragToGroup = true;
        myRadGrid.ClientSettings.AllowExpandCollapse = true;
        myRadGrid.ClientSettings.Scrolling.AllowScroll = false;
        myRadGrid.MasterTableView.ShowGroupFooter = true;
        myRadGrid.MasterTableView.CommandItemDisplay = Telerik.Web.UI.GridCommandItemDisplay.None;
        myRadGrid.MasterTableView.CurrentResetPageIndexAction = Telerik.Web.UI.GridResetPageIndexAction.SetPageIndexToFirst;
        myRadGrid.MasterTableView.Frame = Telerik.Web.UI.GridTableFrame.Void;
        myRadGrid.MasterTableView.TableLayout = Telerik.Web.UI.GridTableLayout.Auto;
 
        myRadGrid.Rebind();
        return myRadGrid;
    }
    void myRadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        string[] dummyData = new string[] {"test1","test2","test3" };
        (source as Telerik.Web.UI.RadGrid).DataSource = dummyData;       
    }
}


0
Nikolay Rusev
Telerik team
answered on 17 Aug 2010, 01:11 PM
Hello Carsten,

I have attached sample page demonstrating how to create RadGrid inside RadListView ItemTemplate and all works properly without need for extra rebinds.

Regards,
Nikolay
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
0
Carsten Koster
Top achievements
Rank 2
answered on 21 Aug 2010, 08:31 AM
Hi Nikolay,

I just had time to test your suggestion. Unfortunately the problem remains! If you try to sort the "Value" column twice, it should go from desc to asc to no sorting. But in your example too, the order is: click, sorted desc, click not sorted, click, sorted desc.

That are my findings, I am sorry but this does not fix my problem.

Thank you for your time.

Regards
Kris
0
Nikolay Rusev
Telerik team
answered on 23 Aug 2010, 08:01 AM
Hello Carsten,

Seems like it is working properly on my end. Please check the attached video.

Greetings,
Nikolay
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
0
Carsten Koster
Top achievements
Rank 2
answered on 23 Aug 2010, 08:50 AM
Hello Nikolay,

this is really getting more curious each time. Please see http://aim.advision-digital.de/test/telerik/test.aspx
for a demonstration of the behavior I experience. I used the code you provided.
I am using VS2010 and  version 2010.2.713.40 of your controls. Tested on a firefox 3.6.8 and iexplorer 8.

Could there be a missing server configuration

Thank you for your time,

Regards,
Kris
0
Accepted
Nikolay Rusev
Telerik team
answered on 25 Aug 2010, 11:31 AM
Hello Carsten,

I believe this is caused due to bug in RadListView that recently has been fixed. If you use 2.0 version of Telerik.Web.UI assembly you will experience correct behavior.

I suggest you to download latest internal build or wait few days for SP1 of RadControls for ASP.NET AJAX where the fix will be officially released.

All the best,
Nikolay
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
Tags
ListView
Asked by
Carsten Koster
Top achievements
Rank 2
Answers by
Nikolay Rusev
Telerik team
Carsten Koster
Top achievements
Rank 2
Share this question
or