030. asp.net中DataList数据绑定跳转(两种方式)的完整示例

时间:2021-08-04 11:11:19

前台代码:

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

<!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" style=" font-size: 9pt">
<div>
<table style="width: 590px; height: 218px;" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 112px; height: 240px">
<asp:DataList ID="DataList1" runat="server" Width="239px" CellPadding="0"
Height="61px" onitemcommand="DataList1_ItemCommand"
onitemdatabound="DataList1_ItemDataBound">
<HeaderTemplate>
<table border="1" cellpadding="0" cellspacing="0" style="width: 300px; text-align: center;">
<tr>
<td colspan="4" style="font-size: 16pt; color: #006600; text-align: center">
分页显示DataList控件中的数据</td>
</tr>
<tr>
<td style="height: 19px; width: 50px; color: #669900;">
编号</td>
<td style="height: 19px; width: 50px; color: #669900;">
姓名</td>
<td style="height: 19px; width: 50px; color: #669900;">
性别</td>
<td style="width: 150px; height: 19px; color: #669900;">
内编号</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table border="1" cellpadding="0" cellspacing="0" style="width: 300px; color: #000000;
text-align: center;">
<tr>
<td style="height: 21px; width: 50px; color: #669900;">
<asp:Label ID="lblStuID" runat="server" Text='<%# Eval("cardNo") %>'></asp:Label></td>
<td style="height: 21px; width: 50px; color: #669900;">
<asp:Label ID="lblStuName" runat="server" Text='<%# Eval("name") %>'></asp:Label></td>
<td style="height: 21px; width: 50px; color: #669900;">
<asp:Label ID="lblStuSex" runat="server" Text='<%# Eval("sex") %>'></asp:Label></td>
<td style="width: 150px; height: 21px; color: #669900;">
<asp:Label ID="lblstuHobby" runat="server" Text='<%# Eval("cardBound") %>'></asp:Label></td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate>
<table style="width: 500px" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 71px; height: 4px" valign="middle">
共有<asp:Label ID="labCount" runat="server" ForeColor="#FF3300" Width="12px"/>页</td>
<td style="width: 73px; height: 4px" valign="middle">
当前<asp:Label ID="labNowPage" runat="server" ForeColor="Brown">1</asp:Label>页</td>
<td style="width: 46px; height: 4px" valign="middle">
<asp:LinkButton ID="lnkbtnFirst" runat="server" CommandName="first"
Font-Underline="False" ForeColor="Black" Width="43px">首页</asp:LinkButton></td>
<td style="width: 55px; height: 4px" valign="middle">
<asp:LinkButton ID="lnkbtnFront" runat="server" CommandName="pre"
Font-Underline="False" ForeColor="Black" Width="62px">上一页</asp:LinkButton></td>
<td style="width: 51px; height: 4px" valign="middle">
<asp:LinkButton ID="lnkbtnNext" runat="server" CommandName="next"
Font-Underline="False" ForeColor="Black" Width="61px">下一页</asp:LinkButton></td>
<td style="width: 29px; height: 4px" valign="middle">
<asp:LinkButton ID="lnkbtnLast" runat="server" Font-Overline="False" CommandName="last"
Font-Underline="False" ForeColor="Black" Width="38px">尾页</asp:LinkButton></td> <%--文本框输入方式实现的页面跳转--%>
<%-- <td style="width: 200px; height: 4px" valign="middle">&nbsp;&nbsp; 跳转至:<asp:TextBox ID="txtPage" runat="server" Width="25px" Height="21px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" CommandName="search" Text="GO"
Height="19px" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtPage" ErrorMessage="请输入数字(除了数值0)"
ValidationExpression="[1-9]+(\d)*"></asp:RegularExpressionValidator>
</td>--%> <%--下拉列表方式实现的页面跳转--%>
<td style="width: 200px; height: 4px" valign="middle">&nbsp;&nbsp; 跳转至:
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
<asp:Button ID="Button1" runat="server" CommandName="search" Text="GO"
Height="19px" />
</td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
</td>
</tr>
<tr>
<td style="width: 112px; height: 12px">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

