2012-10-01 73 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值将被传递给该函数。 –