Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 977849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:58:08+00:00 2026-05-16T03:58:08+00:00

I have a DropDownList on an ASP.NET page that gets populated by a SQL

  • 0

I have a DropDownList on an ASP.NET page that gets populated by a SQL database.

<asp:DropDownList ID="ddlName" runat="server"></asp:DropDownList>

The population is down in the code behind file:

ddlName.DataSource = SqlDataSource1;
ddlName.DataValueField = (this.ddlName.SelectedValue);
ddlName.DataTextField = "ccName";
ddlName.DataBind();

I was wondering if it was possible to change the background or text color of an item in the list based on it’s value?


I just noticed that the example below works when the page first loads but on postback the text color disappears even though that is where the code is. Is there something I am missing?

protected override void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            ddlName.DataSource = SqlDataSource5;
            ddlName.DataValueField = (this.ddlName.SelectedValue);
            ddlName.DataTextField = "ccName";
            ddlName.DataBind();


            foreach (ListItem item in ddlName.Items)
            {
                if (item.Value == "Item 1")
                {
                    item.Attributes.Add("style", "color:red");
                }

                if (item.Value == "Item 2")
                {
                    item.Attributes.Add("style", "color:red");
                }
            }

        }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:58:08+00:00Added an answer on May 16, 2026 at 3:58 am

    Yup you sure can, add this to your page load event.

    foreach(ListItem item in ddlName.Items) {
        if(item.Value == "someStringValue") {
            item.Attributes.Add("style", "color:red")
        }
    }
    

    If that doesn’t work, you can move this code to the DataBound event of the Drop Down List.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dropdownlist in my asp.net page <asp:RadioButtonList ID=rdblstcookinglevel runat=server CellPadding=0 CellSpacing=0 >
I have a DropDownList called (DDL) in ASP.net page, I want that DDL contains
so I am connecting a sql database. I have a asp.net page and when
I have a DropDownList inside a repeater control in an ASP.NET page that has
I have an ASP.NET DropDownList control that retrieves the first and last name from
I have two DropDownList <asp:DropDownList ID=ddl1 runat=server Width=173px CssClass=ddl > </asp:DropDownList> <asp:DropDownList ID=ddl2 runat=server
I have a DropDownList with an SelectedIndexChanged event listener <asp:DropDownList ID=LoanOptionCombo runat=server AutoProstBack=True> Listener
I'm just learning to program...I have a dropdownlist like this. <div> <asp:DropDownList ID=DropDownList1 runat=server>
I have a basic DropDownList bound to a ObjectDataSource: <asp:DropDownList ID=DropDownList1 runat=server AutoPostBack=True DataSourceID=objDataSource1
I am using ASP.NET forms version 3.5 in VB I have a dropdownlist that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.