gridview rowdatabound 分页问题

isabel_cui 2009-04-03 12:39:11

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindgrvTopic();
}
}

protected void grvTopic_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grvTopic.PageIndex = e.NewPageIndex;
BindgrvTopic();
}
protected void BindgrvTopic()
{
this.grvTopic.DataKeyNames = new string[] { "TOPICID" };
grvTopic.DataSource = profileservice.GetAll();
grvTopic.DataBind();
}
catch (Exception exc)
{
Logger.Write(exc.Message.ToString(), "TopicView");
}
}
protected void grvTopic_RowDataBound(object sender, GridViewRowEventArgs e)
{
int topicId = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.DataItemIndex > -1)
{
topicId = Convert.ToInt32(grvTopic.DataKeys[e.Row.DataItemIndex].Value); ImageButton btn1 = e.Row.Cells[0].FindControl("btn1") as ImageButton;
Label lbl = e.Row.Cells[0].FindControl("lbl") as Label;
Image img1 = e.Row.Cells[0].FindControl("img1") as Image;
Image img2 = e.Row.Cells[0].FindControl("img2") as Image;
if (topicService.GetByTopicId(topicId).TopicLevel.ToString() == ConstService.MAINTOPICLEVEL)
{
if (topicService.GetSubTopic(topicService.GetByTopicId(topicId).TopicName).Count > 0)
{
btn1.Visible = true;
}
else
{
img2.Visible = true;
}
}
else
{
e.Row.Visible = false;
img1.Visible = true;
}
}
}
}


分页之后,红色的那里就会报错,说argument out of range
求各位达人帮帮我~~
...全文
91 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
pdsnet 2009-04-03
  • 打赏
  • 举报
回复
sohighthesky 2009-04-03
  • 打赏
  • 举报
回复
RowDataBound好像是在数据行绑定数据时触发,而此时DataKeys有可以还为空,
建议使用楼上说的方法
sohighthesky 2009-04-03
  • 打赏
  • 举报
回复
RowDataBound好像是在数据行绑定数据时触发,而此时DataKeys有可以还为空,
建议使用楼上说的方法
itcrazyman 2009-04-03
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 niuniuhuang 的回复:]
在RowDataBound我一般不用DataKeys,一般这样写
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = e.Row.DataItem as DataRowView;
int topicId=Convert.ToInt32(drv["ID"]);
}
[/Quote]
mark up
niuniuhuang 2009-04-03
  • 打赏
  • 举报
回复
在RowDataBound我一般不用DataKeys,一般这样写
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = e.Row.DataItem as DataRowView;
int topicId=Convert.ToInt32(drv["ID"]);
}
阿非 2009-04-03
  • 打赏
  • 举报
回复
你设断点 看一下 grvTopic.DataKeys 的长度 与e.Row.DataItemIndex 分别是多少

报哪个错 是说e.Row.DataItemIndex 比grvTopic.DataKeys 的长度 大,下标越界

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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