颜色gridview控件自定义分页详解

gridview分页  时间:2021-02-11  阅读:()

GridView控件自定义分页详解

在这里我们将用一个隐藏字段来保存这个PageIndex,即当前页码.当点击上一页时,将它的值减一,知道为0,要注意的一点这里的第一页页码是0而不是1.下面看看代码,然后我们再分析分析!

1<asp:GridView ID="News Grid" runat="s erver" AutoGenerateColumns="False" AllowPaging="fals e"Wid th="100%">

2 <Columns>

3 <asp:BoundField DataField="News Id"HeaderText="新闻ID"/>

4 <asp:HyperLinkField DataNavigateUrlFields="NewsId"DataNavig ateUrlFormat String="~/Details.as px?ID={0}"

5 DataTextField="Title"HeaderText="新闻标题" ItemStyle-Width="70%"/>

6 <asp:BoundField DataField="PostTime"HeaderText="发布时间"/>

7 <asp:CommandField HeaderText=" 新 闻 管 理 " ShowCancelButton="False"Sh owDeleteButton="True"

8 ShowEditButton="True"/>

9 </Columns>

10 </as p:Grid View>

11 <div s tyle="height:16px;p adding-top:5px;margin-right:30px;flo at:right">

12 <asp:HiddenField ID="CurrentPage" runat="s erver"Value="0"/>

13 <asp:LinkButton ID="First" runat="server" CommandArgument="first"On Clic k="Pag erButto n_Clic k">首页</asp:LinkButt on>

14 <asp:LinkButton ID="Prev" runat="server" CommandArgument="prev"On Clic k="P a g e rButto n_Clic k">上一页</asp:Lin kBu tt on>

15 <asp:LinkButton ID="Next" runat="server" CommandArgument="next"On Clic k="P a g e rButto n_Clic k">下一页</asp:Lin kBu tt on>

16 <asp:LinkButton ID="Last" runat="server" CommandArgument="last"On Clic k="Pag erButto n_Clic k">尾页</asp:LinkButt on>

17 </div> CS文件中的代码:

1 protected void PagerButton_Click(object sender,EventArgs e)

2 {

3 int pageIndx=Convert.ToInt32(CurrentPage.Value);

4 int totals=NewsManager.GetNews(0,pageSize).TotalRecords;

5 int pages=(totals%pageSize)==0?(totals/pageSize) : (totals /pageSize+1);

6 string arg=((LinkButton)sender).CommandArgument.ToString().ToLower();

7 switch(arg)

8 {

9 case"prev":

10 if(pageIndx>0)

11 {

12 pageIndx-=1;

13 }

14 break;

15 case"next":

16 if(pageIndx<pages - 1)

17 {

18 pageIndx+=1;

19 }

20 break;

21 case"last":

22 pageIndx=pages - 1;

23 break;

24 default:

25 pageIndx=0;

26 break;

27 }

28 CurrentPage.Value=pageIndx.ToString();

29 NewsGrid.DataSource=NewsManager.GetNews(pageIndx,pageSize).Entities;

30 NewsGrid.DataBind();

31 }

如何在GridView中增加效果protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)

{

//将满足特定条件的行标为高亮if(e.Row.RowTyp e==DataContro lRowType.DataRow)//判定当前的行是否属于datarow类型的行

{int money=Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "MONEY"));//取当前行的列值if(money==77)e.Row.BackCo lor=Color.Red;

//string customer=(string)DataBinder.Eval(e.Row.DataItem, "CUSTOMER");string customer=DataBinder.Eval(e.Row.DataItem, "CUSTOMER").ToString();if(customer=="sdf")e.Row.BackCo lor=Co lor.Re d;

}

//加入鼠标滑过的高亮效果if(e.Row.RowTyp e==DataContro lRowType.DataRow)//判定当前的行是否属于datarow类型的行

{

//当鼠标放上去的时候先保存当前行的背景颜色并给附一颜色e.Ro w.Attributes.A dd("onmouseo ver","currentco lor=this.style.b ackgroundCo lor;this.s tyle.b ackgroundCo lor='y ellow',this.s tyle.fontWeight='';");

//当鼠标离开的时候将背景颜色还原的以前的颜色e.Row.Attributes.Add("onmouseout","this.style.b ackgroundCo lor=currentco lor,this.style.fontWeight='';");

}

//单击行改变行背景颜色if(e.Row.RowTyp e==DataContro lRowType.DataRow)

{

e.Row.Attributes.Add("onclick","this.style.b ackgroundCo lor='#99cc00';this.style.color='buttontext';this.style.cursor='default';");

}

如何在GridView中一次性批量更新多行数据

ASP.NET2.0下含有DropDownList的GridView编辑、删除的完整例

子

<asp:GridView ID="GridView1" runat="server"AutoGenerateColumns="Fals e"PageSize="10"

