如何在数据网格按钮设置的CssClass网格、按钮、数据、如何在

2023-09-08 09:41:48 作者:哀家要男宠三千

我有一个DataGrid中ButtonColumn:

I have a ButtonColumn in a DataGrid:

<asp:ButtonColumn HeaderText="Edit" ButtonType="PushButton" Text="Edit" />

我如何设置它的CSS类?

How do I set it's CSS class?

我可以看到这样做的唯一方法,是挂钩的RowDataBound事件:

The only way I can see to do it, is hooking to the RowDataBound event:

Protected Sub dgSchedule_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgSchedule.ItemDataBound
    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        DirectCast(e.Item.Cells(6).Controls(0), Button).CssClass = "confirmButton"

    End If
End Sub

我只是觉得必须有一个更合适的方法。如果我添加/删除列,我必须回到这里,记得要改变,会发生什么列6 ...

I just feel like there must be a neater way. What happens if I add/remove columns, I'll have to come back here and remember to change column 6...

我试图用一个 TemplateColumn中和往常一样 ASP:按钮 - 这个工作,但然后单击它并没有火,我需要消防网格的ItemCommand事件。

I tried using a TemplateColumn and a usual asp:Button - This worked, but then clicking it did not fire the ItemCommand event of the grid which I need to fire.

推荐答案

我已经用一个GridView,而不是一个DataGrid解决了这个。其实不知道为什么,我在第一时间使用DataGrid中。

I have resolved this by using a GridView instead of a DataGrid. Actually not sure why I used a DataGrid in the first place.

这给出了一个附加属性 ControlStyle-的CssClass

This gives an additional property ControlStyle-CssClass

例如。

<asp:ButtonField HeaderText="Edit" ButtonType="Button" Text="Edit" ControlStyle-CssClass="confirmButton" />