您的位置:首页 > 理论基础 > 计算机网络

GridView分页 用户控件(参考CSharpFish博客)http://www.cnblogs.com/hongyuniu/archive/2008/08/05/1261014.html

2013-05-23 16:55 746 查看
前台:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<div style="text-align:right; width:100%;background-color:#c6d7ef">
<table cellpadding="0" cellspacing="0">
<tr>
<td style="width: 200px; text-align: right">
<asp:Label ID="Label1" runat="server" Text="当前页:"></asp:Label><asp:Label ID="lblCurrentInfo"
runat="server">1</asp:Label>   <asp:Label ID="Label2" runat="server" Text="共"></asp:Label><asp:Label ID="lblCount"
runat="server">1</asp:Label><asp:Label ID="Label3" runat="server" Text="页"></asp:Label><asp:Label
ID="lblTotalNum" runat="server">5</asp:Label><asp:Label ID="Label5" runat="server" Text="条"></asp:Label>
   
</td>
<td style="width: 33px">
<asp:LinkButton ID="lbtnFirst" runat="server" OnClick="PageIndex_Changed">首页</asp:LinkButton></td>
<td style="width: 50px">
<asp:LinkButton ID="lbtnPrev" runat="server" OnClick="PageIndex_Changed">上一页</asp:LinkButton></td>
<td style="width: 50px">
<asp:LinkButton ID="lbtnNext" runat="server" OnClick="PageIndex_Changed">下一页</asp:LinkButton></td>
<td style="width: 33px">
<asp:LinkButton ID="lbtnLast" runat="server" OnClick="PageIndex_Changed">末页</asp:LinkButton></td>
</tr>
</table></div>

后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;

public partial class WebUserControl : System.Web.UI.UserControl
{
#region Properties
[BrowsableAttribute(true)]
[DescriptionAttribute("要绑定数据的GridView.ID")]
[DefaultValueAttribute("")]
[CategoryAttribute("Appearance")]
public string gvID
{
get
{
return (ViewState["gvID"] == null) ? "" : ViewState["gvID"].ToString();
}
set
{
ViewState["gvID"] = value;
}
}

[BrowsableAttribute(true)]
[DescriptionAttribute("每一页显示的行数,默认每页10条")]
[DefaultValueAttribute("")]
[CategoryAttribute("Appearance")]
public int PageSize
{
get
{
return (ViewState["PageSize"] == null) ? 10 : Convert.ToInt32(ViewState["PageSize"]);
}
set
{
ViewState["PageSize"] = value;
}
}

[BrowsableAttribute(true)]
[DescriptionAttribute("要从中读取数据的DataTable.Name")]
[DefaultValueAttribute("")]
[CategoryAttribute("Data")]
public string TableName
{
get
{
return (ViewState["TableName"] == null) ? "" : ViewState["TableName"].ToString();
}
set
{
ViewState["TableName"] = value;
}
}

[BrowsableAttribute(true)]
[DescriptionAttribute("要从中读取数据的DataTable.Name")]
[DefaultValueAttribute("")]
[CategoryAttribute("Data")]
public string OrderBy
{
get
{
return (ViewState["OrderBy"] == null) ? "" : ViewState["OrderBy"].ToString();
}
set
{
ViewState["OrderBy"] = value;
}
}
public string PrimaryKey
{
get
{
return (ViewState["PrimaryKey"] == null) ? "" : ViewState["PrimaryKey"].ToString();
}
set
{
ViewState["PrimaryKey"] = value;
}
}
[BrowsableAttribute(true)]
[DescriptionAttribute("读取数据的Sql语句,多表 联合查询")]
[DefaultValueAttribute("")]
[CategoryAttribute("Data")]
public string Sql
{
get
{
return (ViewState["Sql"] == null) ? "" : ViewState["Sql"].ToString();
}
set
{
ViewState["Sql"] = value;
}
}

[BrowsableAttribute(true)]
[DescriptionAttribute("过滤条件,可以为空")]
[DefaultValueAttribute("")]
[CategoryAttribute("Data")]
public string FilterString
{
get
{
return (ViewState["FilterString"] == null) ? "" : ViewState["FilterString"].ToString();
}
set
{
ViewState["FilterString"] = value;
}
}

[BrowsableAttribute(false)]
[DescriptionAttribute("当前页数")]
[DefaultValueAttribute("")]
public int CurrentInfo
{
get
{
return Convert.ToInt32(lblCurrentInfo.Text.Trim());
}
}
#endregion
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString);
// OleDbConnection conn = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString);
// OleDbCommand comm = new OleDbCommand();
SqlCommand comm = new SqlCommand();
string sql = "";

protected override void OnInit(EventArgs e)
{
this.Visible = false;
}

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//this.lbtnPrev.Enabled = false;
//this.lblCurrentInfo.Text = "1";
}
}

