初始ASP.NET数据控件【续DataList】

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

DataList控件

DataList控件也是一个常用的数据绑定控件,相对于GridView控件虽然没它那么强大的功能,但是灵活性却很强势。因为其本身就是一个富有弹性的控件。DataList控件可以使用模版与定义样式来显示数据,并可以经行数据选择,删除以及编辑。DataList支持的模版有一下介绍:

AlternatingItemTemplate:如果已经定义,DataList中的交替项提供内容和布局;如果未定义则使用ItemTemplateEditItemTemplate:如果已定义,DataList中的当前编辑项提供内容和布局;如果未定义则使用ItemTemplateFoterTemplate:如果已定义,DataList中的当前脚注部分提供内容和布局;如果未定义将不显示脚注部分HeaderTemplate:如果已定义,DataList中的当前页眉部分提供内容和布局;如果未定义将不显示页眉ItemTemplate:DataList中提供内容和布局所要求的模版SelectedItemTemplate:如果定义则为DataList当前选项提供内容和布局;如果未定义则使用ItemTemplateSeparatorTemplate:如果定义则为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">

&nbsp;</td>

<td>

&nbsp;</td>

</tr>

<tr>

<td align="left">空间主人:<a></a></td>

<td align="left">

--创建时间:<a></a></td>

<td>

&nbsp;</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>

&nbsp;&nbsp; 跳转至:<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;

}

}

}

完全的代码如下:

usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespaceDataListApplaction

{publicpartialclassDataListDemo : System.Web.UI.Page

{staticPagedDataSource pds =newPagedDataSource();//创建一个分页数据源的对象且一定要声明为静态//连接字符串SqlConnection connection =newSqlConnection(@"Data Source=.\CENA;Initial Catalog=Asp.net;Persist Security Info=True;User ID=sa;PassWord=123456");protectedvoidPage_Load(objectsender, EventArgs e)

{if(!IsPostBack)

{

BindDataList(0);

}

}///<summary>///实现数据分页///</summary>///<param name="currentpage"></param>privatevoidBindDataList(intcurrentpage)

{

pds.AllowPaging=true;//允许分页pds.PageSize =4;//每页4条数据pds.CurrentPageIndex = currentpage;//当前页为传人的一个int类型stringquerySql =string.Format("SELECT * FROM dbo.Message");

connection.Open();

SqlDataAdapter sda=newSqlDataAdapter(querySql, connection);

DataSet ds=newDataSet();//执行得到的数据放在数据集中sda.Fill(ds);//数据集中的数据放入到分页数据中pds.DataSource = ds.Tables[0].DefaultView;this.DataList1.DataSource =pds;this.DataList1.DataBind();

connection.Close();

}protectedvoidDataList1_ItemCommand(objectsource, 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)

{intPageCount =int.Parse(pds.PageCount.ToString());

TextBox txtPage= e.Item.FindControl("txtPage")asTextBox;intMyPageNum =0;if(!txtPage.Text.Equals(""))

MyPageNum=Convert.ToInt32(txtPage.Text.ToString());if(MyPageNum <=0|| MyPageNum >PageCount)

Response.Write("<script>alert('请输入页数并确定没有超出总页数!')</script>");elseBindDataList(MyPageNum-1);

}

break;

}

}protectedvoidDataList1_ItemDataBound(objectsender, DataListItemEventArgs e)

{if(e.Item.ItemType ==ListItemType.Footer)

{//以下六个为得到脚模板中的控件,并创建变量.Label CurrentPage = e.Item.FindControl("labCurrentPage")asLabel;

Label PageCount= e.Item.FindControl("labPageCount")asLabel;

LinkButton FirstPage= e.Item.FindControl("lnkbtnFirst")asLinkButton;

LinkButton PrePage= e.Item.FindControl("lnkbtnFront")asLinkButton;

LinkButton NextPage= e.Item.FindControl("lnkbtnNext")asLinkButton;

LinkButton LastPage= e.Item.FindControl("lnkbtnLast")asLinkButton;

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时发生

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航