2010-12-01 62 views
0

我需要以编程方式更改GridView内Label的值。GridView事件RowCreated它发生在控件的渲染后?

我使用此代码但不起作用。我无法找到控制。

  • 我的问题是_RowCreated事件在呈现控件之前触发?
  • 如果是,我应该使用哪个事件?

感谢

 protected void uxListAuthorsDisplayer_RowCreated(object sender, GridViewRowEventArgs e) 
     { 
      int myRowIndex = e.Row.RowIndex; 
      //Label myLabel = (Label)e.Row[myRowIndex].FindControl("uxUserIdDisplayer"); 
      if (myRowIndex >= 0) 
      { 
       //e.Row.Cells[3].Text = "Ciao"; 
       Label myLabel = (Label)e.Row.FindControl("uxUserIdDisplayer"); 
       myLabel.Text = "TEST"; 
      } 
     } 

回答

3

你的行中搜索小区索引 “的rowIndex”。

 
protected void uxListAuthorsDisplayer_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
    // Skip the header and footer rows 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     Label myLabel = (Label) e.Row.FindControl("uxUserIdDisplayer"); 
    } 
} 

我更喜欢使用RowDataBound事件,因为当数据被绑定到该行,这样就可以通过的DataBinder.Eval(e.Row.DataItem访问数据之一发生,“数据项目“)

1

GridView.RowCreated事件是适当的。也许你应该检查它是否不是一个标题行,例如,确实没有控件时发现:

if(e.Row.RowType == DataControlRowType.DataRow) 
{ 
    Label myLabel = (Label)e.Row.FindControl("uxUserIdDisplayer"); 
    myLabel.Text = "TEST"; 
}