GridView实现全选、分页实例代码 gridview分页的实现



效果图如上所示,后台代码如下:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Oracle.DataAccess.Client;
using System.Data.OleDb;
public partial class Default3 : System.Web.UI.Page
{
publicOracleConnection conn = null;
publicDataSet ds = new DataSet();
publicOleDbConnection Accessconn = null;
public inttotal = 0;
public intpagesize = 20;
public intpageindex = 0;
protectedvoid Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
getReport();
}
}
publicOracleConnection getOraclConnection()
{
string connectionstr = "DataSource=orcl;userid=jduser;password=jduser;";
try
{
conn = new OracleConnection(connectionstr);
conn.Open();
}
catch(Exception e)
{
//Console.WriteLine(e.Message);
Response.Write(e.Message);
}
return conn;

}

publicOleDbConnection getAccessConnection()
{
try
{
string connectionstr = "Provider=Microsoft.jet.OleDb.4.0;";
connectionstr += @"Data Source=d:databasetrain.mdb";
Accessconn = new OleDbConnection(connectionstr);
}
catch (Exception e)
{

Response.Write(e.Message);
}
return Accessconn;
}
publicDataSet getDataSet(OleDbConnection conn)
{
try
{
string sql = "select top " + pagesize + " * from(select * from train order by sno asc) order by sno";
OleDbDataAdapter oda = new OleDbDataAdapter(sql,conn);
oda.Fill(ds);
}
catch (Exception e)
{

Console.WriteLine(e.Message);
}
return ds;
}
public intgetTotal(OleDbConnection conn)
{
try
{
DataSet ds1 = new DataSet();
string sql = "select count(*) from train";
OleDbDataAdapter oda = new OleDbDataAdapter(sql, conn);
oda.Fill(ds1);
total = int.Parse(ds1.Tables[0].Rows[0][0].ToString());
}
catch (Exception e)
{

Response.Write("获取信息总行数失败!"+e.Message);
}
return total;
}
public voidcloseConnection(OleDbConnection conn)
{
try
{
conn.Close();
}
catch (Exception e)
{

Console.WriteLine(e.Message);
}
}

publicvoid getReport()
{
Accessconn = getAccessConnection();
total = getTotal(Accessconn);
ds = getDataSet(Accessconn);
GridView1.DataSource = ds;
GridView1.PageSize = pagesize;
GridView1.DataBind();
closeConnection(Accessconn);
//GroupCol(GridView1,3,1,10);
}

publicvoid click_pagechanged(object sender, EventArgs e)
{
try
{
getReport();
pageindex = Convert.ToInt32(currentpage.Value);
intpages=(total%pagesize==0)?(total/pagesize):(total/pagesize+1);
DataSet ds2 = new DataSet();
string arg =((LinkButton)sender).CommandArgument.ToString().ToLower();
switch (arg)
{
case "pre":
if (pageindex>0)
{
pageindex = pageindex - 1;
}
break;
case "next":
if (pageindex<pages-1)
{
pageindex = pageindex + 1;
}
break;
case "last":
pageindex = pages - 1;
break;
default:
pageindex = 0;
break;
}
currentpage.Value = pageindex.ToString();
ds2 = getPageData(pageindex);
GridView1.DataSource = ds2;
GridView1.DataBind();
closeConnection(Accessconn);
}
catch (Exception er)
{

Response.Write("点击换页失败!"+er.Message);
}
Label1.Text = "操作信息栏!";
//Response.Write(pageindex);

}
publicDataSet getPageData(int pageindex)
{
int start1 = pageindex*pagesize;
int end1 = (pageindex+1)*pagesize;
DataSet ds3 = new DataSet();
try
{
if(pageindex==0)
{
Accessconn = getAccessConnection();
string str = "select top " + pagesize + " * from train ";
OleDbDataAdapter oda = new OleDbDataAdapter(str, Accessconn);
oda.Fill(ds3);
}
else
{
Accessconn = getAccessConnection();
string str = "select top " + (end1 - start1) + " * from train wheresno not in (select top " + start1 + " sno from train)";
OleDbDataAdapter oda = new OleDbDataAdapter(str, Accessconn);
oda.Fill(ds3);
}

}
catch (Exception ee)
{

Response.Write("获取页面数据集出错"+ee.Message);
}
return ds3;
}