/// <summary>
/// 绑定GridView控件,供页面调用
/// </summary>
public void BindGridView()
{
GetPageNum();
//this.Visible = false;
this.lbtnPrev.Enabled = false;
this.lblCurrentInfo.Text = "1";
BindGV();
}

/// <summary>
/// 绑定GridView控件,供页面调用
/// </summary>
/// <param name="currentInfo">要显示的当前页</param>
public void BindGridView(int currentInfo)
{
GetPageNum();

//this.Visible = false;
this.lbtnPrev.Enabled = false;
this.lblCurrentInfo.Text = currentInfo.ToString();
BindGV();
}

/// <summary>
/// 获取结果总数目和总页数
/// </summary>
/// <returns>总页数</returns>
public int GetPageNum()
{
sql = "";
sql += "select count(*) from ( ";
sql += Sql;
sql += FilterString + ") as a ";
comm.CommandText = sql;
comm.Connection = conn;
conn.Open();
int TotalNum = Convert.ToInt32(comm.ExecuteScalar());
conn.Close();
lblTotalNum.Text = TotalNum.ToString();
int pageNum = Convert.ToInt32(Math.Ceiling(Convert.ToDouble((double)TotalNum / PageSize)));
lblCount.Text = pageNum.ToString();

return TotalNum;
}

#region 按钮处理事件
/**/
/// <summary>
/// 按钮处理事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void PageIndex_Changed(object sender, EventArgs e)
{
LinkButton linkbtn = (LinkButton)sender;
switch (linkbtn.ID.Trim())
{
case "lbtnFirst":
this.lblCurrentInfo.Text = "1";

BindGV();

break;
case "lbtnPrev":

this.lblCurrentInfo.Text = (Convert.ToInt32(this.lblCurrentInfo.Text) - 1).ToString();

BindGV();

break;
case "lbtnNext":

this.lblCurrentInfo.Text = (Convert.ToInt32(this.lblCurrentInfo.Text) + 1).ToString();

BindGV();

break;
case "lbtnLast":
this.lblCurrentInfo.Text = this.lblCount.Text;

BindGV();

break;
}
SetBtnState();
}
private void SetBtnState()
{
if (Convert.ToInt32(this.lblCurrentInfo.Text) >= Convert.ToInt32(this.lblCount.Text) && Convert.ToInt32(this.lblCurrentInfo.Text) <= 1)
{
this.lbtnPrev.Enabled = false;
this.lbtnNext.Enabled = false;
this.lbtnFirst.Enabled = false;
this.lbtnLast.Enabled = false;
}
else if (Convert.ToInt32(this.lblCurrentInfo.Text) >= Convert.ToInt32(this.lblCount.Text))
{
this.lbtnPrev.Enabled = true;
this.lbtnNext.Enabled = false;
this.lbtnFirst.Enabled = true;
this.lbtnLast.Enabled = false;
}
else if (Convert.ToInt32(this.lblCurrentInfo.Text) <= 1)
{
this.lbtnPrev.Enabled = false;
this.lbtnNext.Enabled = true;
this.lbtnFirst.Enabled = false;
this.lbtnLast.Enabled = true;
}
else
{
this.lbtnPrev.Enabled = true;
this.lbtnNext.Enabled = true;
this.lbtnFirst.Enabled = true;
this.lbtnLast.Enabled = true;
}
}
#endregion

/// <summary>
/// 获取数据绑定GridView
/// </summary>
private void BindGV()
{
int lower, upper;
int currentPage = Convert.ToInt32(lblCurrentInfo.Text.Trim());
lower = (currentPage - 1) * PageSize;
upper = lower + PageSize;

DataTable dt = new DataTable();
sql = "";
sql += "select top " + PageSize.ToString() + " * from (" + Sql + FilterString + ") as a ";
if (lower > 0)
{
sql += " where " + PrimaryKey + " not in (select top " + lower.ToString() + " " + PrimaryKey + " from (" + Sql + FilterString + ") as b)";
}
sql += ""+OrderBy+"";
comm.CommandText = sql;
comm.Connection = conn;
// OleDbDataAdapter da = new OleDbDataAdapter(comm);
SqlDataAdapter da = new SqlDataAdapter(comm);
conn.Open();
da.Fill(dt);
conn.Close();

if (dt.Rows.Count == 0)
{
this.Visible = false;
}
else
{
this.Visible = true;
}
GridView gv = (GridView)Page.FindControl(gvID);
gv.DataSource = dt;
gv.DataBind();
SetBtnState();
}

}

引用:

string sql = "select * from EMPLOYEE ";

WebUserControl1.gvID = GridView1.ClientID;
WebUserControl1.Sql = sql;
WebUserControl1.OrderBy = "order by emplID ";
WebUserControl1.PrimaryKey = "emplID";
WebUserControl1.BindGridView();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