Setting the CSS Class for a Button in a DataGrid's ButtonColumn

This demo illustrates how to programmatically set the CSS class for a button in a DataGrid's ButtonColumn. The Button's CSS class is set to buttonStyle, which is defined as follows:

.buttonStyle
{
    FONT-WEIGHT: bold;
    FONT-SIZE: 9pt;
    TEXT-TRANSFORM: capitalize;
    COLOR: white;
    FONT-FAMILY: Verdana;
    BACKGROUND-COLOR: navy
}
    


 TitleDate WrittenViews
Bidirectional Sorting with Up and Down Arrows in the Header6/15/20059533
Creating a Fully Editable DataGrid12/15/200413418
Common DataGrid Mistakes11/6/200317819
Creating Custom Columns for the ASP.NET Datagrid9/10/200313538
DataGrid Made Compliant with Section 508 of the Web Accessibility Initiative Guidelines8/19/20035933
Creating a Multi-table DataGrid in ASP.NET8/15/20039234
Building a Pageable, Sortable DataGrid8/14/20036530
Summarizing Data with ROLLUP7/30/20037058
Including Subheadings in a Datagrid7/26/20037200
Deciding When to Use the DataGrid, DataList or Repeater7/23/20036542
Hierarchical Data Binding in ASP.NET7/1/20038679
Understanding the Differences Among the DataGrid, DataList, and Repeater5/21/200314513
Displaying Records in a DataGrid in Random Order5/10/20035687
Creating a Fully Editable DataGrid4/18/200323203
Building a Master/Detail DataGrid4/2/200321720
Binding a DataGrid to an ADO Recordset3/28/20034462
Adding a New Record to the DataGrid in the Footer2/12/200311647
Displaying a Column's Sum in the Footer2/5/20035615
Retain Scrollback Position After Postback in DataGrid1/15/20035714
MouseOver Coloring for a DataGrid12/17/200215162
Advanced DataGrid Usage11/11/20028549
Displaying Custom Classes in a DataGrid10/23/20024298
Creating a Custom DataGridColumn Class10/2/20024259
Having a Client-Side MessageBox Display When Deleting a Record9/4/20025373
Adding a DropDownList Web Control to the DataGrid's Editing Interface8/7/20026522
Highlighting Search Keywords in a DataGrid Web Control7/24/20024592
DataGrids, DataSets, and XML Strings7/17/20023802
XML, the DataSet, and a DataGrid5/29/20024156
Transferring the Datagrid Data Between Web Forms5/8/20025023
An Extensive Examination of the DataGrid Web Control: Part 14/5/200221145
Top Questions About the DataGrid Web Control1/10/200215308


Source Code
<%@ Page Language="VB" %>
<script runat="server">

Sub Page_Load(sender as Object, e as EventArgs)
  If Not Page.IsPostBack then
    dgArticles.DataSource = GetArticles("DateAuthored DESC")
    dgArticles.DataBind()
  End If
End Sub
    
    
Function GetArticles(sortBy as String) As System.Data.SqlClient.SqlDataReader
    Dim connectionString As String = "<i>connString</i>"
    Dim sqlConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionString)
    
    Dim queryString As String = "SELECT ArticleID, [Articles].[Title], [Articles].[URL], [Articles].[dateAuthored], [Articles"& _
"].[Comments], [Articles].[ClickThroughs] FROM [Articles] ORDER BY " & sortBy
    Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
    
    sqlConnection.Open
    Dim dataReader As System.Data.SqlClient.SqlDataReader = sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
    
    Return dataReader
End Function
    


Sub dgArticles_ItemCreated(sender As Object, e As DataGridItemEventArgs)
   If e.Item.ItemType = ListItemType.Item OR _
      e.Item.ItemType = ListItemType.AlternatingItem then
          
        'Get the button
        Dim myButton as Button = CType(e.Item.Cells(0).Controls(0), Button)
        myButton.CssClass = "buttonStyle"
   End If
End Sub 'Item_Created 
</script>


<link rel="stylesheet" type="text/css" href="/demos/ButtonStyle.css">

<form runat="server">
   <p>
       <asp:DataGrid id="dgArticles" runat="server" AutoGenerateColumns="False" 
                  Font-Size="10pt" Font-Names="Verdana" 
                  OnItemCreated="dgArticles_ItemCreated">
           <HeaderStyle font-size="13pt" font-bold="True" horizontalalign="Center" forecolor="White" backcolor="#006699"></HeaderStyle>
           <AlternatingItemStyle backcolor="#EEEEEE"></AlternatingItemStyle>
           <Columns>
               <asp:ButtonColumn Text="Do Something" ButtonType="PushButton" />
               <asp:HyperLinkColumn DataNavigateUrlField="ArticleID" DataNavigateUrlFormatString="/Articles/Goto.aspx?ID={0}" DataTextField="Title" SortExpression="Title" HeaderText="Title"></asp:HyperLinkColumn>
               <asp:BoundColumn DataField="DateAuthored" SortExpression="dateAuthored DESC" HeaderText="Date Written" DataFormatString="{0:d}"></asp:BoundColumn>
               <asp:BoundColumn DataField="ClickThroughs" SortExpression="ClickThroughs DESC" HeaderText="Views" DataFormatString="{0:d}" ItemStyle-HorizontalAlign="Center"></asp:BoundColumn>
           </Columns>
       </asp:DataGrid>  
   </p>
</form>    
    


[Return to the FAQ...]