How to use ListBox CSS Style in asp.net c#

ListBox CssClass
asp.net listbox web server control has many built in design properties such as BackColor, ForeColor, BorderColor, BorderStyle, BorderWidth, Font, Height, Width, SkinID, EnableTheming etc. by using this properties we can change look and feel of a listbox control. if we want to more customize the listbox control's design then it provide a more useful property name CssClass.

listbox CssClass property get or set Cascading Style Sheet (CSS) class rendered by the listbox control on the client.

if we use css to customize a server control's design, we should use either inline css style or a separate css file. using both of them for a control cause unexpected results. if client browser does not support css then this property will have no effect.

the following example source code demonstrate us how can we extend design of a listbox control using its CssClass property.
ListBoxCssClass.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ListBox1.CssClass = "ListBoxCssClass";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to use ListBox CssClass in asp.net</title>
    <style type="text/css">
        .ListBoxCssClass
        {
            color:GhostWhite;
            background-color:DarkOliveGreen;
            font-family:Courier New;
            font-size:large;
            font-style:italic;
            }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:MediumVioletRed; font-style:italic;">
            How to use ListBox CssClass in asp.net
        </h2>
        <hr width="375" align="left" color="Violet" />
        <asp:ListBox 
            ID="ListBox1"
            runat="server"
            >
            <asp:ListItem Text="MediumAquaMarine"></asp:ListItem>
            <asp:ListItem Text="MediumPurple"></asp:ListItem>
            <asp:ListItem Text="MediumSlateBlue"></asp:ListItem>
            <asp:ListItem Text="MediumSpringGreen"></asp:ListItem>
            <asp:ListItem Text="MediumVioletRed"></asp:ListItem>
        </asp:ListBox>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Apply ListBox CssClass"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples