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

[Solved] Dynamic HeaderTemplate & ItemTemplate

3 Answers 267 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Jomey
Top achievements
Rank 1
Jomey asked on 08 Apr 2008, 12:38 AM

I want to do this dynamically in the codebehind.  Is that possible?

<
HeaderTemplate>

<div>

<table style="width: 100%;" border="0" cellspacing="0" cellpadding="0">

<tr>

<td style="width: 25%;">

User Id

</td>

<td style="width: 25%;">

Name

</td>

<td style="width: 50%;">

Depart

</td>

</tr>

</table>

</div>

</HeaderTemplate>

<ItemTemplate>

<table style="width: 100%;" cellspacing="0" cellpadding="0">

<tr>

<td style="width: 25%;">

<div >

<%

# DataBinder.Eval(Container.DataItem, "id")%>

</div>

</td>

<td style="width: 25%;">

<div >

<%

# DataBinder.Eval(Container.DataItem, "name") %>

</div>

</td>

<td style="width: 50%;">

<div >

<%

# DataBinder.Eval(Container.DataItem, "dep")%>

</div>

</td>

</tr>

</table>

</ItemTemplate>

3 Answers, 1 is accepted

Sort by
0
Rosi
Telerik team
answered on 08 Apr 2008, 05:37 PM
Hi Jomey,

Yes, that is possible.

Please, read our help article for more details - Defining HeaderTemplates at run-time. This approach is described at the second part of the article.

Hope this helps.

Regards,
Rosi
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Jomey
Top achievements
Rank 1
answered on 08 Apr 2008, 09:33 PM
How is the itemtemplate done?  I figured out the header but not sure about the itemtemplate.

Also is it possible - to just bind it to a sqldatasource or a datatable - and have it automatically populate from the datatable header and row values.

Below it says userid and name doesn't exist.  But these are the values that are populated in the datasource of the radcombobox1.

protected void RadComboBox1_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)

{

if (e.Text.Length > 1)

{

SqlDataSource1.SelectParameters[

"EmpName"].DefaultValue = e.Text;

RadComboBox1.DataSourceID =

"SqlDataSource1";

RadComboBox1.DataBind();

}

}



class ItemTemplate : ITemplate

{

public void InstantiateIn(Control container)

{

HtmlTable table = new HtmlTable();

HtmlTableRow row = new HtmlTableRow();

HtmlTableCell cell = new HtmlTableCell();

cell.DataBinding +=

new EventHandler(cell_DataBinding);

row.Controls.Add(cell);

HtmlTableCell cell1 = new HtmlTableCell();

cell1.DataBinding +=

new EventHandler(cell1_DataBinding);

row.Controls.Add(cell1);

table.Controls.Add(row);

container.Controls.Add(table);

}

private void cell_DataBinding(object sender, EventArgs e)

{

HtmlTableCell target = (HtmlTableCell)sender;

RadComboBoxItem item = (RadComboBoxItem)target.BindingContainer;

string itemText = (string)DataBinder.Eval(item, "userid");

target.InnerText = itemText;

}

private void cell1_DataBinding(object sender, EventArgs e)

{

HtmlTableCell target = (HtmlTableCell)sender;

RadComboBoxItem item = (RadComboBoxItem)target.BindingContainer;

string itemText = (string)DataBinder.Eval(item, "name");

target.InnerText = itemText;

}

}

0
Rosi
Telerik team
answered on 09 Apr 2008, 08:54 AM
Hi Jomey,

How you can add Items template at run time you can find here.

As for the binding - I suggest that you see our online example - Databinding

If this does not help you, please open a support ticket and send a running project to test locally. Thus, we will be able to see the problem ourselves and try to find a solution.

Regards,
Rosi
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
ComboBox
Asked by
Jomey
Top achievements
Rank 1
Answers by
Rosi
Telerik team
Jomey
Top achievements
Rank 1
Share this question
or