后台cs代码:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page
{
protected static PagedDataSource ps = new PagedDataSource();//创建一个分页数据源的对象且一定要声明为静态
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();//数据绑定
}
}
//进行数据绑定的方法
public void Bind(int CurrentPage)
{
//实例化SqlConnection对象
SqlConnection sqlCon = new SqlConnection();
//实例化SqlConnection对象连接数据库的字符串
sqlCon.ConnectionString = "server=.;uid=sa;pwd=123.456;database=TYW";
//定义SQL语句
string SqlStr = "select * from card";
//实例化SqlDataAdapter对象
SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon);
//实例化数据集DataSet
DataSet ds = new DataSet();
da.Fill(ds, "cardInfo"); ps.DataSource = ds.Tables["cardInfo"].DefaultView;
ps.AllowPaging = true; //是否可以分页
ps.PageSize = ; //每页显示的条数
ps.CurrentPageIndex = CurrentPage; //取得当前页的页码 this.DataList1.DataSource = ps;
this.DataList1.DataKeyField = "cardNo";
this.DataList1.DataBind();
} protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
switch (e.CommandName)
{
//以下5个为 捕获用户点击 上一页 下一页等时发生的事件
case "first"://第一页
ps.CurrentPageIndex = ;
Bind(ps.CurrentPageIndex);
break;
case "pre"://上一页
ps.CurrentPageIndex = ps.CurrentPageIndex - ;
Bind(ps.CurrentPageIndex);
break;
case "next"://下一页
ps.CurrentPageIndex = ps.CurrentPageIndex + ;
Bind(ps.CurrentPageIndex);
break;
case "last"://最后一页
ps.CurrentPageIndex = ps.PageCount - ;
Bind(ps.CurrentPageIndex);
break;
case "search"://页面跳转页
if (e.Item.ItemType == ListItemType.Footer)
{
int PageCount = int.Parse(ps.PageCount.ToString());
DropDownList ddl = e.Item.FindControl("DropDownList1") as DropDownList;
int MyPageNum = ;
MyPageNum = Convert.ToInt32(ddl.SelectedValue);
if (MyPageNum <= || MyPageNum > PageCount)
Response.Write("<script>alert('请输入页数并确定没有超出总页数!')</script>");
else
Bind(MyPageNum - );
}
break;
}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
//以下六个为得到脚模板中的控件,并创建变量
Label CurrentPage = e.Item.FindControl("labNowPage") as Label;
Label PageCount = e.Item.FindControl("labCount") 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 = (ps.CurrentPageIndex + ).ToString();//绑定显示当前页
PageCount.Text = ps.PageCount.ToString();//绑定显示总页数
if (ps.IsFirstPage)//如果是第一页,首页和上一页不能用
{
FirstPage.Enabled = false;
PrePage.Enabled = false;
}
if (ps.IsLastPage)//如果是最后一页"下一页"和"尾页"按钮不能用
{
NextPage.Enabled = false;
LastPage.Enabled = false;
} DropDownList ddl = e.Item.FindControl("DropDownList1") as DropDownList;
ddl.Items.Clear();
for (int i = ; i <= ps.PageCount; i++)
{
ddl.Items.Add(i.ToString());
}
}
}
}

