Asp.net GridView使用大全(分页实现)
gridview自带的分页功能实现:
要实现grdview分页的功能
操作如下:
1、更改grdview控件的allowpaging属性为true。
2、更改grdview控件的pagesize属性为 任意数值(默认为10)
3、更改grdview控件的pagesetting->mode为numeric等(默认为numeric)该属性为分页样式。
gridview属性设置好了,从页面上也能看到分页样式。
现在开始实现分页的功能:
1、在<<asp:gridview id=......>后添加,onpageindexchanging="gridview1_pageindexchanging"
2、在对应的aspx.cs中添加:
protected void gridview1_pageindexchanging(object sender, gridviewpageeventargs e)
{
gridview1.pageindex = e.newpageindex;
initpage(); //重新绑定gridview数据的函数
}
3、
gridview1.pageindex = e.newpageindex;
完了之后再重新绑定一下gridview。
gridview添加checkbox列实现全选/全部取消功能
首先gridview编辑模版,在模板上添加checkbox控件,之后将新添加列字段转换为templatefiled
后台代码:
using system;
using system.data;
using system.configuration;
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 default5 : system.web.ui.page
{
sqlconnection sqlcon;
string strcon = "data source=(local);database=北风贸易;uid=sa;pwd=sa";
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
bind();
}
}
protected void checkbox2_checkedchanged(object sender, eventargs e)
{
for (int i = 0; i <= gridview1.rows.count - 1; i++)
{
checkbox cbox = (checkbox)gridview1.rows[i].findcontrol("checkbox1");
if (checkbox2.checked == true)
{
cbox.checked = true;
}
else
{
cbox.checked = false;
}
}
}
protected void button2_click(object sender, eventargs e)
{
sqlcon = new sqlconnection(strcon);
sqlcommand sqlcom;
for (int i = 0; i <= gridview1.rows.count - 1; i++)
{
checkbox cbox = (checkbox)gridview1.rows[i].findcontrol("checkbox1");
if (cbox.checked == true)
{
string sqlstr = "delete from 飞狐工作室 where 身份证号码='" + gridview1.datakeys[i].value + "'";
sqlcom = new sqlcommand(sqlstr, sqlcon);
sqlcon.open();
sqlcom.executenonquery();
sqlcon.close();
}
}
bind();
}
protected void button1_click(object sender, eventargs e)
{
checkbox2.checked = false;
for (int i = 0; i <= gridview1.rows.count - 1; i++)
{
checkbox cbox = (checkbox)gridview1.rows[i].findcontrol("checkbox1");
cbox.checked = false;
}
}
public void bind()
{
string sqlstr = "select top 5 * from 飞狐工作室";
sqlcon = new sqlconnection(strcon);
sqldataadapter myda = new sqldataadapter(sqlstr, sqlcon);
dataset myds = new dataset();
sqlcon.open();
myda.fill(myds, "tb_member");
gridview1.datasource = myds;
gridview1.datakeynames = new string[] { "身份证号码" };
gridview1.databind();
sqlcon.close();
}
}
前台主要代码:
<asp:gridview id="gridview1" runat="server" allowsorting="true" autogeneratecolumns="false"
cellpadding="3" font-size="9pt" backcolor="white" bordercolor="#cccccc" borderstyle="none" borderwidth="1px">
<footerstyle backcolor="white" forecolor="#000066" />
<columns>
<asp:templatefield>
<itemtemplate>
<asp:checkbox id="checkbox1" runat="server" />
</itemtemplate>
</asp:templatefield>
<asp:boundfield datafield="身份证号码" headertext="用户id" sortexpression="身份证号码" />
<asp:boundfield datafield="姓名" headertext="用户姓名" sortexpression="姓名"/>
<asp:boundfield datafield="家庭住址" headertext="家庭住址" sortexpression="家庭住址"/>
</columns>
<rowstyle forecolor="#000066" />
<selectedrowstyle backcolor="#669999" font-bold="true" forecolor="white" />
<pagerstyle backcolor="white" forecolor="#000066" horizontalalign="left" />
<headerstyle backcolor="#006699" font-bold="true" forecolor="white" />
</asp:gridview>
<asp:checkbox id="checkbox2" runat="server" autopostback="true" font-size="9pt" oncheckedchanged="checkbox2_checkedchanged"
text="全选" />
<asp:button id="button1" runat="server" font-size="9pt" text="取消" onclick="button1_click" />
<asp:button id="button2" runat="server" font-size="9pt" text="删除" onclick="button2_click" />
推荐阅读
-
Asp.net GridView使用大全(分页实现)
-
ASP.NET Gridview 中使用checkbox删除的2种方法实例分享
-
从零开始实现ASP.NET Core MVC的插件式开发(一) - 使用ApplicationPart动态加载控制器和视图...
-
基于asp.net + easyui框架,一步步学习easyui-datagrid——实现分页和搜索(二) 博客分类: 【EasyUI】 asp.net框架jsonjavascript
-
在Spring Boot中使用Spring-data-jpa实现分页查询
-
JDBC使用游标实现分页查询的方法
-
yii2使用GridView实现数据全选及批量删除按钮示例
-
JDBC使用游标实现分页查询的方法
-
yii2使用gridView实现下拉列表筛选数据
-
asp.net gridview中用checkbox全选的几种实现的区别