快来看看!

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();
}
}
}
...全文
55 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
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;

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

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

试试用AI创作助手写篇文章吧