关于VS2005中GridView的自定义分页,单选、多选的简单应用。
图片效果:
简单示例,代码如下,
fenpage.aspx的代码:
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="fenpage.aspx.cs" Inherits="gridview_fenpage" %>

<!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>

<script language="javascript" type="text/javascript">
// 全部选中
function QuanXuan_Click()

{
if (document.form1.checkboxname.length)

{
for (var i=0;i<document.form1.checkboxname.length;i++)

{
document.form1.checkboxname[i].checked = true;
}
}
else

{
document.form1.checkboxname.checked = true;
}
}
// 取消选中
function QuXiao_Click()

{
if (document.form1.checkboxname.length)

{
for (var i=0;i<document.form1.checkboxname.length;i++)

{
document.form1.checkboxname[i].checked = false;
}
}
else

{
document.form1.checkboxname.checked = false;
}
}
// 判断没有选中的返回false
function slcNo_click()

{
if (document.form1.checkboxname.length)

{
for (var i=0;i<document.form1.checkboxname.length;i++)

{
if(document.form1.checkboxname[i].checked)

{
return true;
}
}
}
else

{
if(document.form1.checkboxname.checked)

{
return true;
}
}
alert("请选择后再操作!");
return false;
}
// 改变行的颜色
if (!objbeforeItem)

{
var objbeforeItem=null;
var objbeforeItembackgroundColor=null;
}
function ItemOver(obj)

{
objbeforeItembackgroundColor=obj.style.backgroundColor;
obj.style.backgroundColor="#B9D1F3";
objbeforeItem=obj;
}
function ItemOut(obj)

{
if(objbeforeItem)

{
objbeforeItem.style.backgroundColor=objbeforeItembackgroundColor;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="0" cellspacing="0" border="0" width="60%" 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">
<Columns>
<asp:BoundField HeaderText="编号" DataField="id" Visible="false" />
<asp:TemplateField HeaderText="多选">
<ItemTemplate>
<input type="checkbox" id="checkboxname" name="checkboxname" value='<%# DataBinder.Eval(Container.DataItem, "id")%>
' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="单选">
<ItemTemplate>
<input type="radio" id="RadioName" name="RadioName" value='<%# DataBinder.Eval(Container.DataItem, "id")%>
'/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="姓名" DataField="name" />
<asp:BoundField HeaderText="身份证号" DataField="card" />
<asp:BoundField HeaderText="建立时间" DataField="createdate" />
</Columns>
<PagerSettings Visible="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>
<asp:Label ID="LblNoRecord" runat="server" Text="记录为零" Visible="False"></asp:Label></td>
</tr>
<tr>
<td>
<input type="button" value="全部选中" onclick="QuanXuan_Click()" />
<input type="button" value="取消选中" onclick="QuXiao_Click()" />
<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;

public partial class
gridview_fenpage : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)

{
GridViewBind();
}

private void GridViewBind()

{
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
string SqlStr = "SELECT * FROM test01";
DataSet ds = new DataSet();

try

{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds, "test01");
if (conn.State.ToString() == "Open") conn.Close();

GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();

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)

{
btnFirst.Visible = false;
btnPrev.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;

LblCurrentIndex.Visible = false;
LblPageCount.Visible = false;
LblRecordCount.Visible = false;

LblNoRecord.Visible = true;
}
else if (GridView1.PageCount == 1)

{
btnFirst.Visible = false;
btnPrev.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
}
}
catch(Exception ex)

{
Response.Write("数据库错误,错误原因:"+ex.Message);
Response.End();
}
}
protected void PagerButtonClick(object sender, EventArgs e)

{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch (arg)

{
case "prev":
if (GridView1.PageIndex > 0)

{
GridView1.PageIndex -= 1;
}
break;
case "next":
if (GridView1.PageIndex < (GridView1.PageCount - 1))

{
GridView1.PageIndex += 1;
}
break;
case "last":
GridView1.PageIndex = (GridView1.PageCount - 1);
break;
default:
GridView1.PageIndex = 0;
break;
}

GridViewBind();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{
e.Row.Attributes["onmouseover"] = "ItemOver(this)";
e.Row.Attributes["onmouseout"] = "ItemOut(this)";
}

protected void Button2_Click(object sender, EventArgs e)

{
string str="";
string []ckb=null;

str=Request.Form.Get("checkboxname");

ckb=str.Split(new char[]
{','});

Response.Write("直接在页面中得到的值为:"+str+"<br>");

Response.Write("处理后存放在数组中,如下:<br>");
for(int i=0;i<ckb.Length;i++)

{
Response.Write("ckb["+i+"]的值为:"+ckb[i]+"<br>");
}
}
protected void Button1_Click(object sender, EventArgs e)

{
Response.Write(Request.Form.Get("RadioName"));
}
}
数据库中表的生成代码:
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

ALTER TABLE [dbo].[test01] ADD
CONSTRAINT [DF_test01_createdate] DEFAULT (getdate()) FOR [createdate]
,
CONSTRAINT [PK_test01] PRIMARY KEY CLUSTERED
(
[id]
)
ON [PRIMARY]
GO
图片效果:
简单示例,代码如下,
fenpage.aspx的代码:





















































































































































































fenpage.aspx.cs的代码:


























































































































































数据库中表的生成代码:














