快来看看!

wpx933 2008-12-29 09:32:42
<asp:GridView ID="GridViewBook" OnPageIndexChanging="GridViewPageExchange" runat="server" AutoGenerateColumns="false" DataKeyNames="ID" OnRowCommand="RowCommandClick" AllowPaging="true" PageSize=5 PagerSettings-Mode="Numeric" >
<Columns>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:ImageButton ID="ImageButtonEdit" CommandName="Edit" ImageUrl="~/Image/edit.gif" runat="server" />
<asp:ImageButton ID="ImageButtonDelete" CommandName="Delete" ImageUrl="~/Image/delete.gif" runat="server" />
</ItemTemplate>
</asp:TemplateField>


<asp:BoundField HeaderText="书编号" DataField="BookID"/>
<asp:BoundField HeaderText="书名" DataField="Bookname"/>
<asp:BoundField HeaderText="中文书名" DataField="ChineseName"/>
<asp:BoundField HeaderText="类别" DataField="BookType" />
<asp:BoundField HeaderText="作者" DataField="Author"/>
<asp:BoundField HeaderText="翻译者" DataField="Translator"/>
<asp:BoundField HeaderText="bookconcern" DataField="BookConcern"/>
<asp:BoundField HeaderText="出版日期" DataField="PublishDate"/>
<asp:BoundField HeaderText="版本" DataField="Edition"/>
<asp:BoundField HeaderText="价格" DataField="Price"/>
<asp:BoundField HeaderText="摘要" DataField="Brief"/>
</Columns>
</asp:GridView>



// GridViewRow 删除,修改操作
protected void RowCommandClick(object sender,GridViewCommandEventArgs e)
{
try
{
int rowIndex = ((e.CommandSource as ImageButton).NamingContainer as GridViewRow).RowIndex;

int id = Convert.ToInt32(GridViewBook.DataKeys[rowIndex].Value);

if (e.CommandName == "Edit")
{
Response.Redirect("AddBook.aspx?Mode=Edit&ID=" + id.ToString/////////////把Try Catch 去掉就会出异常:
//////////// 异常详细信息: System.NullReferenceException:
//////////////// 未将对象引用设置到对象的实例

/////////////////////////
/////////////就是这一行
}
else if (e.CommandName == "Delete")
{

}
}
catch
{

}
}



//分页

protected void GridViewPageExchange(object sender, GridViewPageEventArgs e)
{
GridViewBook.PageIndex = e.NewPageIndex;
BindData();
}
}
}
...全文
27 2 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wpx933 2008-12-29
[Quote=引用 1 楼 flyjimi 的回复:]
出错的地方应该不是你标注的这一行,你是单步调试的?

检查第一行的转换对不对
int rowIndex = ((e.CommandSource as ImageButton).NamingContainer as GridViewRow).RowIndex;
[/Quote]

说说看看,这一行怎么了?
  • 打赏
  • 举报
回复
flyjimi 2008-12-29
出错的地方应该不是你标注的这一行,你是单步调试的?

检查第一行的转换对不对
int rowIndex = ((e.CommandSource as ImageButton).NamingContainer as GridViewRow).RowIndex;
  • 打赏
  • 举报
回复
相关推荐
发帖
C#

10.8w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
帖子事件
创建了帖子
2008-12-29 09:32
社区公告

让您成为最强悍的C#开发者