前面有一篇文章写的是GridView自定义分页存储过程,所用的方法是三种分页方法中的临时表法。现在用Top方法做了一个用户自定义控件.优点:支持多表联合查询,支持排序
前面有一篇文章写的是GridView自定义分页存储过程,所用的方法是三种分页方法中的临时表法。现在用Top方法做了一个用户自定义控件.优点:支持多表联合查询,支持排序,缺点:用以被注入攻击
效果如图:
aspx代码:
aspx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="GvPageSet.ascx.cs" Inherits="Control_GvPageSet" %>
<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>
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="GvPageSet.ascx.cs" Inherits="Control_GvPageSet" %>
<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>
cs代码:
Code
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.ComponentModel;
using System.Data.OleDb;
public partial class Control_GvPageSet : 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;
}
}
[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
OleDbConnection conn = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Access"].ConnectionString);
OleDbCommand comm = new OleDbCommand();
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 + OrderBy + ") as a ";
if(lower > 0)
sql += " where ID not in (select top " + lower.ToString() + " ID from (" + Sql + FilterString + OrderBy + ") as b)";
comm.CommandText = sql;
comm.Connection = conn;
OleDbDataAdapter da = new OleDbDataAdapter(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();
}
}
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.ComponentModel;
using System.Data.OleDb;
public partial class Control_GvPageSet : 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;
}
}
[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
OleDbConnection conn = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Access"].ConnectionString);
OleDbCommand comm = new OleDbCommand();
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 + OrderBy + ") as a ";
if(lower > 0)
sql += " where ID not in (select top " + lower.ToString() + " ID from (" + Sql + FilterString + OrderBy + ") as b)";
comm.CommandText = sql;
comm.Connection = conn;
OleDbDataAdapter da = new OleDbDataAdapter(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();
}
}