GridView控件禁用编辑/删除,如果列值 [英] GridView Disable edit/delete if column has value

查看:488
本文介绍了GridView控件禁用编辑/删除,如果列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GridView控件与编辑和删除功能。我想编辑和删除要禁用或隐藏,如果在 GridView控件该行特定的列有一个值:

I have a GridView with edit and delete enabled. I want the edit and delete to be disabled or hidden if a specific column for that row in the GridView has a value:

在这种情况下,我想默认的编辑/删除 GridView控件的选项都无法使用为具有在名称列中的值。这意味着只有3列将可编辑。

In this case I want the default edit/delete options of the GridView do be unavailable for that has a value in the Name column. This means only the 3rd column will be editable.

我利用默认的GridView控件:

I am making use of the default GridView controls:

<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />

如果编辑在点击&LT;&EditItemTemplate的GT; 的显示行和沃德和部门值可以改变。在 GridView控件连接到链接到具有CRUD操作的类一个ObjectDataSource。

If Edit is clicked the <EditItemTemplate> for the row is displayed and the Ward and Sector values can be changed. The GridView is attached to an ObjectDataSource that is linked to a class that has the CRUD operations.

推荐答案

您可以解析出ID字段的值,并把编辑的可见度/删除GridView的RowDataBound事件的按钮。以下是我如何做到这一点,下面code进去你的GridView的RowDataBound事件处理程序:

You can parse out the value of the ID field and set the visibility of the edit/delete buttons in the gridview's RowDataBound event. Here's how I do it, the following code goes in your gridview's RowDataBound event handler:

    If e.Row.RowType = DataControlRowType.DataRow Then
        'Get reference to the button you want to hide/show
        Dim delButton As ImageButton = CType(e.Row.Cells(3).Controls(2), ImageButton)
        'check your data for value
        Dim bIsThereAValue as Boolean = DoACheckForAValue(YourGridView.DataKeys(e.Row.RowIndex).Value)
        delButton.Visible = bIsThereAValue
    End If

这个方法很适合我,我经常用这哪里该项目可能在其他实体使用,我禁用能够删除的项目(例如该网格显示可用的照片,但图片是在画廊中使用,然后我禁用能够删除照片,直到它是一个画廊的不再是一部分)。

This method works well for me, I often use this where this item may be used in another entity, I disable being able to delete the item (for example this grid shows available pictures, but the picture is being used in a gallery, then I disable being able to delete the picture until it is no longer part of a gallery).

您会想要确保你在为你列的值做适当的检查,我不确定随便,如果IsNothing会工作,或者你将不得不比较的String.Empty或东西,但它不该' T为太困难了。

You'll want to make sure you do an adequate check for your column's value in, I'm unsure offhand, if IsNothing would work, or if you would have to compare to string.empty or something, but it shouldn't be too difficult.

这篇关于GridView控件禁用编辑/删除,如果列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