New to Telerik UI for ASP.NET AJAX? Start a free 30-day trial
Programmatic Definition
As with any control, the RadListView control can be created dynamically, to allow for more flexibility.
As with any other control which uses templates, the structure needs to be created in the PageInit event handler.Each type of template can be created using a separate class do define its structure.Below is a sample, which demonstrates one such approach.
On the *aspx *page there is a simple placeholder, where the actual contents of the *ListView *control will be added:
ASP.NET
<telerik:RadFormDecorator RenderMode="Lightweight" ID="RadFormDecorator1" runat="server" />
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
<telerik:RadAjaxPanel runat="server" ID="RadAjaxPanel1" LoadingPanelID="RadAjaxLoadingPanel1">
<asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
</telerik:RadAjaxPanel>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT TOP 10 [CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address] FROM [Customers]" />
<telerik:Footer runat="server" ID="Footer1">
The actual contents are determined by the code-behind, where each template is declared in a separate class:
C#
public partial class DefaultCS : System.Web.UI.Page
{
protected void Page_Init(object sender, System.EventArgs e)
{
RadListView listView = new RadListView();
listView.DataSourceID = "SqlDataSource1";
listView.LayoutTemplate = new LayoutTemplate();
listView.ItemTemplate = new ItemTemplate();
listView.AlternatingItemTemplate = new AlterNatingItemTemplate();
listView.EmptyDataTemplate = new EmptyDataTemplate();
listView.EditItemTemplate = new EditItemTemplate();
listView.ItemPlaceholderID = "itemPlaceholder";
PlaceHolder1.Controls.Add(listView);
}
}
public class LayoutTemplate : ITemplate
{
protected LiteralControl lControl;
public LayoutTemplate()
{
}
public void InstantiateIn(System.Web.UI.Control container)
{
Table LayoutTable = new Table();
TableRow LayoutTableRow1 = new TableRow();
TableRow LayoutTableRow2 = new TableRow();
LayoutTableRow1.ID = "LayoutTableRow1";
LayoutTableRow1.BackColor = System.Drawing.Color.LightGray;
LayoutTableRow2.ID = "itemPlaceholder";
TableCell LayoutTableRow1Cell1 = new TableCell();
LayoutTableRow1Cell1.Width = Unit.Pixel(50);
TableCell LayoutTableRow1Cell2 = new TableCell();
LayoutTableRow1Cell2.Width = Unit.Pixel(150);
TableCell LayoutTableRow1Cell3 = new TableCell();
LayoutTableRow1Cell3.Width = Unit.Pixel(250);
TableCell LayoutTableRow1Cell4 = new TableCell();
LayoutTableRow1Cell4.Width = Unit.Pixel(200);
TableCell LayoutTableRow1Cell5 = new TableCell();
LayoutTableRow1Cell5.Width = Unit.Pixel(150);
Label CustomerID = new Label();
CustomerID.Text = "CustomerID";
LayoutTableRow1Cell2.Controls.Add(CustomerID);
Label CompanyName = new Label();
CompanyName.Text = "CompanyName";
LayoutTableRow1Cell3.Controls.Add(CompanyName);
Label ContactName = new Label();
ContactName.Text = "ContactName";
LayoutTableRow1Cell4.Controls.Add(ContactName);
Label ContactTitle = new Label();
ContactTitle.Text = "ContactTitle";
LayoutTableRow1Cell5.Controls.Add(ContactTitle);
LayoutTableRow1.Cells.Add(LayoutTableRow1Cell1);
LayoutTableRow1.Cells.Add(LayoutTableRow1Cell2);
LayoutTableRow1.Cells.Add(LayoutTableRow1Cell3);
LayoutTableRow1.Cells.Add(LayoutTableRow1Cell4);
LayoutTableRow1.Cells.Add(LayoutTableRow1Cell5);
LayoutTable.Rows.Add(LayoutTableRow1);
LayoutTable.Rows.Add(LayoutTableRow2);
container.Controls.Add(LayoutTable);
}
}
public class EmptyDataTemplate : ITemplate
{
public EmptyDataTemplate()
{
}
public void InstantiateIn(System.Web.UI.Control container)
{
Label label = new Label();
label.ID = "NoRecordsLabel";
label.Text = "No Records are available at this time";
container.Controls.Add(label);
}
}
public class EditItemTemplate : ITemplate
{
protected LiteralControl lControl;
public EditItemTemplate()
{
}
public void InstantiateIn(System.Web.UI.Control container)
{
TableRow ItemTemplateRow1 = new TableRow();
ItemTemplateRow1.Style["background-color"] = "#45474A";
ItemTemplateRow1.ID = "LayoutTableRow1";
TableCell ItemTemplateRow1Cell1 = new TableCell();
ItemTemplateRow1Cell1.Width = Unit.Pixel(50);
TableCell ItemTemplateRow1Cell2 = new TableCell();
ItemTemplateRow1Cell2.Width = Unit.Pixel(100);
TableCell ItemTemplateRow1Cell3 = new TableCell();
ItemTemplateRow1Cell3.Width = Unit.Pixel(250);
TableCell ItemTemplateRow1Cell4 = new TableCell();
ItemTemplateRow1Cell4.Width = Unit.Pixel(150);
TableCell ItemTemplateRow1Cell5 = new TableCell();
ItemTemplateRow1Cell5.Width = Unit.Pixel(150);
ImageButton UpdateButton = new ImageButton();
UpdateButton.ID = "UpdateButton";
UpdateButton.CommandName = "Update";
UpdateButton.ImageUrl = "~/ListView/Examples/DefiningStructure/DeclarativeDefinition/Img/Update.gif";
ItemTemplateRow1Cell1.Controls.Add(UpdateButton);
ImageButton CancelButton = new ImageButton();
CancelButton.ID = "CancelButton";
CancelButton.CommandName = "Cancel";
CancelButton.ImageUrl = "~/ListView/Examples/DefiningStructure/DeclarativeDefinition/Img/Cancel.gif";
ItemTemplateRow1Cell1.Controls.Add(CancelButton);
TextBox CustomerID = new TextBox();
CustomerID.Width = Unit.Pixel(70);
CustomerID.ID = "CustomerID";
CustomerID.DataBinding += new System.EventHandler(CustomerID_DataBinding);
ItemTemplateRow1Cell2.Controls.Add(CustomerID);
TextBox CompanyName = new TextBox();
CompanyName.Width = Unit.Pixel(210);
CompanyName.ID = "CompanyName";
CompanyName.DataBinding += new System.EventHandler(CompanyName_DataBinding);
ItemTemplateRow1Cell3.Controls.Add(CompanyName);
TextBox ContactName = new TextBox();
ContactName.Width = Unit.Pixel(110);
ContactName.ID = "ContactName";
ContactName.DataBinding += new System.EventHandler(ContactName_DataBinding);
ItemTemplateRow1Cell4.Controls.Add(ContactName);
TextBox ContactTitle = new TextBox();
ContactTitle.Width = Unit.Pixel(120);
ContactTitle.ID = "ContactTitle";
ContactTitle.DataBinding += new System.EventHandler(ContactTitle_DataBinding);
ItemTemplateRow1Cell5.Controls.Add(ContactTitle);
ItemTemplateRow1.Cells.Add(ItemTemplateRow1Cell1);
ItemTemplateRow1.Cells.Add(ItemTemplateRow1Cell2);
ItemTemplateRow1.Cells.Add(ItemTemplateRow1Cell3);
ItemTemplateRow1.Cells.Add(ItemTemplateRow1Cell4);
ItemTemplateRow1.Cells.Add(ItemTemplateRow1Cell5);
container.Controls.Add(ItemTemplateRow1);
}
void ContactTitle_DataBinding(object sender, System.EventArgs e)
{
TextBox ContactTitle = (TextBox)sender;
ContactTitle.Text = ((ContactTitle.NamingContainer as RadListViewDataItem).DataItem as DataRowView)["ContactTitle"].ToString();
}
void ContactName_DataBinding(object sender, System.EventArgs e)
{
TextBox ContactName = (TextBox)sender;
ContactName.Text = ((ContactName.NamingContainer as RadListViewDataItem).DataItem as DataRowView)["ContactName"].ToString();
}
void CompanyName_DataBinding(object sender, System.EventArgs e)
{
TextBox CompanyName = (TextBox)sender;
CompanyName.Text = ((CompanyName.NamingContainer as RadListViewDataItem).DataItem as DataRowView)["CompanyName"].ToString();
}
void CustomerID_DataBinding(object sender, System.EventArgs e)
{
TextBox CustomerID = (TextBox)sender;
CustomerID.Text = ((CustomerID.NamingContainer as RadListViewDataItem).DataItem as DataRowView)["CustomerID"].ToString();
}
}
public class ItemTemplate : ITemplate
{
protected LiteralControl lControl;
public ItemTemplate()
{
}
public void InstantiateIn(System.Web.UI.Control container)
{
TableRow ItemTemplateRow1 = new TableRow();
ItemTemplateRow1.Style["background-color"] = "#99ccff";
ItemTemplateRow1.ID = "LayoutTableRow1";
TableCell ItemTemplateRow1Cell1 = new TableCell();
ItemTemplateRow1Cell1.Width = Unit.Pixel(50);
TableCell ItemTemplateRow1Cell2 = new TableCell();
ItemTemplateRow1Cell2.Width = Unit.Pixel(100);
TableCell ItemTemplateRow1Cell3 = new TableCell();
ItemTemplateRow1Cell3.Width = Unit.Pixel(250);
TableCell ItemTemplateRow1Cell4 = new TableCell();
ItemTemplateRow1Cell4.Width = Unit.Pixel(150);
TableCell ItemTemplateRow1Cell5 = new TableCell();
ItemTemplateRow1Cell5.Width = Unit.Pixel(150);
ImageButton imageButton = new ImageButton();
imageButton.ID = "EditButton";
imageButton.CommandName = "Edit";
imageButton.ImageUrl = "~/ListView/Examples/DefiningStructure/DeclarativeDefinition/Img/Edit.gif";
ItemTemplateRow1Cell1.Controls.Add(imageButton);
Label CustomerID = new Label();
CustomerID.ID = "CustomerID";
CustomerID.DataBinding += new System.EventHandler(CustomerID_DataBinding);
ItemTemplateRow1Cell2.Controls.Add(CustomerID);
Label CompanyName = new Label();
CompanyName.ID = "CompanyName";
CompanyName.DataBinding += new System.EventHandler(CompanyName_DataBinding);
ItemTemplateRow1Cell3.Controls.Add(CompanyName);
Label ContactName = new Label();
ContactName.ID = "ContactName";
ContactName.DataBinding += new System.EventHandler(ContactName_DataBinding);
ItemTemplateRow1Cell4.Controls.Add(ContactName);
Label ContactTitle = new Label();
ContactTitle.ID = "ContactTitle";
ContactTitle.DataBinding += new System.EventHandler(ContactTitle_DataBinding);
ItemTemplateRow1Cell5.Controls.Add(ContactTitle);
ItemTemplateRow1.Cells.Add(ItemTemplateRow1Cell1);
ItemTemplateRow1.Cells.Add(ItemTemplateRow1Cell2);
ItemTemplateRow1.Cells.Add(ItemTemplateRow1Cell3);
ItemTemplateRow1.Cells.Add(ItemTemplateRow1Cell4);
ItemTemplateRow1.Cells.Add(ItemTemplateRow1Cell5);
container.Controls.Add(ItemTemplateRow1);
}
void ContactTitle_DataBinding(object sender, System.EventArgs e)
{
Label ContactTitle = (Label)sender;
ContactTitle.Text = ((ContactTitle.NamingContainer as RadListViewDataItem).DataItem as DataRowView)["ContactTitle"].ToString();
}
void ContactName_DataBinding(object sender, System.EventArgs e)
{
Label ContactName = (Label)sender;
ContactName.Text = ((ContactName.NamingContainer as RadListViewDataItem).DataItem as DataRowView)["ContactName"].ToString();
}
void CompanyName_DataBinding(object sender, System.EventArgs e)
{
Label CompanyName = (Label)sender;
CompanyName.Text = ((CompanyName.NamingContainer as RadListViewDataItem).DataItem as DataRowView)["CompanyName"].ToString();
}
void CustomerID_DataBinding(object sender, System.EventArgs e)
{
Label CustomerID = (Label)sender;
CustomerID.Text = ((CustomerID.NamingContainer as RadListViewDataItem).DataItem as DataRowView)["CustomerID"].ToString();
}
}
public class AlterNatingItemTemplate : ITemplate
{
protected LiteralControl lControl;
public AlterNatingItemTemplate()
{
}
public void InstantiateIn(System.Web.UI.Control container)
{
TableRow ItemTemplateRow1 = new TableRow();
ItemTemplateRow1.Style["background-color"] = "#ccffff";
ItemTemplateRow1.ID = "LayoutTableRow1";
TableCell ItemTemplateRow1Cell1 = new TableCell();
ItemTemplateRow1Cell1.Width = Unit.Pixel(50);
TableCell ItemTemplateRow1Cell2 = new TableCell();
ItemTemplateRow1Cell2.Width = Unit.Pixel(100);
TableCell ItemTemplateRow1Cell3 = new TableCell();
ItemTemplateRow1Cell3.Width = Unit.Pixel(250);
TableCell ItemTemplateRow1Cell4 = new TableCell();
ItemTemplateRow1Cell4.Width = Unit.Pixel(150);
TableCell ItemTemplateRow1Cell5 = new TableCell();
ItemTemplateRow1Cell5.Width = Unit.Pixel(150);
ImageButton imageButton = new ImageButton();
imageButton.ID = "EditButton";
imageButton.CommandName = "Edit";
imageButton.ImageUrl = "~/ListView/Examples/DefiningStructure/DeclarativeDefinition/Img/Edit.gif";
ItemTemplateRow1Cell1.Controls.Add(imageButton);
Label CustomerID = new Label();
CustomerID.ID = "CustomerID";
CustomerID.DataBinding += new System.EventHandler(CustomerID_DataBinding);
ItemTemplateRow1Cell2.Controls.Add(CustomerID);
Label CompanyName = new Label();
CompanyName.ID = "CompanyName";
CompanyName.DataBinding += new System.EventHandler(CompanyName_DataBinding);
ItemTemplateRow1Cell3.Controls.Add(CompanyName);
Label ContactName = new Label();
ContactName.ID = "ContactName";
ContactName.DataBinding += new System.EventHandler(ContactName_DataBinding);
ItemTemplateRow1Cell4.Controls.Add(ContactName);
Label ContactTitle = new Label();
ContactTitle.ID = "ContactTitle";
ContactTitle.DataBinding += new System.EventHandler(ContactTitle_DataBinding);
ItemTemplateRow1Cell5.Controls.Add(ContactTitle);
ItemTemplateRow1.Cells.Add(ItemTemplateRow1Cell1);
ItemTemplateRow1.Cells.Add(ItemTemplateRow1Cell2);
ItemTemplateRow1.Cells.Add(ItemTemplateRow1Cell3);
ItemTemplateRow1.Cells.Add(ItemTemplateRow1Cell4);
ItemTemplateRow1.Cells.Add(ItemTemplateRow1Cell5);
container.Controls.Add(ItemTemplateRow1);
}
void ContactTitle_DataBinding(object sender, System.EventArgs e)
{
Label ContactTitle = (Label)sender;
ContactTitle.Text = ((ContactTitle.NamingContainer as RadListViewDataItem).DataItem as DataRowView)["ContactTitle"].ToString();
}
void ContactName_DataBinding(object sender, System.EventArgs e)
{
Label ContactName = (Label)sender;
ContactName.Text = ((ContactName.NamingContainer as RadListViewDataItem).DataItem as DataRowView)["ContactName"].ToString();
}
void CompanyName_DataBinding(object sender, System.EventArgs e)
{
Label CompanyName = (Label)sender;
CompanyName.Text = ((CompanyName.NamingContainer as RadListViewDataItem).DataItem as DataRowView)["CompanyName"].ToString();
}
void CustomerID_DataBinding(object sender, System.EventArgs e)
{
Label CustomerID = (Label)sender;
CustomerID.Text = ((CustomerID.NamingContainer as RadListViewDataItem).DataItem as DataRowView)["CustomerID"].ToString();
}
}