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

ionic2 cordova build android - Unable resolve gradle 2.2.3

OpenCover code coverage for .Net Core

Using Custom DLL with IronPython / Scripts