030. asp.net中DataList数据绑定跳转(两种方式)的完整示例的更多相关文章

  1. c&num;ASP&period;NET中页面传值共有这么几种方式

    一.目前在ASP.NET中页面传值共有这么几种方式: 1.Response.Redirect("http://www.hao123.com",false); 目标页面和原页面可以在 ...

  2. 在asp&period;net中使用confirm可以分为两种&colon;

    在asp.net中使用confirm可以分为两种: 1.没有使用ajax,confirm会引起也面刷新 2.使用了ajax,不会刷新 A.没有使用ajax,可以用StringBuilder来完成. ( ...

  3. 怎样在Android开发中FPS游戏实现的两种方式比较

    怎样在Android开发中FPS游戏实现的两种方式比较 如何用Android平台开发FPS游戏,其实现过程有哪些方法,这些方法又有哪些不同的地方呢?首先让我们先了解下什么是FPS 英文名:FPS (F ...

  4. strus2中获取表单数据 两种方式 属性驱动 和模型驱动

    strus2中获取表单数据 两种方式 属性驱动 和模型驱动 属性驱动 /** * 当前请求的action在栈顶,ss是栈顶的元素,所以可以利用setValue方法赋值 * 如果一个属性在对象栈,在页面 ...

  5. HTML中设置背景图的两种方式

    HTML中设置背景图的两种方式 1.background    background:url(images/search.png) no-repeat top; 2.background-image ...

  6. &lbrack;Android&rsqb; Android ViewPager 中加载 Fragment的两种方式 方式&lpar;二&rpar;

    接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...

  7. &lbrack;Android&rsqb; Android ViewPager 中加载 Fragment的两种方式 方式&lpar;一&rpar;

    Android ViewPager 中加载 Fragmenet的两种方式 一.当fragment里面的内容较少时,直接 使用fragment xml布局文件填充 文件总数 布局文件:view_one. ...

  8. &period;Net 中读写Oracle数据库常用两种方式

    .net中连接Oracle 的两种方式:OracleClient,OleDb转载 2015年04月24日 00:00:24 10820.Net 中读写Oracle数据库常用两种方式:OracleCli ...

  9. 【转】在Android Studio中下载Android SDK的两种方式(Android Studio3&period;0、windows)

    在Android Studio中下载Android SDK的两种方式(Android Studio3.0.windows) 方式一.设置HTTP Proxy1. 打开Settings2. 点击HTTP ...

随机推荐

  1. 机顶盒上gridview&plus;ScrollView的使用。

    最近在机顶盒上做一个gridview, 其焦点需要在item的子控件上,但gridview的焦点默认在item上,通过 android:descendantFocusability="aft ...

  2. Delphi中函数定义和声明的位置

    当函数(或过程)A定义在函数(或过程)B之前,那么函数B就可以调用函数A,并且编译成功,例如下面的 procedure TForm1.btn1Click(Sender: TObject); 和   f ...

  3. HLA中常用的基本术语

    (1)联邦(Federation):用于实现某一特定仿真目的的分布仿真系统. (2)联邦成员(Federate):参与联邦的所有应用都称为联邦成员,简称成员. (3)对象(Object):构成成员的基 ...

  4. github page使用

    github page介绍: https://help.github.com/categories/20/articles 使用GitHub Pages建立博客 与GitHub建立好链接之后,就可以方 ...

  5. Python3基础 使用list&lpar;&rpar; 生成一个空列表

    镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...

  6. 201521123038 《Java程序设计》 第四周学习总结

    201521123038 <Java程序设计> 第四周学习总结 1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 1.通过 ins ...

  7. python rsa 加密

    rsa 非对称加密, 公钥加密, 私钥解密, 有公钥无法推导出私钥, 私钥保密 import rsa n = 1024 # n 越大生成公钥, 秘钥及加密解密所需时间就越长 key = rsa.new ...

  8. 滴滴工程师带你深入理解 TCP 握手分手全过程

      本文作者:饶全成,中科院计算所硕士,滴滴出行后端研发工程师. 个人主页:https://zhihu.com/people/raoquancheng   记得刚毕业找工作面试的时候,经常会被问到:你 ...

  9. mysql mysqld&period;sock文件丢失问题

    修改mysql 编码为utf8时 在/etc/mysql/目录下 在 [client] 添加 default-character-set=utf8 [mysqld]添加 default-charact ...

  10. &lbrack;转&rsqb;Microsoft SQL SERVER 2008 R2 REPORT SERVICE 匿名登录

    本文转自:https://www.cnblogs.com/Zouzhe/p/5736070.html SQL SERVER 2008 R2 是微软目前最新的数据库版本,在之前的SQL SERVER 2 ...