Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set CssClass on button in DataGrid

I have a ButtonColumn in a DataGrid:

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

How do I set it's CSS class?

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

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

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.

like image 659
bgs264 Avatar asked Oct 25 '25 05:10

bgs264


1 Answers

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

This gives an additional property ControlStyle-CssClass

e.g.

<asp:ButtonField HeaderText="Edit" ButtonType="Button" Text="Edit" ControlStyle-CssClass="confirmButton" />
like image 184
bgs264 Avatar answered Oct 26 '25 20:10

bgs264