GridView Disable edit/delete if column has value

自闭症网瘾萝莉.ら 提交于 2019-12-25 02:57:38

问题


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:

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.

I am making use of the default GridView controls:

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

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.


回答1:


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).

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.



来源:https://stackoverflow.com/questions/23680973/gridview-disable-edit-delete-if-column-has-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!