Width="542px"AllowPaging="True"AllowSorting="True"

DataKeyNames="DB31_1,DB31_2"

OnRowCanc eling Edit="Grid View 1_RowCanc elingEdit"OnRowDeleting="GridView 1_RowDeleting"OnRowEditing="GridView 1_RowEditing"OnRowUpdating="GridView 1_RowUp dating"

OnPageIndexChanging="GridView 1_PageIndexChanging"

OnRowDataBound="Grid View1_RowDataBound"

On Sele ctedIndexCh ang ed="GridView 1_Se le ctedIndexCh ange d"On So rting="Grid View 1_So rting">

<Columns>

<asp:TemplateField HeaderText="序号">

<It emT emp lat e>

<%#this.Grid View1.PageIndex*this.GridView1.PageSize+th is.Grid View 1.Ro ws.Co unt+1%>

</It emT emp lat e>

</a sp:T emp lateFie ld>

<asp:TemplateField HeaderText="学历代码"SortExpress ion="DB1_1">

<Ed itIt emT emp lat e>

<%--<asp:TextBox ID="TextBox1" runat="server"Text='<%#

Bind("DB1_1")%>'></asp:TextBox>--%>

<asp:DropDownLis t ID="ddlXL" runat="s erver" DataValueField='<%#

Bind("DB 1_1")%>'></asp:DropDownLis t>

</Ed itIt emT emp lat e>

<ItemTemp late>

<asp:Label ID="Label1" runat="server"Text='<%#

Bind("xu e liText")%>'></asp:Lab e l>

</It emT emp lat e>

</a sp:T emp lat eFie ld>

<asp:TemplateField HeaderText="学历名称"SortExpress ion="DB1_2">

<EditItemTemp late>

<asp:TextBox ID="TextBox2" runat="server"Text='<%#

Bind("DB1_2")%>'></asp:TextBox>

</Ed itIt emT emp lat e>

<It emT emp lat e>

<asp:Label ID="Label2" runat="server"Text='<%#

Bind("DB 1_2")%>'></asp:Lab el>

</It emT emp lat e>

</a sp:T emp lat e Fie ld>

<asp:TemplateField HeaderText="操作"ShowHeader="False">

<EditItemTemp late>

<asp:LinkButton ID="LinkButton 1" runat="s erver"Causes Validation="True"Co mmandName="Update"

Text="更新"></asp:LinkButton>

<asp:LinkButton ID="LinkButton2" runat="s erver"Causes Validation="Fals e"Co mman dName="Canc e l"

Text="取消"></asp:LinkButton>

</EditItemTemp late>

<It emT emp lat e>

<asp:LinkButton ID="LinkButton 1" runat="s erver"CausesValidation="Fals e"Co mman dName="Edit"

Text="编辑"OnClientClick="return confirm('确认要编辑吗 ');"></as p:LinkButton><asp:LinkButton ID="LinkButton3" runat="s erver"Causes Validation="Fals e"Co mman dName="De lete"

Text="删除"OnClientClick="return confirm('确认要删除吗 ');"></as p:LinkButton><asp:LinkButton ID="LinkButton2" runat="s erver"Causes Validation="Fals e"Co mman dName="S e le ct"

Text="选择"></asp:LinkButton>

</It emT emp lat e>

</as p:Temp lateField>

</Columns>

<AlternatingRowStyle BackColor="Aquamarine" />

</a sp:Gr idView>

///<summary>

///绑定数据到Grid View

///</summary>private void GridViewBind()

{

检索数据库string strSql="SELECT*FROM DB1";

得到数据集

this.GridView 1.DataSource=conn.GetDs(strSql).Tab les[0].DefaultView;this.GridView 1.DataBind();

}

///<summary>

///编辑当前行

///</summary>

///<p aram name="s ender"></param>

///<p aram n ame="e"></p aram>protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e)

{

Grid View 1.EditIndex=e.NewEditIndex;

//当前编辑行背景色高亮this.GridView 1.EditRowStyle.BackCo lor=Co lor.Fro mName("#F7CE90");

Grid ViewBind();

}

///<summary>

///取消编辑状态

///</summary>

///<p aram name="s ender"></param>

///<p aram n ame="e"></p aram>protected void GridView1_RowCancelingEdit(object s ender,GridView CancelEditEventArgs e){

GridView 1.EditIndex=-1;

Grid ViewBind();

}

///<summary>

///删除记录过程

///</summary>

///<p aram name="s ender"></param>

///<p aram n ame="e"></p aram>protected void GridView1_RowDeleting(object sender,GridViewDe leteEventArgs e){

//得到单位编号s tring rowToDelete=GridView1.DataKeys[e.RowIndex].Va lues[0].To String();

//转换为整数

//int ID=Convert.ToInt32(rowToDelete);

