2013-07-24 48 views
1

我遇到了很多麻煩,並且我遵循了大量具有相同問題的人的示例代碼。基本上我有一個gridview,我有一個複選框和另一個鏈接按鈕。如果另一列中的數據綁定鏈接按鈕不爲空(字段不爲空),我想隱藏/禁用一行中的複選框。我已經嘗試了各種方法來做到這一點...(lb!= null),(lb.Text!= null)另外,我試圖通過列號找到控件...沒有運氣有條件地隱藏Gridview的行中的複選框

我是什麼做錯了? (gridview的功能通常比複選框隱藏功能等)

我試着調試和它似乎是它沒有獲得通過的第一個if語句(行類型== ...)

的.cs:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
    LinkButton lb = e.Row.FindControl("LinkButtonPO") as LinkButton; 

    if (lb.CommandArgument != null) 
    { 
     CheckBox cb = e.Row.FindControl("CbPO") as CheckBox; 

     if (cb != null) 
     cb.Visible = false; 

    } 
    } 
} 

的.aspx

<asp:GridView ID="GridView1" 
    CssClass="Gridview" runat="server" 
    AllowSorting="True" 
    AutoGenerateColumns="False" 
    DataKeyNames="Order_ID" 
    DataSourceID="OrderHistoryData" 
    HorizontalAlign="Center" 
    EmptyDataText="No Data to Display" 
    Width="785px" 
    AlternatingRowStyle-CssClass="alt" AllowPaging="True" 
    PagerStyle-CssClass="pager" GridLines="None" PageSize="20" 
    ShowHeaderWhenEmpty="True" OnRowDataBound="GridView1_RowDataBound"> 
       <ItemTemplate> 
       <asp:LinkButton ID="LinkButtonPO" runat="server" CommandArgument='<%# Bind("PO_ID") %>' OnClick="LinkButtonPO_Click" Text='<%# Bind("PO_Lit") %>'></asp:LinkButton> 
      </ItemTemplate> 
      <asp:TemplateField > 
      <ItemTemplate> 
       <asp:CheckBox ID="CbPO" runat="server" OnCheckedChanged="CbPO_CheckedChanged" Visible="true" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
+0

第一行是Type:Header,因此在第一次調試時,它不會傳遞實際檢查DataRow的IF語句。此外,我剛剛通過您的代碼和它的工作適合我。複選框變得隱藏起來。請告訴什麼是PO_ID?它是主鍵嗎?確保PO_ID不爲空 –

回答

0

LinkButton.CommandArgument以這種方式被實現(ILSpy上.NET 4):

public string CommandArgument 
{ 
    get 
    { 
     string text = (string)this.ViewState["CommandArgument"]; 
     if (text != null) 
     { 
      return text; 
     } 
     return string.Empty; 
    } 
    set 
    { 
     this.ViewState["CommandArgument"] = value; 
    } 
} 

因此,通常在ASP.NET中,屬性絕不是null,而是String.Empty

因此改變

if (lb.CommandArgument != null) 
    cb.Visible = false; 

cb.Visible = lb.CommandArgument.Length > 0; 
0

我使用這樣的和對我的作品

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      LinkButton lb = e.Row.FindControl("LinkButtonPO") as LinkButton; 
      CheckBox cb = e.Row.FindControl("CbPO") as CheckBox; 
      if (cb != null) 
       { 
        cb.Visible = false; 
       } 
     } 
    } 
-1

你沒有使用的列和Asp:模板列的LinkBut​​ton的所以使用那。