2013-08-01 59 views
2

我有一個gridview gvData我想要的是當TransType列中的記錄等於甜品,然後顯示寫,RT。如果其他東西只顯示關閉編輯刪除。在GridView中啓用/禁用鏈接按鈕根據條件

關閉編輯刪除寫RT是一個模板字段

ID TRANSTYPE R  C  TIME  
1  Dessert 12:00 12:05 12  Close Edit Delete Write RT 


<asp:TemplateField ShowHeader="False"> 
<ItemTemplate> 
    <asp:LinkButton ID="lbClose" runat="server" CausesValidation="False" CommandName="CloseClicked" OnClick="CloseClick_Click">Close</asp:LinkButton> 
    <asp:LinkButton ID="lbEdit" runat="server" CausesValidation="False" CommandName="EditRow" OnClick="Edit_Click" CommandArgument='<%# Eval("Id")%>'>Edit</asp:LinkButton> 
    <asp:LinkButton ID="lbDelete" runat="server" CausesValidation="False" CommandName="DeleteRow"OnClick="Delete_Click" OnClientClick="return confirm('Are you sure you want to Delete this Transaction?');">Delete ||</asp:LinkButton> 
    <asp:LinkButton ID="lbWrite" runat="server" CausesValidation="False" CommandName="WriteClicked" OnClick="Write_Click">Write</asp:LinkButton> 
    <asp:LinkButton ID="lbRT" runat="server" CausesValidation="False" CommandName="RT"OnClick="RT_Click">RT</asp:LinkButton> 
</ItemTemplate> 

回答

1

在您gvData _OnRowDataBound,檢查病情,並做出相應的按鈕Visible屬性設置爲false每一行。

  protected void gvData_OnRowDataBound(object sender, GridViewRowEventArgs e) 
      { 
       LinkButton lbClose = (LinkButton)e.Row.Cells[5].FindControl("lbClose"); 
       LinkButton lbEdit = (LinkButton)e.Row.Cells[5].FindControl("lbEdit"); 
       LinkButton lbDelete = (LinkButton)e.Row.Cells[5].FindControl("lbDelete"); 
       LinkButton lbWrite = (LinkButton)e.Row.Cells[5].FindControl("lbWrite"); 
       LinkButton lbRT = (LinkButton)e.Row.Cells[5].FindControl("lbRT"); 

       if(e.Row.Cells[1].Text=="Dessert") 
       { 
        lbClose.Visible = false; 
        lbEdit.Visible = false; 
        lbDelete.Visible = false; 
       } 
       else 
       { 
        lbWrite.Visible = false; 
        lbRT.Visible = false; 
       } 
      } 
+0

我編輯了你的答案,我找到了Trans by lblTrans。請+1我的問題 – Apollo

1

在過去,我做了一個代碼隱藏方法來評估和返回一個布爾值。

protected bool IsTransTypeDessert(string transType) 
{ 
    return transType.ToLower() == "dessert"; 
} 

然後在標記,調用該方法如下所示:

我不記得
<asp:LinkButton ID="lbWrite" runat="server" CausesValidation="False" CommandName="WriteClicked" OnClick="Write_Click" 
Visible='<%# IsTransTypeDessert(Eval("TRANSTYPE") != null ? Eval("TRANSTYPE").ToString() : "") %>'>Write</asp:LinkButton> 

的一件事是,如果IsTransTypeDessert需要返回「真」或「假」或字符串表示如果布爾將工作。測試將決定它。

+0

開箱即用 – mzonerz

+0

很好的答案+1。我編輯,以確保如果匹配正確的類型。 – Si8