//从数据库中删除string str="DELETE FROM DB1 where DB1_1="+"'"+rowToDelete+"'"+"";try

{conn.RunSql(str);

//重新绑定数据

Grid ViewBind();

}catch(Exception ex)

{

Response.Write("数据库错误错误原因 "+ex.Message);

Response.End();

}

}

///<summary>

///更新记录过程

///</summary>

///<p aram name="s ender"></param>

///<p aram n ame="e"></p aram>protected void GridView1_RowUpdating(object sender,GridViewUpdateEventArgs e){string ID=Grid View1.DataKeys[e.RowIndex].Values[0].To String();string DB 1_1=((Te xtBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;//string DB1_2=((Te xtBox)GridView1.Rows[e.RowIndex].F indControl("TextBox2")).Text;string DB1_2=

(((Drop DownList))GridView 1.Rows[e.RowIndex].F indContro l("ddlXL")).Sele ctedItem.Text;//判断表单项是否有空并给出提示信息if(DB 1_1=="" | |DB 1_2=="")

{conn.Alert("请输入完整信息 ",Page);return;

}try

{conn.BuilderEdit("select*from DB 1 where DB 1_1='"+ID+"'");conn.dr["DB 1_1"]=DB 1_1;conn.dr["DB 1_2"]=DB 1_2;c o nn.Bu ild e rEd it Clo s e();

}catch(OracleException err)

{if(err.Code.To String()=="1")conn.Alert("错误 已存在具有相同主键的记录",Page);elseconn.Alert("错误未能添加记录",Page);

}

Response.Write("<script language='javascript'>alert('数据已被保存! ');</script>");

//返回浏览状态

GridView 1.EditIndex=-1;

Grid ViewBind();

}

///<summary>

///分页事件

///</summary>

///<p aram name="s ender"></param>

///<p aram n ame="e"></p aram>protected void GridView1_PageIndexChanging(object sender,GridViewPageEventArgs e)

{

Grid View 1.PageIndex=e.NewPageIndex;

Grid ViewBind();

}

///<summary>

///加入鼠标效果及为DropDownLis t绑定值

///</summary>

///<p aram name="s ender"></param>

///<p aram name="e"></p aram>protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)

{

//为Drop DownLis t绑定值if(((Drop DownLis t)e.Row.FindContro l("ddlXL")) !=null)

{

DropDownList ddlXL=(DropDownList)e.Row.FindControl("ddlXL");d d lXL.It e ms.Cle ar();ddlXL.Items.Add(new ListItem("博士","1"));ddlXL.Items.Add(new ListItem("硕士","2"));ddlXL.Items.Add(new ListItem("学士","3"));

}

//加入鼠标滑过的高亮效果if(e.Row.RowType==DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行{

//当鼠标放上去的时候先保存当前行的背景颜色并给附一颜色e.Row.Attributes.Add("onmous eover",

"currentco lo r=th is.sty le.b ackgroundCo lo r;this.s ty le.b ackgroundCo lo r='y e llo w',th is.s tyle.fontWe ight='';");

//当鼠标离开的时候将背景颜色还原的以前的颜色e.Row.Attributes.Add("onmous eout",

"this.style.b ackgroundCo lor=currentco lor,this.s tyle.fontWeight='';");

}

//单击行改变行背景颜色

if(e.Row.RowType==DataControlRowType.DataRow)

{e.Ro w.Attributes.Add("o nc lick", "th is.sty le.b ackgroundCo lo r='#99c c 00';this.style.color='buttontext';this.style.cursor='default';");

}

}

ASP.NET2.0下含有CheckBo x的GridView删除选定记录实例<asp:GridView ID="GridView1" runat="server"AutoGenerateColumns="Fals e"PageSize="5"

2 Width="726px"AllowPaging="True"AllowSorting="True"

3 DataKeyNames="DB1_1,DB1_2" OnRowDeleting="GridView1_RowDeleting" OnPageIndexChanging="Grid View1_PageIndexChanging"OnRowDataBound="Grid View1_RowDataBound"On Sorting="Grid View 1_Sorting"Height="279p x">

4 <Co lu mn s>

5 <asp:TemplateField HeaderText="序号">

6 <It emT emp lat e>

7 <%#this.GridView 1.Pag eIndex*this.GridView 1.Pag e Size+th is.Grid View 1.Ro ws.Co unt+1%>

8 </It emT emp lat e>

9 </a sp:T emp lateFie ld>

10 <asp:TemplateField HeaderText="选择">

11 <It emT emp lat e>

12 <asp:CheckBox ID="chkSelect" runat="s erver"></asp:CheckBox>

13 </It emT emp lat e>

14 </a sp:Temp lateFie ld>

15 <as p:TemplateField HeaderText="操作"ShowHeader="False">

16 <It emT emp lat e>

