想做点东西就准备哈基础知识,仔细分析了哈这2个控件发现功能强大,希望微软有一天将这2个控件结合开发出一个控件就好了,嘿嘿.但是各有千秋.要想实现分页首选就是gridview,但是无法实现模板编辑后进行行和列的控制,那就用datelist但是没有分页,datelist可以轻松得到效果,但是没有分页,怎么半,不用怕,嘿嘿有我,花了2天写了个代码比网上的简单多了哈哈,终于写完了可以吃饭烙好开心哦.希望大家顶下这个代码,介绍给朋友,页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>
<!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:DataList ID="DataList1" runat="server" RepeatColumns="2" RepeatDirection="Horizontal">
<ItemTemplate>
<table>
<tr>
<td style="width: 220px; height: 91px">
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Bind("qq") %>' /></td>
</tr>
<tr>
<td style="width: 220px; height: 14px">
<asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<br />
<table style="width: 630px">
<tr>
<td style="width: 100px">
总共<asp:Label ID="lblPagecount" runat="server"></asp:Label>页
</td>
<td style="width: 100px">
<asp:LinkButton ID="lbtnLast" runat="server"onClick="lbtnLast_Click">上一页</asp:LinkButton></td>
<td style="width: 100px">
<asp:LinkButton ID="lbtnNext" runat="server"onClick="lbtnNext_Click">下一页</asp:LinkButton></td>
<td style="width: 100px">
<asp:DropDownList ID="ddlPageIndex" runat="server"onSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:Label ID="lblIndex" runat="server" Text="1" Visible="False"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</html>
页面的cs代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Default5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.bind();
}
}
protected void bind()
{
int pageIndex = Convert.ToInt32(lblIndex.Text);
int count = db.sele("select count(*) from table2");
PagedDataSource ps = new PagedDataSource();
ps.DataSource = db.dt("select * from table2").DefaultView;
ps.AllowPaging = true;
ps.PageSize = 6;
ps.CurrentPageIndex = pageIndex - 1;
//lblPagecount.Text = Convert.ToString(count/ps.PageSize+1);
if (!IsPostBack)
{
for (int i = 1; i <= ps.PageCount; i++)
{
ddlPageIndex.Items.Add(i.ToString());
}
}
//ddlPageIndex.SelectedItem.Text = pageIndex.ToString();
lbtnLast.Enabled = true;
lbtnNext.Enabled = true;
if (ps.IsFirstPage)
{
lbtnLast.Enabled = false;
}
if (ps.IsLastPage)
{
lbtnNext.Enabled = false;
}
lblPagecount.Text = Convert.ToString(ps.PageCount);
ddlPageIndex.SelectedItem.Text = pageIndex.ToString();
this.DataList1.DataSource = ps;
this.DataList1.DataBind();
}
protected void lbtnLast_Click(object sender, EventArgs e)
{
lblIndex.Text = Convert.ToString(Convert.ToInt32(lblIndex.Text) - 1);
ddlPageIndex.SelectedValue = lblIndex.Text;
this.bind();
}
protected void lbtnNext_Click(object sender, EventArgs e)
{
lblIndex.Text = Convert.ToString(Convert.ToInt32(lblIndex.Text) + 1);
ddlPageIndex.SelectedValue = lblIndex.Text;
this.bind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
lblIndex.Text = ddlPageIndex.SelectedItem.Text;
this.bind();
}
}
创建了一个类:
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;
/// <summary>
/// db 的摘要说明
/// </summary>
public class db
{
public db()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static SqlConnection con()
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=gouwu;Integrated Security=True");
return con;
}
public static DataTable dt(string que)
{
SqlConnection con = db.con();
con.Open();
SqlDataAdapter olda = new SqlDataAdapter();
olda.SelectCommand = new SqlCommand(que, con);
DataSet ds = new DataSet();
olda.Fill(ds, "aa");
return ds.Tables["aa"];
con.Close();
}
public static bool insert(string que)
{
SqlConnection con = db.con();
con.Open();
SqlCommand cmd = new SqlCommand(que, con);
int count = Convert.ToInt32(cmd.ExecuteNonQuery());
if (count > 0)
{
return true;
}
else
{
return false;
}
con.Close();
}
public static string sel(string que)
{
SqlConnection con = db.con();
con.Open();
SqlCommand cmd = new SqlCommand(que, con);
return Convert.ToString(cmd.ExecuteScalar());
con.Close();
}
public static int sele(string que)
{
SqlConnection con = db.con();
con.Open();
SqlCommand cmd = new SqlCommand(que, con);
return Convert.ToInt32(cmd.ExecuteScalar());
}
}
转载于:https://www.cnblogs.com/yugang/archive/2008/10/09/1307451.html