datalist控件没有内置的分页功能,所以对于分页需自己写程序去实现;本示例还包括了DataList的所有基本操作,如添加,删除,修改,选择等;具体代码如下:
前台代码:Default.aspx
----------------------------------------
<%@ Page language="c#" Codebehind="Default.aspx.cs" AutoEventWireup="false" Inherits="Example.DataListTest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataListTest</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<style type="text/css">TD { FONT-SIZE: 12px; COLOR: #000000; FONT-FAMILY: "宋体" }
</style>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr>
<td><a href="/DlNested.aspx">DataList嵌套实例</a></td>
</tr>
<tr>
<td>
<table cellSpacing="0" borderColorDark="white" cellPadding="0" width="100%" align="center"
border="1">
<tr>
<td><asp:datalist id="DataList1" runat="server" Width="90%" Height="280px" RepeatColumns="2" RepeatDirection="Horizontal"
RepeatLayout="Flow">
<headertemplate>
<table id="Table6" cellspacing="0" bordercolordark="white" cellpadding="0" width="100%"
border="1">
<tr>
<td width="80%"><font face="宋体">TypeName</font></td>
<td width="50"><font face="宋体">选择</font></td>
<td width="50"><font face="宋体">确定</font></td>
<td width="50"><font face="宋体">删除</font></td>
</tr>
</table>
</headertemplate>
<selecteditemtemplate>
<font face="宋体">
<table id="Table3" cellspacing="0" cellpadding="0" width="100%" border="1" bordercolordark="white">
<tr>
<td><font face="宋体">TypeID</font></td>
<td><font face="宋体" color="#ff3366"><font face="宋体">TypeName</font></font></td>
</tr>
<tr>
<td style="HEIGHT: 21px"><font face="宋体">
<asp:Label id=lblTypeID runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "I_TypeID") %>'>
</asp:Label></font></td>
<td style="HEIGHT: 21px"><font face="宋体" color="#ff3366">
<asp:Label id=lblTypeName runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "C_TypeName") %>'>
</asp:Label></font></td>
</tr>
<tr>
<td colspan="2">
<asp:Button id="btnBack" runat="server" CommandName="cancel" Text="返回"></asp:Button></td>
</tr>
</table>
</font>
</selecteditemtemplate>
<footertemplate>
<table id="Table8" cellspacing="0" bordercolordark="white" cellpadding="0" width="100%"
border="1">
<tr>
<td><font face="宋体">TypeID</font></td>
<td>
<asp:TextBox id="txtTypeIDInsert" runat="server"></asp:TextBox></td>
<td><font face="宋体">TypeName</font></td>
<td>
<asp:TextBox id="txtTypeNameInsert" runat="server"></asp:TextBox></td>
<td align="center">
<asp:Button id="btnInsert" runat="server" Text="新增" CommandName="Insert"></asp:Button></td>
</tr>
</table>
</footertemplate>
<itemtemplate>
<table cellspacing="0" cellpadding="0" width="100%" border="1" bordercolordark="white">
<tr>
<td width="80%"><font face="宋体" color="red">
<%# DataBinder.Eval(Container.DataItem,"C_TypeName")%>
</font>
</td>
<td width="50">
<asp:Button id="btnSelect" runat="server" CommandName="select" Text="选择"></asp:Button></td>
<td width="50">
<asp:Button id="btnModify" runat="server" CommandName="edit" Text="编辑" EnableViewState="true"></asp:Button></td>
<td width="50"><font face="宋体">
<asp:Button id=btnDelete runat="server" CommandName="delete" Text="删除" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "I_TypeID") %>'>
</asp:Button></font></td>
</tr>
</table>
</itemtemplate>
<edititemtemplate>
<table id="Table2" cellspacing="0" cellpadding="0" width="100%" border="1">
<tr>
<td style="WIDTH: 75px; HEIGHT: 23px"><font face="宋体"><font face="宋体">TypeName</font></font></td>
<td style="HEIGHT: 23px"><font face="宋体" color="#ff3366">
<asp:TextBox id=txtTypeName runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "C_TypeName") %>'>
</asp:TextBox></font></td>
</tr>
<tr>
<td style="WIDTH: 75px"><font face="宋体">
<asp:Button id=btnUpdate runat="server" CommandName="update" Text="更新" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "I_TypeID") %>'>
</asp:Button></font></td>
<td><font face="宋体" color="#ff3366">
<asp:Button id="btnCancle" runat="server" CommandName="cancel" Text="取消"></asp:Button></font></td>
</tr>
</table>
</edititemtemplate>
</asp:datalist></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<TABLE cellSpacing="0" borderColorDark="white" cellPadding="0" width="100%" border="1">
<TR align="center">
<TD noWrap width="10%">共
<asp:label id="lblRecNum" runat="server"></asp:label>条记录</TD>
<TD noWrap width="30%">第
<asp:label id="lblCurPageNO" runat="server"></asp:label>页/共
<asp:label id="lblPageNum" runat="server"></asp:label>页</TD>
<TD noWrap width="5%"><asp:button id="btnFirst" runat="server" Text="首页"></asp:button></TD>
<TD noWrap width="5%"><asp:button id="btnPreview" runat="server" Text="上页"></asp:button></TD>
<TD noWrap width="5%"><asp:button id="btnNext" runat="server" Text="下页"></asp:button></TD>
<TD noWrap width="5%"><asp:button id="btnLast" runat="server" Text="末页"></asp:button></TD>
<TD noWrap width="20%">每页<asp:dropdownlist id="ddlPageRowNum" runat="server" AutoPostBack="True"></asp:dropdownlist>行</TD>
<TD noWrap width="20%">转到
<asp:dropdownlist id="ddlJumpToPageNum" runat="server" AutoPostBack="True"></asp:dropdownlist>页</TD>
</TR>
</TABLE>
</td>
</tr>
</table>
</form>
</body>
</HTML>
后台代码:Default.aspx.cs
----------------------------------------
/*----------------------------------------------------------------
// 版权所有: 亿思维软件工作室
// 网站地址: Http://www.easewe.com
// 联系方式: 87015203(QQ),easewe@126.com(Email)
// 整理日期: 2005.09.14
//----------------------------------------------------------------*/
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
namespace Example
{
/// <summary>
/// DataListTest 的摘要说明。
/// </summary>
public class DataListTest : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList DataList1;
protected System.Web.UI.WebControls.Label lblRecNum;
protected System.Web.UI.WebControls.Label lblCurPageNO;
protected System.Web.UI.WebControls.Label lblPageNum;
protected System.Web.UI.WebControls.Button btnFirst;
protected System.Web.UI.WebControls.Button btnPreview;
protected System.Web.UI.WebControls.Button btnNext;
protected System.Web.UI.WebControls.Button btnLast;
protected System.Web.UI.WebControls.DropDownList ddlPageRowNum;
protected System.Web.UI.WebControls.DropDownList ddlJumpToPageNum;
protected string connString;
protected int pageNum = -1;
protected int recNum = -1;
protected int pageRecNum = -1;
protected int curPageNO = -1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
connString = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Request.PhysicalApplicationPath + "DataList.mdb";
if ( !Page.IsPostBack )
{
//-----------recNum
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = connString;
conn.Open();
OleDbCommand cmd = new OleDbCommand("select count(*) from ArticleType",conn);
recNum = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
//-----------pageRecNum
ddlPageRowNum.Items.Clear();
for( int i=1;i<=recNum;i++ )
{
ddlPageRowNum.Items.Add(i.ToString());
}
if ( recNum < 5 )
{
ddlPageRowNum.SelectedIndex = recNum - 1;
}
else
{
ddlPageRowNum.SelectedIndex = 4;
}
//------------------pageRecNum,pageNum
pageRecNum = Convert.ToInt32(ddlPageRowNum.SelectedValue);
if ( Convert.ToInt32(recNum % pageRecNum) == 0 )
{
pageNum = Convert.ToInt32(recNum/pageRecNum);
}
else
{
pageNum = Convert.ToInt32(recNum/pageRecNum) + 1;
}
ddlJumpToPageNum.Items.Clear();
for( int i=1;i<=pageNum;i++ )
{
ddlJumpToPageNum.Items.Add(i.ToString());
}
lblPageNum.Text = pageNum.ToString();
ViewState["PageRowNum"] = pageRecNum;
ViewState["RecNum"] = recNum;
ViewState["PageNum"] = pageNum;
//-----------------curPageNO
curPageNO = 1;
ViewState["CurPageNO"] = curPageNO;
//------------------
}
pageRecNum = Convert.ToInt32(ViewState["PageRowNum"]);
recNum = Convert.ToInt32(ViewState["RecNum"]);
curPageNO = Convert.ToInt32(ViewState["CurPageNO"]);
pageNum = Convert.ToInt32(ViewState["PageNum"]);
//------------pageNum
if ( pageRecNum==-1 || recNum==-1 )
{
Response.Write("发生错误!");
Response.End();
}
if ( curPageNO <= 1 )
{
curPageNO = 1;
btnFirst.Enabled = false;
btnPreview.Enabled = false;
}
//-----------------
lblRecNum.Text = recNum.ToString();
lblCurPageNO.Text = curPageNO.ToString();
//-----------------
if ( !Page.IsPostBack )
{
BindData();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataList1.ItemCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.DataList1_ItemCommand);
this.DataList1.CancelCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.DataList1_CancelCommand);
this.DataList1.EditCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.DataList1_EditCommand);
this.DataList1.UpdateCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.DataList1_UpdateCommand);
this.DataList1.DeleteCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.DataList1_DeleteCommand);
this.DataList1.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(this.DataList1_ItemDataBound);
this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click);
this.btnPreview.Click += new System.EventHandler(this.btnPreview_Click);
this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
this.btnLast.Click += new System.EventHandler(this.btnLast_Click);
this.ddlPageRowNum.SelectedIndexChanged += new System.EventHandler(this.ddlPageRowNum_SelectedIndexChanged);
this.ddlJumpToPageNum.SelectedIndexChanged += new System.EventHandler(this.ddlJumpToPageNum_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
#region datelist
private void BindData()
{
string commandText = "select * from ArticleType";
//----------
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = connString;
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(commandText,conn);
DataSet ds = new DataSet();
da.Fill(ds,(curPageNO-1) * pageRecNum,pageRecNum,"ArticleType");
//----------
DataList1.DataSource = ds.Tables["ArticleType"];
DataList1.DataBind();
ds.Clear();
conn.Close();
lblCurPageNO.Text = curPageNO.ToString();
}
private void DataList1_EditCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
BindData();
}
private void DataList1_DeleteCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
string typeID = e.CommandArgument.ToString();
string commandText = "Delete From ArticleType Where I_TypeID=@TypeID";
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = connString;
conn.Open();
OleDbCommand cmd = new OleDbCommand(commandText,conn);
OleDbParameter[] param = new OleDbParameter[1];
param[0] = new OleDbParameter("TypeID",typeID);
cmd.Parameters.Add(param[0]);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
conn.Close();
//----------------------
recNum --;
lblRecNum.Text = recNum.ToString();
//-----------pageRecNum
ddlPageRowNum.Items.Clear();
for( int i=1;i<=recNum;i++ )
{
ddlPageRowNum.Items.Add(i.ToString());
}
ddlPageRowNum.SelectedValue = ViewState["PageRowNum"].ToString();
//------------------pageRecNum,pageNum
pageRecNum = Convert.ToInt32(ddlPageRowNum.SelectedValue);
if ( Convert.ToInt32(recNum % pageRecNum) == 0 )
{
pageNum = Convert.ToInt32(recNum/pageRecNum);
}
else
{
pageNum = Convert.ToInt32(recNum/pageRecNum) + 1;
}
ddlJumpToPageNum.Items.Clear();
for( int i=1;i<=pageNum;i++ )
{
ddlJumpToPageNum.Items.Add(i.ToString());
}
lblPageNum.Text = pageNum.ToString();
lblCurPageNO.Text = pageNum.ToString();
ViewState["PageRowNum"] = pageRecNum;
ViewState["RecNum"] = recNum;
ViewState["PageNum"] = pageNum;
//-----------------curPageNO
curPageNO = Convert.ToInt32(ViewState["CurPageNO"]);
if ( curPageNO>pageNum )
{
curPageNO = pageNum;
}
ddlJumpToPageNum.SelectedValue = "1";
ViewState["CurPageNO"] = curPageNO;
//-----------------
if ( curPageNO == 1 )
{
btnFirst.Enabled = false;
btnPreview.Enabled = false;
}
if ( curPageNO == pageNum )
{
btnNext.Enabled = false;
btnLast.Enabled = false;
}
//----------
DataList1.EditItemIndex = -1;
DataList1.SelectedIndex = -1;
//----------------------
BindData();
}
private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
{
Button btnDel = (Button)e.Item.FindControl("btnDelete");
btnDel.Attributes.Add("onclick","return confirm('确定要删除[" + DataBinder.Eval(e.Item.DataItem,"C_TypeName") + "]吗?')");
e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='#cccc00'");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor='white'");
}
}
private void DataList1_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
if ( e.CommandName == "select" )
{
DataList1.SelectedIndex = e.Item.ItemIndex;
BindData();
}
if ( e.CommandName == "Insert" )
{
string typeID = ((TextBox)e.Item.FindControl("txtTypeIDInsert")).Text.ToString();
string typeName = ((TextBox)e.Item.FindControl("txtTypeNameInsert")).Text.ToString();
//----------
string commandText = "Insert Into ArticleType(C_TypeName,I_TypeID)";
commandText = commandText + " Values(@TypeName,@TypeID)";
//-----------
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = connString;
conn.Open();
OleDbCommand cmd = new OleDbCommand(commandText,conn);
OleDbParameter[] param = new OleDbParameter[2];
param[0] = new OleDbParameter("TypeName",typeName);
param[1] = new OleDbParameter("TypeID",typeID);
cmd.Parameters.Add(param[0]);
cmd.Parameters.Add(param[1]);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
conn.Close();
//----------------------
recNum ++;
lblRecNum.Text = recNum.ToString();
//-----------pageRecNum
ddlPageRowNum.Items.Clear();
for( int i=1;i<=recNum;i++ )
{
ddlPageRowNum.Items.Add(i.ToString());
}
ddlPageRowNum.SelectedValue = ViewState["PageRowNum"].ToString();
//------------------pageRecNum,pageNum
pageRecNum = Convert.ToInt32(ddlPageRowNum.SelectedValue);
if ( Convert.ToInt32(recNum % pageRecNum) == 0 )
{
pageNum = Convert.ToInt32(recNum/pageRecNum);
}
else
{
pageNum = Convert.ToInt32(recNum/pageRecNum) + 1;
}
ddlJumpToPageNum.Items.Clear();
for( int i=1;i<=pageNum;i++ )
{
ddlJumpToPageNum.Items.Add(i.ToString());
}
lblPageNum.Text = pageNum.ToString();
lblCurPageNO.Text = pageNum.ToString();
ViewState["PageRowNum"] = pageRecNum;
ViewState["RecNum"] = recNum;
ViewState["PageNum"] = pageNum;
//-----------------curPageNO
curPageNO = pageNum;
ddlJumpToPageNum.SelectedValue = "1";
ViewState["CurPageNO"] = curPageNO;
//-----------------
btnFirst.Enabled = true;
btnPreview.Enabled = true;
btnNext.Enabled = false;
btnLast.Enabled = false;
//----------
DataList1.EditItemIndex = -1;
DataList1.SelectedIndex = -1;
BindData();
}
}
private void DataList1_CancelCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
DataList1.SelectedIndex = -1;
DataList1.EditItemIndex = -1;
BindData();
}
private void DataList1_UpdateCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
string typeID = e.CommandArgument.ToString();
string typeName = ((TextBox)e.Item.FindControl("txtTypeName")).Text.ToString();
//----------
string commandText = "Update ArticleType Set C_TypeName=@TypeName";
commandText = commandText + " Where I_TypeID=@TypeID";
//-----------
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = connString;
conn.Open();
&n