
·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 初始ASP.NET数据控件【续DataList】
DataList控件
DataList控件也是一个常用的数据绑定控件,相对于GridView控件虽然没它那么强大的功能,但是灵活性却很强势。因为其本身就是一个富有弹性的控件。DataList控件可以使用模版与定义样式来显示数据,并可以经行数据选择,删除以及编辑。DataList支持的模版有一下介绍:
下面实验一个用SQL语句从数据库中查询数据的功能。
先创建一个asp.net的网站,然后在页面中添加一个DataList控件,用来显示分页数据。
然后编辑我的DataList控件步骤如下:
编辑ItemTemplate选项,具体代码如下:
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<table>
<tr style="border-bottom-style: groove; border-bottom-width: medium; border-bottom-color: #FFFFFF">
<td rowspan="3" align="center" class="style3">
<a href='#'></a>
</td>
<td align="left">
留言人:
<a></a>
</td>
<td align="left">
</td>
<td>
</td>
</tr>
<tr>
<td align="left">
空间主人:<a></a></td>
<td align="left">
--创建时间:<a></a></td>
<td>
</td>
</tr>
<tr>
<td align="left" colspan="3">
个性签名:<a ></a></td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate>
<div style="text-align: center">
<table id="Page" border="1" cellpadding="0" cellspacing="0"
style="font-size: 12px; width: 68%">
<tr>
<td >
<asp:Label ID="labCurrentPage" runat="server"></asp:Label>/
<asp:Label ID="labPageCount" runat="server"></asp:Label>
<asp:LinkButton ID="lnkbtnFirst" runat="server" CommandName="first" Font-Underline="False"
ForeColor="Black">首页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnFront" runat="server" CommandName="PRe" Font-Underline="False"
ForeColor="Black">上一页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnNext" runat="server" CommandName="next" Font-Underline="False"
ForeColor="Black">下一页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnLast" runat="server" CommandName="last" Font-Underline="False"
ForeColor="Black">尾页</asp:LinkButton>
跳转至:<asp:TextBox ID="txtPage" runat="server" Width="35px" Height="21px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" CommandName="search" Text="GO"
Height="19px" />
<br />
</td>
</tr>
</table>
</div>
</FooterTemplate>
</asp:DataList>
先看我们的数据库里的测试数据:
,然后我们需要搞一个数据分页的功能,并且将分页的数据绑定到DataList空间上。
具体代码如:
/// <summary>
/// 实现数据分页
/// </summary>
/// <param name="currentpage"></param>
private void BindDataList(int currentpage)
{
pds.AllowPaging = true; //允许分页
pds.PageSize = 4; //每页4条数据
pds.CurrentPageIndex = currentpage; //当前页为传人的一个int类型
string querySql = string.Format("SELECT * FROM dbo.Message");
connection.Open();
SqlDataAdapter sda = new SqlDataAdapter(querySql, connection);
DataSet ds = new DataSet();
//执行得到的数据放在数据集中
sda.Fill(ds);
//数据集中的数据放入到分页数据中
pds.DataSource = ds.Tables[0].DefaultView;
this.DataList1.DataSource = pds;
this.DataList1.DataBind();
connection.Close();
}
然后我们需要触发DataList的ItemDataBound事件来显示控件中当前的页码和总页码。然后设置分页按钮是否可用的状态:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
switch (e.CommandName)
{
//以下5个为 捕获用户点击 上一页 下一页等时发生的事件
case "first"://第一页
pds.CurrentPageIndex = 0;
BindDataList(pds.CurrentPageIndex);
break;
case "pre"://上一页
pds.CurrentPageIndex = pds.CurrentPageIndex - 1;
BindDataList(pds.CurrentPageIndex);
break;
case "next"://下一页
pds.CurrentPageIndex = pds.CurrentPageIndex + 1;
BindDataList(pds.CurrentPageIndex);
break;
case "last"://最后一页
pds.CurrentPageIndex = pds.PageCount - 1;
BindDataList(pds.CurrentPageIndex);
break;
case "search"://页面跳转页
if (e.Item.ItemType == ListItemType.Footer)
{
int PageCount = int.Parse(pds.PageCount.ToString());
TextBox txtPage = e.Item.FindControl("txtPage") as TextBox;
int MyPageNum = 0;
if (!txtPage.Text.Equals(""))
MyPageNum = Convert.ToInt32(txtPage.Text.ToString());
if (MyPageNum <= 0 || MyPageNum > PageCount)
Response.Write("<script>alert('请输入页数并确定没有超出总页数!')</script>");
else
BindDataList(MyPageNum - 1);
}
break;
}
}
然后我们继续对DataList控件的ItemCommand事件,在该事件中,主要针对“首页/上一页/下一页/末页”分页按钮被点击时,以及在文本框输入页数跳转功能的实现简单操作:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
//以下六个为得到脚模板中的控件,并创建变量.
Label CurrentPage = e.Item.FindControl("labCurrentPage") as Label;
Label PageCount = e.Item.FindControl("labPageCount") as Label;
LinkButton FirstPage = e.Item.FindControl("lnkbtnFirst") as LinkButton;
LinkButton PrePage = e.Item.FindControl("lnkbtnFront") as LinkButton;
LinkButton NextPage = e.Item.FindControl("lnkbtnNext") as LinkButton;
LinkButton LastPage = e.Item.FindControl("lnkbtnLast") as LinkButton;
CurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();//绑定显示当前页
PageCount.Text = pds.PageCount.ToString();//绑定显示总页数
if (pds.IsFirstPage)//如果是第一页,首页和上一页不能用
{
FirstPage.Enabled = false;
PrePage.Enabled = false;
}
if (pds.IsLastPage)//如果是最后一页"下一页"和"尾页"按钮不能用
{
NextPage.Enabled = false;
LastPage.Enabled = false;
}
}
}
完全的代码如下:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace DataListApplaction
{
public partial class DataListDemo : System.Web.UI.Page
{
static PagedDataSource pds = new PagedDataSource(); //创建一个分页数据源的对象且一定要声明为静态
//连接字符串
SqlConnection connection = new SqlConnection(@"Data Source=.\CENA;Initial Catalog=Asp.net;Persist Security Info=True;User ID=sa;PassWord=123456");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataList(0);
}
}
/// <summary>
/// 实现数据分页
/// </summary>
/// <param name="currentpage"></param>
private void BindDataList(int currentpage)
{
pds.AllowPaging = true; //允许分页
pds.PageSize = 4; //每页4条数据
pds.CurrentPageIndex = currentpage; //当前页为传人的一个int类型
string querySql = string.Format("SELECT * FROM dbo.Message");
connection.Open();
SqlDataAdapter sda = new SqlDataAdapter(querySql, connection);
DataSet ds = new DataSet();
//执行得到的数据放在数据集中
sda.Fill(ds);
//数据集中的数据放入到分页数据中
pds.DataSource = ds.Tables[0].DefaultView;
this.DataList1.DataSource = pds;
this.DataList1.DataBind();
connection.Close();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
switch (e.CommandName)
{
//以下5个为 捕获用户点击 上一页 下一页等时发生的事件
case "first"://第一页
pds.CurrentPageIndex = 0;
BindDataList(pds.CurrentPageIndex);
break;
case "pre"://上一页
pds.CurrentPageIndex = pds.CurrentPageIndex - 1;
BindDataList(pds.CurrentPageIndex);
break;
case "next"://下一页
pds.CurrentPageIndex = pds.CurrentPageIndex + 1;
BindDataList(pds.CurrentPageIndex);
break;
case "last"://最后一页
pds.CurrentPageIndex = pds.PageCount - 1;
BindDataList(pds.CurrentPageIndex);
break;
case "search"://页面跳转页
if (e.Item.ItemType == ListItemType.Footer)
{
int PageCount = int.Parse(pds.PageCount.ToString());
TextBox txtPage = e.Item.FindControl("txtPage") as TextBox;
int MyPageNum = 0;
if (!txtPage.Text.Equals(""))
MyPageNum = Convert.ToInt32(txtPage.Text.ToString());
if (MyPageNum <= 0 || MyPageNum > PageCount)
Response.Write("<script>alert('请输入页数并确定没有超出总页数!')</script>");
else
BindDataList(MyPageNum - 1);
}
break;
}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
//以下六个为得到脚模板中的控件,并创建变量.
Label CurrentPage = e.Item.FindControl("labCurrentPage") as Label;
Label PageCount = e.Item.FindControl("labPageCount") as Label;
LinkButton FirstPage = e.Item.FindControl("lnkbtnFirst") as LinkButton;
LinkButton PrePage = e.Item.FindControl("lnkbtnFront") as LinkButton;
LinkButton NextPage = e.Item.FindControl("lnkbtnNext") as LinkButton;
LinkButton LastPage = e.Item.FindControl("lnkbtnLast") as LinkButton;
CurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();//绑定显示当前页
PageCount.Text = pds.PageCount.ToString();//绑定显示总页数
if (pds.IsFirstPage)//如果是第一页,首页和上一页不能用
{
FirstPage.Enabled = false;
PrePage.Enabled = false;
}
if (pds.IsLastPage)//如果是最后一页"下一页"和"尾页"按钮不能用
{
NextPage.Enabled = false;
LastPage.Enabled = false;
}
}
}
}
}
View Code
写完程序运行结果如下:
关于DataList就先了解到这里,下面在列举一下DataList常用的事件
| 事件 | 说明 |
| CancelCommand | 对DataList控件中的某项单击Cancel按钮时发生 |
| DeleteCommand | 对DataList控件中的某项单击Delete按钮时发生 |
| EditCommand | 对DataList控件中的某项单击Edit按钮时发生 |
| ItemCommand | 当单击DataList控件的任意按钮时发生 |
| ItemDataBound | 当项被数据绑定到DataList控件时发生 |
| SelectedIndexChanged | 在两次服务发送之间,在数据列表控件中选择了不同的项时发生 |
| UpdateCommand | 对DataList控件中的某项单击Update时发生 |