ItemTemplate in the serverside

3 posts, 0 answers
  1. David
    David avatar
    81 posts
    Member since:
    May 2011

    Posted 10 May 2011 Link to this post

    Hi,

    I was looking the TreeList demo and I find this demo (http://demos.telerik.com/aspnet-ajax/treelist/examples/firstlook/defaultcs.aspx). In this demo, they add a dataSource to their TreeList and they used a <ItemTemplate> to put the ProductName  and the Quantity in the same field. Here the code: 
         <%# Eval("ProductName")%><%# Eval("Quantity").ToString() != "" ? "&nbsp;(" + Eval("Quantity").ToString() + ")" : ""%>

    I wonder if it was possible to do the same, but in the code-behind (serverside). 

    Here is my code :

    if (e.Node.Text == "Users")
    {
        Telerik.Web.UI.TreeListBoundColumn nameColumn =
            new Telerik.Web.UI.TreeListBoundColumn();
     
        nameColumn.DataField = "name";
        nameColumn.UniqueName = "name";
        nameColumn.HeaderText = "Name";
     
     
     
        RadTreeList1.Columns.Clear();
     
        RadTreeList1.Columns.Add(nameColumn);
     
        RadTreeList1.DataSource = MyDataUser.GetData();
    }

    What I want is to take the fields (in my datasource) "firstName" and "lastName" and put it together in the "nameColumns" by using a itemTemplate.

    Thank you

    David
  2. Gimmik
    Gimmik avatar
    170 posts
    Member since:
    May 2011

    Posted 10 May 2011 Link to this post

    Hi David,

    The simplest way to combine two elements server-side is to update the data before it is bound to the control. This can get pretty complicated since data needs to be rebound every time the state changes. However, Telerik provides a handy event you can wire-up to easily accomplish this. The "OnNeedDataSource" will be fired off each time the control needs to rebind data. Here is a simple example.

    <telerik:RadGrid id="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource">
        <MasterTableView AutoGenerateColumns="true" />
    </telerik:RadGrid>

    protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        String ConnString = ConfigurationManager.ConnectionStrings["Northwind_ConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(ConnString);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand("SELECT EmployeeID, LastName, FirstName FROM Employees", conn);
     
        DataTable myDataTable = new DataTable();
     
        conn.Open();
        try
        {
            adapter.Fill(myDataTable);
        }
        finally
        {
            conn.Close();
        }
     
        myDataTable.Columns.Add("FullName", typeof(String));
         
        for (int i=0; i<myDataTable.Rows.Count; i++)
            myDataTable.Rows[i]["FullName"] = myDataTable.Rows[i]["FirstName"] + " " + myDataTable.Rows[i]["LastName"];
     
        RadGrid1.DataSource = myDataTable;
    }

    You should be able to apply the same concept to your RadTreeList and avoid the client-side scripting. However, I personally prefer the client-side approach for performance reasons.

    I hope this helps!
    -Gimmik
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. David
    David avatar
    81 posts
    Member since:
    May 2011

    Posted 11 May 2011 Link to this post

    Works great! Thank
Back to Top