2010-03-26 75 views
2

我很困惑这一个。数据绑定后,我想修改Gridview Hyperlinkfield中的文本。我在msdn上发现了类似的代码,我无法启动它。修改Gridview中的超链接字段文本

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     e.Row.Cells[2].Text = e.Row.Cells[2].Text + "random text"; 
    } 

我也试过类似的代码在Page_PreRender事件中没有运气。我也尝试在这一行代码之前调用DataBind(),而没有任何帮助。我总是在没有来自数据库的数据的情况下在单元格中获得“随机文本”。由于

+0

由于您已发布的代码 - 除了调用DataBind()之外,prerender,init等中没有任何内容,您会获得多行,单元格2填充了“随机文本”?如果你评论这一行,它充满了数据? – 2010-03-26 12:41:09

+0

这是正确的。 – JohnG 2010-03-26 14:24:52

回答

3

我想你应该尝试像...

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
    HyperLink hyp = (HyperLink)e.Row.Findcontrol("YourHyperlinkID"); 
    hyp.Text = "Your New Text"; 
    } 
} 
+0

您需要为该解决方案创建一个TemplateColumn – citronas 2010-03-26 15:30:20

+0

我试过了,但Hyperlinkfield没有ID属性,所以我无法通过调用FindControl来“查找”它。 – JohnG 2010-03-26 22:57:41

1

,我发现你的问题,而试图figure this out myself(基于上a similar answer)。我看到这个问题来自不久前,但我想回答它,以防其他人发现它寻找答案。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    e.Row.Cells[2].Controls[0].Text = e.Row.Cells[2].Controls[0].Text + "random text"; 
}