グリッドビューに削除ボタンがありますが、SQL クエリの結果によってはこのボタンが機能しないようにします。
例
「船のコンテナ」を含むグリッドビューがあり、このリストからコンテナの1つを削除したいのですが、「このコンテナを削除できるようにするには、製品から削除してください」というメッセージを表示したいので、船のコンテナが使用されているため、削除されないようにする必要があります。
これを行う方法は次のとおりです。
<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>
削除ボタンを無効にするAllowDocDelete関数に注意してください。この関数は、次のように、ページクラスをスライドさせるように宣言する必要があります。
public bool AllowDocDelete(object item)
{
bool result = false;
//TODO: check your condition
return result;
}
アイテムオブジェクトは、バインドされたエンティティを表します。