关于VS2005中GridView的自定义分页,单选、多选、排序、自增列的简单应用。
多选时,只有全部选中时“多选”才选中。
图片效果:
简单示例,代码如下,
fenpage.aspx的代码:
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="fenpage.aspx.cs" Inherits="gridview_fenpage"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<!
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
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
<
script
language
="javascript"
type
="text/javascript"
>
// 判断多选是否与选中项(没有选中的返回false)
function slcNo_click()
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
if (document.form1.checkboxname.length)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
for (var i=0;i<document.form1.checkboxname.length;i++)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
if(document.form1.checkboxname[i].checked)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
return true;
}
}
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
if(document.form1.checkboxname.checked)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
return true;
}
}
alert("请选择后再操作!");
return false;
}
// 鼠标经过改变行的颜色
if (!objbeforeItem)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
var objbeforeItem=null;
var objbeforeItembackgroundColor=null;
}
function ItemOver(obj)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
if(objbeforeItem)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
objbeforeItem.style.backgroundColor = objbeforeItembackgroundColor;
}
objbeforeItembackgroundColor = obj.style.backgroundColor;
objbeforeItem = obj;
obj.style.backgroundColor = "#B9D1F3";
}
//
// 多选的全选与取消
function checkJs(boolvalue)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
if(document.all.checkboxname.length>1)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
for(var i=0;i<document.all.checkboxname.length;i++)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
document.all.checkboxname[i].checked = boolvalue;
}
}
else
document.all.checkboxname.checked = boolvalue;
}
//
// 只有全部选中时“全选”选中
function SingleCheckJs()
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
var flag1=false;
var flag2=false;
if (document.form1.checkboxname.length)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
for (var i=0;i<document.form1.checkboxname.length;i++)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
if(document.form1.checkboxname[i].checked)
flag1 = true;
else
flag2 = true;
}
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
if(document.form1.checkboxname.checked)
flag1 = true;
else
flag2 = true;
}
if(flag1==true&&flag2==false)
document.getElementById("chk").checked = true;
else
document.getElementById("chk").checked = false;
}
//
</
script
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
>
<
table
cellpadding
="0"
cellspacing
="0"
border
="0"
width
="80%"
style
="font-size: 11px"
>
<
tr
>
<
td
align
="center"
>
<
asp:GridView
ID
="GridView1"
runat
="server"
Width
="100%"
CellPadding
="4"
ForeColor
="#333333"
AutoGenerateColumns
="False"
AllowPaging
="True"
PageSize
="12"
BorderColor
="Silver"
BorderStyle
="Solid"
BorderWidth
="1px"
OnRowDataBound
="GridView1_RowDataBound"
ShowFooter
="True"
EmptyDataText
="没有数据记录!!"
AllowSorting
="True"
OnSorting
="GridView1_Sorting"
>
<
Columns
>
<
asp:BoundField
HeaderText
="编号"
DataField
="id"
Visible
="False"
/>
<
asp:TemplateField
HeaderText
="<input type='checkbox' id='chk' name='chk' οnclick='checkJs(this.checked);' />全选"
FooterText
="全选"
>
<
ItemTemplate
>
<
input
type
="checkbox"
id
="checkboxname"
name
="checkboxname"
value
='<%#
DataBinder.Eval(Container.DataItem, "id")%
>
' οnclick='SingleCheckJs();' />
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
="单选"
FooterText
="单选"
>
<
ItemTemplate
>
<
input
type
="radio"
id
="RadioName"
name
="RadioName"
value
='<%#
Eval("id")%
>
' />
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
="自增列"
FooterText
="自增列"
>
<
ItemTemplate
>
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
<%
# (Container.DataItemIndex+1).ToString()
%>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:BoundField
HeaderText
="姓名"
FooterText
="姓名"
DataField
="name"
SortExpression
="name"
/>
<
asp:BoundField
HeaderText
="身份证号"
FooterText
="身份证号"
DataField
="card"
SortExpression
="card"
/>
<
asp:BoundField
HeaderText
="价格"
FooterText
="价格"
DataField
="price"
DataFormatString
="{0:¥#,##0.00}"
HtmlEncode
="False"
SortExpression
="price"
/>
<
asp:BoundField
HeaderText
="数字"
FooterText
="数字"
DataField
="price"
DataFormatString
="{0:0.00}"
HtmlEncode
="False"
SortExpression
="price"
/>
<
asp:BoundField
HeaderText
="建立时间"
FooterText
="建立时间"
DataField
="createdate"
DataFormatString
="{0:yyyy年MM月dd日 hh时mm分ss秒}"
HtmlEncode
="False"
SortExpression
="createdate"
/>
</
Columns
>
<
RowStyle
BackColor
="#F7F6F3"
ForeColor
="#333333"
/>
<
EditRowStyle
BackColor
="#999999"
/>
<
SelectedRowStyle
BackColor
="#E2DED6"
Font-Bold
="True"
ForeColor
="#333333"
/>
<
PagerStyle
BackColor
="#284775"
ForeColor
="White"
HorizontalAlign
="Center"
/>
<
AlternatingRowStyle
BackColor
="White"
ForeColor
="#284775"
/>
<
PagerSettings
Visible
="False"
/>
<
FooterStyle
Font-Bold
="True"
/>
<
HeaderStyle
Font-Bold
="False"
Font-Italic
="False"
/>
</
asp:GridView
>
</
td
>
</
tr
>
<
tr
>
<
td
align
="center"
style
="height: 25px"
>
<
asp:LinkButton
ID
="btnFirst"
CommandArgument
="first"
OnClick
="PagerButtonClick"
runat
="server"
>
首 页
</
asp:LinkButton
>
<
asp:LinkButton
ID
="btnPrev"
CommandArgument
="prev"
OnClick
="PagerButtonClick"
runat
="server"
>
上一页
</
asp:LinkButton
>
<
asp:LinkButton
ID
="btnNext"
CommandArgument
="next"
OnClick
="PagerButtonClick"
runat
="server"
>
下一页
</
asp:LinkButton
>
<
asp:LinkButton
ID
="btnLast"
CommandArgument
="last"
OnClick
="PagerButtonClick"
runat
="server"
>
尾 页
</
asp:LinkButton
>
<
asp:Label
ID
="LblCurrentIndex"
runat
="server"
></
asp:Label
>
<
asp:Label
ID
="LblPageCount"
runat
="server"
></
asp:Label
>
<
asp:Label
ID
="LblRecordCount"
runat
="server"
></
asp:Label
></
td
>
</
tr
>
<
tr
>
<
td
>
<
asp:Button
ID
="Button2"
runat
="server"
Text
="checkbox得到选择的行"
OnClick
="Button2_Click"
>
</
asp:Button
>
<
asp:Button
ID
="Button1"
runat
="server"
Text
="radio得到选择的行"
OnClick
="Button1_Click"
></
asp:Button
>
</
td
>
</
tr
>
</
table
>
</
div
>
</
form
>
</
body
>
</
html
>
fenpage.aspx.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;
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
public
partial
class
gridview_fenpage : System.Web.UI.Page
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
protected void Page_Load(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
Button2.Attributes["onclick"] = "return slcNo_click();";
GridViewBind("");
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
private void GridViewBind(string Sqlsort)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
string connStr = ConfigurationManager.ConnectionStrings["ConnString1"].ConnectionString;
string SqlStr = "SELECT * FROM test01 where id<1000" + Sqlsort;
DataSet ds = new DataSet();
try
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds, "test01");
if (conn.State.ToString() == "Open") conn.Close();
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
LblCurrentIndex.Text = "第 " + (GridView1.PageIndex + 1).ToString() + " 页";
LblPageCount.Text = "共 " + GridView1 .PageCount.ToString()+ " 页";
LblRecordCount.Text = "总共 "+ds.Tables[0].Rows.Count.ToString()+" 条";
if (ds.Tables[0].Rows.Count == 0)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
btnFirst.Visible = false;
btnPrev.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
LblCurrentIndex.Visible = false;
LblPageCount.Visible = false;
LblRecordCount.Visible = false;
}
else if (GridView1.PageCount == 1)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
btnFirst.Visible = false;
btnPrev.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
// 计算生成分页页码,分别为:"首 页" "上一页" "下一页" "尾 页"
btnFirst.CommandName = "1";
btnPrev.CommandName = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
btnNext.CommandName = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());
btnLast.CommandName = GridView1.PageCount.ToString();
//
}
catch(Exception ex)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
Response.Write("数据库错误,错误原因:"+ex.Message);
Response.End();
}
}
protected void PagerButtonClick(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName)-1;
GridViewBind("");
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
e.Row.Attributes["onmouseover"] = "ItemOver(this)";
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
protected void Button2_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
string str="";
string []ckb=null;
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
str=Request.Form.Get("checkboxname");
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
ckb=str.Split(new char[]
{','});
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
Response.Write("直接在页面中得到的值为:"+str+"<br>");
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
Response.Write("处理后存放在数组中,如下:<br>");
for(int i=0;i<ckb.Length;i++)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
Response.Write("ckb["+i+"]的值为:"+ckb[i]+"<br>");
}
}
protected void Button1_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
Response.Write(Request.Form.Get("RadioName"));
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
string sql = "";
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
if (ViewState["SortDirection"] == null || ViewState["SortDirection"].ToString().CompareTo("") == 0)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
ViewState["SortDirection"] = " desc";
}
else
ViewState["SortDirection"] = "";
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
sql = " order by " + e.SortExpression + ViewState["SortDirection"];
GridViewBind(sql);
}
}
数据库中表的生成代码:
CREATE
TABLE
[
dbo
]
.
[
test01
]
(
[
id
]
[
decimal
]
(
18
,
0
)
IDENTITY
(
1
,
1
)
NOT
NULL
,
[
name
]
[
varchar
]
(
50
) COLLATE Chinese_PRC_CI_AS
NULL
,
[
card
]
[
varchar
]
(
50
) COLLATE Chinese_PRC_CI_AS
NULL
,
[
createdate
]
[
datetime
]
NULL
)
ON
[
PRIMARY
]
GO
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
ALTER
TABLE
[
dbo
]
.
[
test01
]
ADD
CONSTRAINT
[
DF_test01_createdate
]
DEFAULT
(
getdate
())
FOR
[
createdate
]
,
CONSTRAINT
[
PK_test01
]
PRIMARY
KEY
CLUSTERED
(
[
id
]
)
ON
[
PRIMARY
]
GO