Skip to main content

ASP.net ListView

The following examples give an overview of how to use ListView.

Simple ListView


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JL.aspx.cs" Inherits="JL" %>

<%@ Register Assembly="SystemCore.CommonAppControls" Namespace="SystemCore.CommonAppControls"
TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:PlaceHolder runat="server" ID="test">Id :
<%# Eval("id") %>
<br />
</asp:PlaceHolder>
</ItemTemplate>
</asp:ListView>
</div>
</form>

<script runat="server">
protected override void OnLoad(EventArgs e)
{
ListView1.DataSource = Util.GetData();
ListView1.DataBind();
base.OnLoad(e);
}
</script>

</body>
</html>


Simple ListView With DataPager



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LVPager.aspx.cs" Inherits="LVPager" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@ Register Assembly="SystemCore.CommonAppControls" Namespace="SystemCore.CommonAppControls"
TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:PlaceHolder runat="server" ID="test">Id :
<%# Eval("id") %>
<br />
</asp:PlaceHolder>
</ItemTemplate>
</asp:ListView>
<asp:DataPager runat="server" ID="lvPager" PagedControlID="ListView1" PageSize="3">
<Fields>
<asp:NumericPagerField Visible=true />
</Fields>
</asp:DataPager>
</div>
</form>

<script runat="server">
protected override void OnLoad(EventArgs e)
{
ListView1.DataSource = Util.GetData();
ListView1.DataBind();
base.OnLoad(e);
}
</script>

</body>
</html>


Control to Add DataBinding functionality into your ListView Templates

The key item to note here are adding databinding event and customizing your template namely, ItemTemplate.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Drawing;
using System.Web;

namespace SystemCore.CommonAppControls
{

[ToolboxData("<{0}:JList runat=server>")]
public class JList : Control
{

public ListView lv { get; set; }

public JList()
{
lv = new ListView();
}
protected override void OnInit(EventArgs e)
{
lv.LayoutTemplate = new LayoutTemplate();
lv.ItemTemplate = new ItemTemplate();
lv.EditItemTemplate = new EditItemTemplate();
base.OnInit(e);
}

protected override void CreateChildControls()
{
Controls.Add(lv);
base.CreateChildControls();
}

}

public class LayoutTemplate : Control, ITemplate
{
PlaceHolder pHolder = null;

public LayoutTemplate()
{
pHolder = new PlaceHolder();
}

protected override void OnInit(EventArgs e)
{

base.OnInit(e);
}

protected override void CreateChildControls()
{
base.CreateChildControls();
}

public void InstantiateIn(Control container)
{
pHolder.ID = "itemPlaceholder";
container.Controls.Add(pHolder);
}
}


public class ItemTemplate : Control, ITemplate
{
protected override void CreateChildControls()
{
base.CreateChildControls();
}

public void InstantiateIn(Control container)
{
PlaceHolder p = new PlaceHolder() { ID = "test2" };

Label txtName = new Label();
txtName.Text = "Username:";
txtName.DataBinding += new EventHandler(txtName_DataBinding);

Label Passwrd = new Label();
Passwrd.Text = "Password";
Passwrd.DataBinding += new EventHandler(Passwrd_DataBinding);

p.Controls.Add(txtName);
p.Controls.Add(Passwrd);
container.Controls.Add(p);
}

void txtName_DataBinding(object sender, EventArgs e)
{
}

void Passwrd_DataBinding(object sender, EventArgs e)
{

IDataItemContainer d = (IDataItemContainer)((Label)sender).NamingContainer;
var x = (string)DataBinder.Eval(d.DataItem, "id").ToString();

((Label)sender).Text = (String)DataBinder.Eval(((IDataItemContainer)((Label)sender).NamingContainer).DataItem, "id").ToString();
}
}

public class EditItemTemplate : Control, ITemplate
{
protected override void CreateChildControls()
{
this.Controls.Add(new PlaceHolder() { ID = "test" });
TextBox txtName = new TextBox();
txtName.Text = "Username";
TextBox Passwrd = new TextBox();
Passwrd.Text = "Password";
this.Controls.Add(new Label() { Text = "Name" });
Controls.Add(txtName);
this.Controls.Add(new Label() { Text = "Password" });
Controls.Add(Passwrd);

base.CreateChildControls();
}

#region ITemplate Members

public void InstantiateIn(Control container)
{
// throw new NotImplementedException();
}

#endregion
}

}




Comments

Popular posts from this blog

Android Programmatically apply style to your view

Applying style to your view (button in this case) dynamically is pretty easy. All you have to do is place the following in your layout folder (res/layout)
Let's call this file : buttonstyle.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" > <shape> <solid android:color="#449def" /> <stroke android:width="1dp" android:color="#2f6699" /> <corners android:radius="3dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item> <shape> <gradient android:startColor="#449def" a…

OpenCover code coverage for .Net Core

I know there are many post out there getting code coverage for .dotnetcore. I'm using opencover to address this needs.

In case, you do no want to use opencover and wanted to stick with vs2015 code coverage, you can try to copy Microsoft.VisualStudio.CodeCoverage.Shim.dll from C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Dynamic Code Coverage Tools\coreclr\ and drop it into your project "bin\Debug\netcoreapp1.0" folder.  Please note : you need to be on VS2015 Enterprise to do this. 

To get started, I guess we need to add OpenCover and ReportGenerator for our test projects, as shown in diagram below :-



When nuget packge gets restored, we will have some binaries downloaded to our machine and we going to use this to generate some statistics. I think the biggest issue is to getting those command lines work.

In dotnetcore, we run test project using "dotnet test" (assuming you are in the test project folder - if not please go there)  So we add this …

DataTable does not have AsEnumerable

I have problem locating my AsEnumerable extension method in my DataTabe (System.Data). Thank god for this post by Angel
(http://blogs.msdn.com/angelsb/archive/2007/02/23/does-not-contain-a-definition-for.aspx)

I was able to find this method once i have added reference to the following assembly.

C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.DataSetExtensions.dll

Try to do a dummy Build and you should be able to get it.