2012-10-01 71 views
2

我在我的GridView中有一個刪除按鈕,但我希​​望此按鈕不能工作,具體取決於sql查詢的結果。Gridview中的條件刪除按鈕

一個例子

我有「船集裝箱」一個gridview,我想刪除此列表中的容器之一,但我想顯示一條消息「,以便能夠刪除該容器,請將其從產品中刪除「,以便在使用船舶集裝箱時,我需要防止它被刪除。

+0

知道您要刪除船舶集裝箱並沒有什麼幫助,但看看您嘗試過的內容會有幫助。 –

+0

其實我沒有嘗試,因爲我沒有任何想法。在我看來,我可以通過rowcommand捕獲按鈕點擊,但是如果在rowcommand內部寫入條件是什麼樣的呢?什麼代碼將取消該刪除命令。 – HOY

+0

您只需首先執行查詢以檢查容器是否仍在使用,然後您可以顯示消息或執行刪除命令。 –

回答

3

這裏是你可以做到這一點的方式:禁用刪除按鈕AllowDocDelete功能

<asp:GridView ID="EntityGridView" runat="server" DataKeyNames="DocumentId" AutoGenerateColumns="False" 
    AllowPaging="True" AllowSorting="False" SkinID="GridViewSmall" OnRowCommand="EntityGridView_RowCommand" 
    OnPageIndexChanged="EntityGridView_PageIndexChanged"> 
    <Columns> 
     <asp:TemplateField ItemStyle-CssClass="TemplateFieldThreeColumns"> 
      <ItemTemplate> 
       <asp:ImageButton ID="ImageButton1" ImageAlign="Top" runat="server" ImageUrl='<% #ResolveImageUrl(Eval("Extension").ToString()) %>' 
        ToolTip='<%# Eval("Extension").ToString() %>' CommandName="Select" CommandArgument='<%# Eval("DocumentId") %>' /> 
      <asp:ImageButton ID="btnDelete" runat="server" ToolTip="<% $resources:AppResource,Delete %>" 
       SkinID="DeletePage" OnClientClick="<%# GetDeleteConfirmation(Resources.AppResource.ConfirmDocumentDelete) %>" 
       CommandName="CustomDelete" CommandArgument='<%# Eval("DocumentId") %>' Visible='<% #AllowDocDelete(Container.DataItem) %>' /> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:BoundField DataField="Title" HeaderText="<% $resources:AppResource,Title %>" /> 
     <asp:BoundField DataField="Author" HeaderText="<% $resources:AppResource,Author %>" /> 
     <asp:BoundField DataField="FileName" HeaderText="<% $resources:AppResource,FileName %>" /> 
     <asp:BoundField DataField="Created" HeaderText="<% $resources:AppResource,Created %>" /> 
    </Columns> 
    <EmptyDataTemplate> 
     <asp:Label ID="EmptyLabel" runat="server" Text='<%# Resources.AppResource.NoContentToDisplay %>' CssClass="NoDataLabel"></asp:Label> 
    </EmptyDataTemplate> 
</asp:GridView> 

採取通知。這個函數應該在你的頁面類中聲明,如下所示:

public bool AllowDocDelete(object item) 
    { 
     bool result = false; 
     //TODO: check your condition 
     return result; 
    } 

Item對象表示綁定實體。

+0

我很接近我的解決方案,你的代碼是AllowDocDelete(Container.DataItem),我不明白這個部分,我需要將我的containerId作爲字符串參數發送到AllowDocDelete函數,通常我通過<%#Bind 「ContainerId」)%>,但如何在函數內部做到這一點? – HOY

+1

調用AllowDocDelete(Eval(「ContainerId」))和ContainerId值將被傳遞給該函數。 –