2009-11-20 30 views
0

我有GridView的列看起來就像是:GridView.FindControl() - 的作品,但不能正常

  <Columns> 
      <asp:TemplateField HeaderText="Opcje"> 
       <ItemTemplate> 
        <asp:LinkButton runat="server" Text="Accept" ID="AcceptBtn" CommandName="Accept"/> 
        <asp:LinkButton runat="server" Text="Deny" ID="DenyBtn" CommandName="Deny"/> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 

正在創建一個新的行會,我想改變這兩個的LinkBut​​ton的CommandArgument屬性:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 
    { 
     ((LinkButton)e.Row.FindControl("AcceptBtn")).CommandArgument = myFiles[fileIndex].Name; 
     ((LinkButton)e.Row.FindControl("DenyBtn")).CommandArgument = myFiles[fileIndex].Name; 
    } 

的問題是,代碼似乎沒有改變任何東西,當我點擊AcceptBtn,下面的代碼被調用:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     if (e.CommandName == "Accept") 
     { 
      string ss = (string)e.CommandArgument; 
      ... 
     } 
    } 

ss =「」。爲什麼?如果這個頁面是PostedBack,那麼這兩個CommandArguments都被清除了?

回答

2

嘗試在RowDataBound事件中設置CommandArgument而不是RowCreated

1

你需要使用RowDataBound事件如..

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     ((LinkButton)e.Row.FindControl("AcceptBtn")).CommandArgument = myFiles[fileIndex].Name; 
     ((LinkButton)e.Row.FindControl("DenyBtn")).CommandArgument = myFiles[fileIndex].Name; 
    } 
} 
0

,你在哪裏設置

myFiles[fileIndex].Name; 

和它的每一個組成部分?

相關問題