17 <asp:LinkButton ID="lbtnDelete" runat="s erver"CausesValidation="Fals e"Co mman dName="De lete"

18 Text="删除"OnClientClick="return confirm('确认要删除吗

');"></asp:LinkButton>

19 </It emT emp lat e>

20 </a s p:Te mp lat e F ie ld>

21 </Columns>

22 <AlternatingRowStyle BackColor="Aquamarine" />

23 </a sp:Grid View>

1 <as p:CheckBox ID="chkSelectAll" runat="server"Text="全部选中"

OnCheckedChanged="chkSelectAll_CheckedChanged"AutoPos tBack="True"></asp:CheckBox>2 <as p:Button ID="btnDelete" runat="server"Text="删除"OnClick="btnDelete_Click" />1 protected void btnDelete_Click(object sender,EventArgs e)

2 {

3 stringstrDelete="";

4 for(int i=0; i<this.GridView1.Rows.Count; i++)

5 {

6 stringLabel;

7 bool isChecked=((CheckBox)GridView1.Rows[i].FindControl("chkSelect")).Checked;8 Label=((Label)GridView1.Rows[i].FindControl("labXH")).Text;

9 if(isChecked)

10 {

11 strDelete="DB1_1"+"="+Label;

12 }

13 }

14 conn.RunSql("Delete fromDB1 where"+strDelete

15 this.chkSelectAll.Checked=false;

16 GridViewBind();

17 }

18

19 protected void chkSelectAll_CheckedChanged(object sender,EventArgs e)

20 {

21 //遍历GridView行获取CheckBox属性

22 for(int i=0; i<this.GridView1.Rows.Count; i++)

23 {

24 ((Ch e ckBo x)Grid View 1.Ro ws[i].Fin d Co ntro l("chkS e lect")).Ch e cked=this.chkSelectAll.Checked;

25 }

26 }

Webhosting24:$1.48/月起,日本东京NTT直连/AMD Ryzen 高性能VPS/美国洛杉矶5950X平台大流量VPS/1Gbps端口/

Webhosting24宣布自7月1日起开始对日本机房的VPS进行NVMe和流量大升级,几乎是翻倍了硬盘和流量,价格依旧不变。目前来看,日本VPS国内过去走的是NTT直连,服务器托管机房应该是CDN77*(也就是datapacket.com),加上高性能平台(AMD Ryzen 9 3900X+NVMe),还是有相当大的性价比的。此外在6月30日,又新增了洛杉矶机房,CPU为AMD Ryzen 9...

PQS彼得巧 年中低至38折提供台湾彰化HiNet线路VPS主机 200M带宽

在六月初的时候有介绍过一次来自中国台湾的PQS彼得巧商家(在这里)。商家的特点是有提供台湾彰化HiNet线路VPS主机,起步带宽200M,从带宽速率看是不错的,不过价格也比较贵原价需要300多一个月,是不是很贵?当然懂的人可能会有需要。这次年中促销期间,商家也有提供一定的优惠。比如月付七折,年付达到38折,不过年付价格确实总价格比较高的。第一、商家优惠活动年付三八折优惠:PQS2021-618-C...

Sharktech云服务器35折年付33美元起,2G内存/40G硬盘/4TB流量/多机房可选

Sharktech又称SK或者鲨鱼机房,是一家主打高防产品的国外商家,成立于2003年,提供的产品包括独立服务器租用、VPS云服务器等,自营机房在美国洛杉矶、丹佛、芝加哥和荷兰阿姆斯特丹等。之前我们经常分享商家提供的独立服务器产品,近期主机商针对云虚拟服务器(CVS)提供优惠码,优惠后XS套餐年付最低仅33.39美元起,支持使用支付宝、PayPal、信用卡等付款方式。下面以XS套餐为例,分享产品配...

gridview分页为你推荐
免费制作qq空间怎么样免费做qq空间u盘无法读取我的U盘插进电脑后提示无法识别的USB设备,该怎么办?邮箱怎么写正确的邮箱地址怎么写网络明星哪个知道这个网络明星叫什么?快速美白好方法有什么好方法能快速美白?百度抢票浏览器百度手机浏览器怎么抢票 手机百度浏览器抢票方法彩信中心短信中心号码是多少直播加速手机上什么软件可以帮助直播加速,大神们推荐推荐免费免费建站可以不用钱免费做一个网站吗lockdowndiphone4s 完美越狱5.1.1时出现Could not connect to lockdownd。求救啊!!
域名申请 网站空间免备案 in域名注册 万网域名管理 cve-2014-6271 美国主机代购 20g硬盘 有奖调查 cn3 如何安装服务器系统 smtp虚拟服务器 英雄联盟台服官网 atom处理器 lamp兄弟连 葫芦机 godaddy中文 网络安装 达拉斯 linuxvi命令 更多