2013-08-01 58 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