publicvoid checkAll(object sender,EventArgs e)
{
CheckBox ch1 =(CheckBox)GridView1.HeaderRow.FindControl("checkbox1");
CheckBox ch2 =(CheckBox)GridView1.HeaderRow.FindControl("checkbox2");
CheckBox ch3 =(CheckBox)GridView1.HeaderRow.FindControl("checkbox3");
if(ch1.Checked==true)
{
for (int i = 0; i < GridView1.Rows.Count;i++ )
{
CheckBox item =(CheckBox)GridView1.Rows[i].FindControl("itemcheckbox");
item.Checked = true;
}
ch2.Checked = false;
ch3.Checked=false;
}

}
public voidcheckConverseAll(object sender,EventArgs e)
{
CheckBox ch1 =(CheckBox)GridView1.HeaderRow.FindControl("checkbox1");
CheckBox ch2 =(CheckBox)GridView1.HeaderRow.FindControl("checkbox2");
CheckBox ch3 =(CheckBox)GridView1.HeaderRow.FindControl("checkbox3");
if (ch3.Checked == true)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox item =(CheckBox)GridView1.Rows[i].FindControl("itemcheckbox");
if (item.Checked == true)
{
GridView实现全选、分页实例代码 gridview分页的实现
item.Checked = false;
}
else
{
item.Checked = true;
}

}
ch1.Checked = false;
ch2.Checked = false;
}
else
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox item =(CheckBox)GridView1.Rows[i].FindControl("itemcheckbox");
item.Checked = false;
}
}
}
public voidnotcheckAll(object sender,EventArgs e)
{
CheckBox ch1 =(CheckBox)GridView1.HeaderRow.FindControl("checkbox1");
CheckBox ch2 =(CheckBox)GridView1.HeaderRow.FindControl("checkbox2");
CheckBox ch3 =(CheckBox)GridView1.HeaderRow.FindControl("checkbox3");
if (ch2.Checked == true)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox item =(CheckBox)GridView1.Rows[i].FindControl("itemcheckbox");
item.Checked = false;
}
ch1.Checked = false;
ch3.Checked = false;
}
}
public voidselectEvent(object sender,EventArgs e)
{
LinkButton button = (LinkButton)sender;
string args = button.CommandArgument.ToString();
Label1.Text="您触发了 "+args+" 的选择操作!";
}
public voideditEvent(object sender,EventArgs e)
{
LinkButton button = (LinkButton)sender;
string args = button.CommandArgument.ToString();
Label1.Text = "您触发了 " + args + " 的修改操作!";
}
public voiddelEvent(object sender,EventArgs e)
{
LinkButton button = (LinkButton)sender;
string args = button.CommandArgument.ToString();
Label1.Text = "您触发了 " + args + " 的删除操作!";

}
public voidGroupCol(GridView gd,int cols,int srows,int erows)
{
if(gd.Rows.Count<1||cols>gd.Columns.Count-1)
{
return;
}
TableCell oldtc = gd.Rows[srows].Cells[cols];
for (int i = 1; i < erows-srows; i++)
{
TableCell tc=gd.Rows[srows+i].Cells[cols];
tc.Visible = false;
if (oldtc.RowSpan==0)
{
oldtc.RowSpan = 1;
}
oldtc.RowSpan++;
oldtc.VerticalAlign = VerticalAlign.Middle;
}
}

publicvoid operationCheckBox(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox ch =(CheckBox)GridView1.Rows[i].FindControl("itemcheckbox");
if(ch.Checked==true)
{
//Response.Write(GridView1.Rows[i].Cells[1].ToString());
//Response.Write("动作成功!");
Label1.Text = "触发了对 " +GridView1.Rows[i].Cells[1].Text.ToString()+" 的操作!";
}
}
}


}
前台代码如下:

<%@ Page Language="C#" CodeFile="GridViewTest.aspx.cs" Inherits="Default3"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>GridViewTest</title>
</head>
<body>
<form id="form1"runat="server">
<div align="center">
<asp:Label ID="Label1" runat="server" Text="操作信息栏!"Visible="true"></asp:Label>
</div>

