Highlight DropDownList Item Color in ASP.Net

This article explains:

  • How to dynamically bind DropDownList from SQL Server database table.
  • How to highlight DropDownList item color based on condition or flag.
Database

I used the following table and Stored Procedure to show that.



Use the following script to create the preceding table Mas_Department.
  1. Create Table Mas_Department  
  2. (  
  3. DeptId int Primary Key,  
  4. DeptName varchar(50),  
  5. Status bit  
  6. )  
To insert the records:
  1. insert Mas_Department values(1, 'IT', 1)  
  2. insert Mas_Department values(2, 'HR', 1)  
  3. insert Mas_Department values(3, 'Accounts', 1)  
  4. insert Mas_Department values(7, 'FieldTechnician', 0)  
  5. insert Mas_Department values(4, 'Sales', 1)  
  6. insert Mas_Department values(5, 'BPO', 0)  
  7. insert Mas_Department values(6, 'Finance', 1)  
  8. insert Mas_Department values(7, 'FieldTechnician', 0)  
Use the following script to create the Stored Procedure USP_Select_Mas_Department to get all the Departments.
  1. Create procedure [dbo].[USP_Select_Mas_Department] AS Begin  
  2. Select  
  3. D.DeptId,  
  4. D.DeptName as Department,  
  5. D.Status  
  6. From  
  7. Mas_Department D END  
Create the project

Go to "Start", then click "All Programs" and open "Microsoft Visual Studio 2010".

In "Microsoft Visual Studio 2015", go to "File", then click "New" and select "Project". Now select "C#" and click "ASP.NET Empty Web Application".

Provide the project the name as you wish and specify the location.

Web.Config

Create the connection string in the Web.Config file as shown in the following:
  1. <connectionStrings>  
  2.     <add name="conStr"   
  3.          connectionString="Password=1234; User ID=sa; Database=DB_Jai; Data Source=."   
  4.          providerName="System.Data.SqlClient"/>  
  5. </connectionStrings>  
Next right-click on the Solution Explorer and add a web form to your project.

.aspx file

Design the .aspx page as in the following:
  1. <form id="form1" runat="server">  
  2.     <div align="center">  
  3.         <fieldset style="width: 30%;">  
  4.             <legend>Highlight DropDownList Item Color</legend>  
  5.             <table style="width: 25%;">  
  6.                 <tr>  
  7.                     <td>  
  8.                         Department :  
  9.                     </td>  
  10.                     <td>  
  11.                         <asp:DropDownList ID="ddlDepartment" runat="server">  
  12.                         </asp:DropDownList>  
  13.                     </td>  
  14.                 </tr>  
  15.             </table>  
  16.         </fieldset>  
  17.     </div>  
  18.     </form>  
CodeBehind

Add the following namespaces:
  1. using System.Data;  
  2. using System.Data.SqlClient;  
  3. using System.Configuration;  
ConnectionString 
  1. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);  
User Defined Functions
  1. #region User Defined Methods  
  2. // To dynamically Bind/Fill DropDownList from Sql Server Database table, and highlight the color for flag 0 records.   
  3. protected void ddlBindDepartments() {  
  4.     SqlDataAdapter adp = new SqlDataAdapter("USP_Select_Mas_Department", con);  
  5.     adp.SelectCommand.CommandType = CommandType.StoredProcedure;  
  6.     DataSet ds = new DataSet();  
  7.     adp.Fill(ds);  
  8.     if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) {  
  9.         ddlDepartment.DataSource = ds;  
  10.         ddlDepartment.DataValueField = "DeptId";  
  11.         ddlDepartment.DataTextField = "Department";  
  12.         ddlDepartment.DataBind();  
  13.     }  
  14.   
  15.     DataView dv = ds.Tables[0].DefaultView;  
  16.     dv.RowFilter = "Status='" + false + "'";  
  17.     foreach(DataRowView dr in dv) {  
  18.         foreach(ListItem item in ddlDepartment.Items) {  
  19.             if (dr["DeptId"].ToString() == item.Value.ToString()) {  
  20.                 item.Attributes.Add("style""background-color:#3399FF;color:white;font-weight:bold;");  
  21.             }  
  22.         }  
  23.     }  
  24. }  
  25.  
  26. #endregion  
Page Event Handlers
  1. #region Page Event Handlers  
  2.   
  3. // Page Load  
  4. protected void Page_Load(object sender, EventArgs e) {  
  5.     ddlDepartment.SelectedIndex = -1;  
  6.     if (!Page.IsPostBack) {  
  7.         ddlBindDepartments();  
  8.     }  
  9. }  
  10.  
  11. #endregion  
Output

BPO, Field Technician are (Flag 0) highlighted in the following output:



I hope you enjoyed this article. Please provide your valuable suggestions and feedback.

Up Next
    Ebook Download
    View all
    Learn
    View all