gridview关于分页问题
来源:互联网 发布:反对网络语言暴力文章 编辑:程序博客网 时间:2024/06/09 16:06
【转】http://hi.baidu.com/tiandaoliwen/blog/item/d5e1f60f2543312f6159f30b.html
在用ASP.NET做网站后台页面时,我用了GridView控件,但没有选择链接数据源,是自己写的代码来链接数据库的,删除也是自己写的,其中链接数据库是成功了的,但删除出了问题
代码如下:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlstr = "delete from Teacher where TeacherID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
string strCon = "Server=(local);DataBase=JPKC;Uid=sa;Pwd=";
SqlConnection sqlcon = new SqlConnection(strCon);
SqlCommand sqlcom= new SqlCommand(sqlstr, sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
bind();
}
protected void bind()
{
string sqlstr = "select * from Teacher";
string strCon = "Server=(local);DataBase=JPKC;Uid=sa;Pwd=";
SqlConnection sqlcon = new SqlConnection(strCon);
sqlcon.Open();
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
myda.Fill(myds,"Teacher");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "TeacherID" };
GridView1.DataBind();
sqlcon.Close();
}
报的错是这样的
GridView“GridView1”激发了未处理的事件“RowDeleting”。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Web.HttpException: GridView“GridView1”激发了未处理的事件“RowDeleting”。
代码应该没问题,难道事件没有注册
<asp:GridView ID="GridView1" runat="server" OnRowDeleting="GridView1_RowDeleting" >
处理方法有2种:
(1)如果你不需要这个事件,那么删除
GridView2.RowDeleting += .....
这一行.
(2)如果需要这个事件,则添加处理该事件的方法,
方法的具体格式去查MSDN中关于RowDeleting的帮助.
事件如果不是必须动态生成的就在用户控件里写好吧
在上面的代码中,我们设置了一个链接linkbutton,其中指定了commandname为"Delete",commandargument为要删除的记录的ID编号,注意一旦commandname设置为delete这个名称后,gridview中的GridView_RowCommand 和 GridView_Row_Deleting 事件都会被激发接者,我们处理其rowdatabound事件中:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
l.Attributes.Add('onclick", "javascript:return " + "confirm("是否要删除该记录? " +
DataBinder.Eval(e.Row.DataItem, "id") + "')");
}
}
在这段代码中,首先检查是否是datarow,是的话则得到每个linkbutton,再为其添加客户端代码,基本和asp.net 1.1的做法差不多。
之后,当用户选择了确认删除后,我们有两种方法对其进行继续的后续删除处理,因为我们将删除按钮设置为Delete,方法一是在row_command事件中写入如下代码:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int id = Convert.ToInt32(e.CommandArgument);
// 删除记录的专门过程
DeleteRecordByID(id);
}
}
另外一种方法是使用gridview的row_deletting事件,先在页面HTML代码中,添加<asp:GridView DataKeyNames="CategoryID" ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" onRowDeleting="GridView1_RowDeleting">
然后添加row_deleting事件:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int categoryID = (int) GridView1.DataKeys[e.RowIndex].Value;
DeleteRecordByID(categoryID);
}
看你用什么數據源。。。如果有通過數據源控件如:objectdatasource。是不需要寫事件。
如果是用databind。那就要。
PagedDataSource 分页显示,使用介绍
Asp.net提供了三个功能强大的列表控件:DataGrid、DataList和Repeater控件,但其中只有DataGrid控件提供分页功能。相对DataGrid,DataList和Repeater控件具有更高的样式自定义性,所以很多时候我们喜欢使用DataList或Repeater控件来显示数据。
PagedDataSource 类封装 DataGrid 控件的属性,这些属性使 DataGrid 可以执行分页。
PagedDataSource 类的部分公共属性:
AllowCustomPaging获取或设置指示是否启用自定义分页的值。
AllowPaging 获取或设置指示是否启用分页的值。
Count 获取要从数据源使用的项数。
CurrentPageIndex 获取或设置当前页的索引,从0开始计数。
DataSource 获取或设置数据源。
DataSourceCount 获取数据源中的项数。
FirstIndexInPage 获取页中的第一个索引。
IsCustomPagingEnabled获取一个值,该值指示是否启用自定义分页。
IsFirstPage 获取一个值,该值指示当前页是否是首页。
IsLastPage 获取一个值,该值指示当前页是否是最后一页。
IsPagingEnabled 获取一个值,该值指示是否启用分页。
IsReadOnly 获取一个值,该值指示数据源是否是只读的。
IsSynchronized 获取一个值,该值指示是否同步对数据源的访问(线程安全)。
PageCount 获取显示数据源中的所有项所需要的总页数。
备注 :
当启用分页功能时(通过将 AllowPaging 属性设置为 true),请使用 PageCount 属性确定显示数据源中的记录所需的总页数。将数据源中的总记录数除以每页中所显示的记录的数目(由 PageSize 属性指定)并将结果向上舍入后得到此值。
PageSize 获取或设置要在单页上显示的项数。
VirtualCount 获取或设置在使用自定义分页时数据源中的实际项数。
DataGrid控件就是使用PagedDataSource类来实现数据分页显示的,所以DataList和Repeater也同样可以使用PagedDataSource来显示分页。
gridview获取选中行中的某一个值
string i = gvr.Cells[0].Text;
- gridview关于分页问题
- 关于GridView的分页问题
- 关于GridView查询后分页的问题
- 关于Dropdownlist绑定GridView分页出错问题
- 关于GridView分页
- 关于GridView和ObjectDataSource结合的分页问题
- Gridview的分页问题
- GridView分页问题
- GridView 分页问题
- gridview 分页问题
- 关于GridView的分页-万能分页代码
- Gridview中简单分页问题
- 【原】关于GridView文本过长显示的问题和GridView中带有标签模版的分页问题
- 关于gridview排序问题
- 关于GridView分页页码的讨论
- 分页与查询 关于gridview 引入AspNetPager
- 关于DATALIST和GRIDVIEW分页的方法
- 关于GridView分页页码的讨论
- extjs学习笔记(二) Ext.Element类
- 解决GridView“GridView1”激发了未处理的事件“PageIndexChanging”处理
- Windows核心编程:DLL基础
- 医学图像增强4
- 支付宝倪行军:为交易额“裸奔”第一人
- gridview关于分页问题
- myeclipse6.0下svn插件的安装
- Google Code Jam 2009资格赛 题目B Watersheds
- 解决GridView 激发了未处理的事件“PageIndexChanging”
- 测试 orange 中代码产生的问题
- c# equals与==的区别
- 今天——项目组组长
- 一个用C#获取硬件信息的类
- acm 之路