2014-10-06 89 views
0

我想完成一個簡單的任務; a gridview column如果整數等於零,則獲取整數數據打印「激活」並將鏈接按鈕的commandname設置爲激活,否則將鏈接按鈕文本和命令值設置爲不激活。Gridview列鏈接按鈕根據數據更改值

這裏是我迄今爲止

<Columns> 
    <asp:BoundField HeaderText = "User Name" DataField="UserName" 
        HeaderStyle-Width="16%" ItemStyle-HorizontalAlign="Center" /> 
    <asp:BoundField HeaderText = "Role" DataField="RoleNAME" 
        HeaderStyle-Width="14%" ItemStyle-HorizontalAlign="Center" /> 
    <asp:BoundField HeaderText = "Group" DataField="GroupName" 
        HeaderStyle-Width="12%" ItemStyle-HorizontalAlign="Center" /> 
    <asp:ButtonField ButtonType="link" CommandName = "Active" 
        HeaderText="Status" Text="Active" 
        HeaderStyle-Width="10%" ItemStyle-HorizontalAlign="Center" /> 
    <asp:ButtonField ButtonType="link" CommandName = "Edit" 
        HeaderText="" Text="Edit/View" 
        HeaderStyle-Width="10%" ItemStyle-HorizontalAlign="Center" /> 
</Columns> 

代碼隱藏

protected void grdMyQueue_RowdataBound(object sender, GridViewRowEventArgs e) 
{ 
    // In template column, 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     var obj = (User)e.Row.DataItem; 
     if (obj.isDisabled == 0) 
     { 
      LinkButton linkButton = new LinkButton(); 
      linkButton.Text = "Active"; 
      linkButton.Enabled = true; 
      linkButton.CommandName = "Active"; 
      //linkButton.CommandArgument = e.Row. //this might be causing the problem 
      e.Row.Cells[3].Controls.Clear(); 
      e.Row.Cells[3].Controls.Add(linkButton); 
     } 
     else 
     { 
      LinkButton linkButton = new LinkButton(); 
      linkButton.Text = "InActive"; 
      linkButton.Enabled = true; 
      linkButton.CommandName = "InActive"; 
      e.Row.Cells[3].Controls.Add(linkButton); 

     } 

    } 
} 

然而,當我點擊細胞我在onrowcommand功能

protected void OnRowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    int index = Convert.ToInt32(e.CommandArgument); // this is the error line 
    GridViewRow gvRow = userManager.Rows[index]; 
    TableCell usrName = gvRow.Cells[0]; 
    string userName = usrName.Text; 
.... 

得到一個錯誤的積極的LinkBut​​ton我如何設法更改LinkButton文本和CommandName取決於整數數據?

P.S我試圖Eval,但我不能弄清楚怎麼回事....

+0

'linkBut​​ton.CommandArgument = e.Row.RowIndex.ToString();'這解決了問題 – 2014-10-06 19:04:49

回答

0

我會簡單地從數據源返回數據,如預期。然後,我會使用Update Panel來避免Postback將從您的Link Button發生。至於文本修改,我會使用Javascript,所以JavaScript將讀取客戶端上的頁面。所以,當您的數據源返回:

  • 一個
  • 一個
  • 兩個
  • 一個

,JavaScript會分析這些行,然後修改Grid相當無痛人流中的內部單元。

的Javascript的一個例子:如果一個複選框被選中

$(".Activator").each(function() { 
    if ($(this).prev().find(':checkbox').prop('checked')) { 
      $(this).text("Deactivate"); 
    } 
}); 

這個例子將驗證,如果是修改內部text到類.Activator

然後在Grid對前端代碼的內部項目看起來像:

<asp:TemplateField HeaderText="Active"> 
    <ItemTemplate> 

      <asp:CheckBox 
       ID="CustomerCheck" runat="server" 
       Checked='<%# Eval("Active") %>' 
       Enabled="false" /> 

      <asp:LinkButton 
       ID="lbCustomerActive" runat="server" 
       Text="Activate" 
       CommandName="OnActivate" 
       ToolTip='<%# "Activate or Deactivate Course: " + Eval("CourseID") %>' 
       OnClick="CustomerCheck_Click" 
       CssClass="Activator"> 

     </asp:LinkButton> 

    </ItemTemplate> 
</asp:TemplateField> 

正如你可以看到,作爲內部數據被更改的JavaScript會自動爲每一次我調整值Data Source再次綁定。其中CustomerCheck_Click將執行服務器上的代碼,該代碼將讀取包含Row IdCommand Argument。其中Update該特定記錄沒有問題,並重新綁定Grid。 (部分謊言,我正在解析Tooltip)。

你會實例化在服務器端,如:

((LinkButton)sender).ToolTip;