How to set CssClass to a button in a DataGrid

I have a ButtonColumn in a DataGrid:

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

How to set CSS class?

The only way I can do this is to hook into 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 that there needs to be a tidier way. What happens if I add / remove columns, I have to go back here and don't forget to change column 6 ...

I tried to use TemplateColumnregular asp:Button. This worked, but then clicked it so as not to fire the ItemCommand event of the grid that I need to fire.

+3
source share
3 answers

I solved this using a GridView instead of a DataGrid. I don’t really know why I used DataGrid in the first place.

This gives an additional property. ControlStyle-CssClass

eg.

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

:

1. CssClass ButtonColumn:

<asp:ButtonColumn CssClass="myStyle" ...></asp:ButtonColumn>

HTML-:

<td class="myStyle">
    <input type=button name=select ...>
</td>

, CSS . , :

.myClass
{
   /*Your style attributes go here*/
}

, CSS:

.myClass INPUT
{
   /*Your style attributes go here*/
}

2. TemplateColumn ButtonColumn CssClass :

<asp:TemplateColumn HeaderText="Delete">
    <ItemTemplate>
        <asp:Button ID="DeleteButton" runat=server Text="Delete" CssClass="myClass" CommandName="Delete" />
    </ItemTemplate>
</asp:TemplateColumn>

CSS , :

.myClass
{
   /*Your style attributes go here*/
}
0

DataGrid, GridView, OnItemDataBound.

OnItemDataBound="mydatagrid_ItemDataBound"

CSS :

//ON DATA BIND
protected void mydatagrid_ItemDataBound(object sender, DataGridItemEventArgs e)
    if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer) {
        Button myButton= (Button)e.Item.Cells[5].Controls[0];
        myButton.Attributes["class"] = "buttonClass";   
    }
}
0

All Articles