<div align="center">
<asp:GridView ID="GridView1" runat="server"
CellPadding="4" ForeColor="#333333" GridLines="Both"ShowFooter="false" DataKeyNames="sno">
<FooterStyle BackColor="#507CD1" Font-Bold="True"ForeColor="White"/>
<RowStyle BackColor="#EFF3FB"/>
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="checkbox1" runat="server"Text="全选" OnCheckedChanged="checkAll"/>
<asp:CheckBox ID="checkbox2" runat="server"Text="全不选" OnCheckedChanged="notcheckAll"/>
<asp:CheckBox ID="checkbox3" runat="server"Text="反选" OnCheckedChanged="checkConverseAll"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="itemcheckbox"runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="sno" HeaderText="序号"/>
<asp:BoundField DataField="ID" HeaderText="车次"/>
<asp:BoundField DataField="type" HeaderText="列车类型"/>
<asp:BoundField DataField="s_no" HeaderText="站序"/>
<asp:BoundField DataField="station" HeaderText="车站"/>
<asp:BoundField DataField="day" HeaderText="日期"/>
<asp:BoundField DataField="a_time" HeaderText="到达时间"/>
<asp:BoundField DataField="d_time" HeaderText="发车时间"/>
<asp:BoundField DataField="distance" HeaderText="里程"/>
<asp:BoundField DataField="p1" HeaderText="硬座/一等座"/>
<asp:BoundField DataField="p2" HeaderText="硬卧/二等座"/>
<asp:BoundField DataField="p3" HeaderText="软座/特等座"/>
<asp:BoundField DataField="p4" HeaderText="软卧"/>
<asp:TemplateField>
<HeaderTemplate>
操作
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"CommandArgument='<%# eval_r("sno") %>'CommandName="sel" Text="选择"OnClick="selectEvent"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server"CommandArgument='<%# eval_r("sno") %>'CommandName="upd" Text="修改"OnClick="editEvent"></asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server"CommandArgument='<%# eval_r("sno") %>'CommandName="sel" Text="删除"OnClick="delEvent"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>

<PagerStyle BackColor="#2461BF" ForeColor="White"HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1"Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True"ForeColor="White" />
<EditRowStyle BackColor="#2461BF"/>
<AlternatingRowStyle BackColor="White"/>

</asp:GridView>
<div align="center">
<asp:Table runat="server">
<asp:TableRow>
<asp:TableCell>
<asp:Button ID="Button1" runat="server"OnClick="operationCheckBox" Text="操作" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
<br/>
</div>
<div align="center">
<asp:HiddenField ID="currentpage" runat="server"Value="0" />
<asp:LinkButton ID="LinkButton3"CommandArgument="first" runat="server" Text="首页"OnClick="click_pagechanged"></asp:LinkButton>
<asp:LinkButton ID="LinkButton4"CommandArgument="pre" runat="server" Text="上一页"OnClick="click_pagechanged"></asp:LinkButton>
&nbsp;&nbsp;当前页:<%=int.Parse(currentpage.Value)+1%> &nbsp;&nbsp;
<asp:LinkButton ID="LinkButton5"CommandArgument="next" runat="server" Text="下一页"OnClick="click_pagechanged"></asp:LinkButton>
<asp:LinkButton ID="LinkButton6"CommandArgument="last" runat="server" Text="尾页"OnClick="click_pagechanged"></asp:LinkButton>
</div>
</form>
</body>
</html>

  

爱华网本文地址 » http://www.413yy.cn/a/25101015/271329.html

更多阅读

全选快捷键是什么 excel全选快捷键

全选快捷键是什么——简介电脑里面的全选快捷键是什么,怎么快速全选全部文件!全选快捷键是什么——方法/步骤全选快捷键是什么 1、第一种,可以全选页面内的文字,或一个文件夹里面的所有文件按ctrl+a,如图全选快捷键是什么 2、

声明:《GridView实现全选、分页实例代码 gridview分页的实现》为网友痞味见底分享!如侵犯到您的合法权益请联系我们删除