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

ItemTemplate in the serverside

2 Answers 69 Views
TreeList
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 10 May 2011, 02:20 PM
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 Answers, 1 is accepted

Sort by
0
Gimmik
Top achievements
Rank 1
answered on 10 May 2011, 10:16 PM
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
0
David
Top achievements
Rank 1
answered on 11 May 2011, 02:53 PM
Works great! Thank
Tags
TreeList
Asked by
David
Top achievements
Rank 1
Answers by
Gimmik
Top achievements
Rank 1
David
Top achievements
Rank 1
Share this question
or