Hi,

I wish to put the title of each dropdownlist. For example, if I've numbers in my dropdownlist (one, two, and three) I'd like my dropdownlist to be like this:

Numbers
------------
One
Two
Three

Also, I wish to know how to add a title if I bound mu dropdownlist?

Thank you.

Recommended Answers

All 10 Replies

Is this what you want to do...?

<SELECT NAME="fruits">
    <OPTGROUP LABEL="sweet">
      <OPTION LABEL="melon">melon</OPTION>
      <OPTION LABEL="mango">ripe mango</OPTION>
    </OPTGROUP>

    <OPTGROUP LABEL="sour">
      <OPTION LABEL="kiwi">Kiwi</OPTION>
      <OPTION LABEL="lemon">Lemon</OPTION>
      <OPTION LABEL="guava">Guava</OPTION>
    </OPTGROUP>
    <OPTION>Tomato</OPTION>
    <OPTION>Eggplant</OPTION>
  </SELECT>

The optgroup isn't supported by old browsers though. However, they'll just ignore it and your code will still be valid.

Thank you for your answer. I'm newbie to asp.net and I've not understood very well your code. Please can you tell me where should I put this code?

What I want is to add a title to my dropdownlist. Is it possible or what should I do is to consider the title like an item in the dropdownlist control?

P.S: I'm using vs2005.

Thank you.

Eep! I thought this was the html/css thread... *shrinks in shame*

I'll help you out though! *edits signature and searches for ASP tutorials*

Eh.. from what I've read so far, it seems that ASP is like PHP in that you can use it to retrieve records from a database and "echo" html code to the browser...

The first code I gave is the html part of the dropdown menu. That's what you'll use for the response.write portion of your code...

*offers her reputation, soul, and firstborn to misskeen for her stupid attempts at helping*

try this tutorial for generating a dropdown list with ASP

If you were asking how to label the drop down, just use <label></label> tags in HTML.

Hi,

Thank you again for your help. I don't know why I should use a piece of code like php? I was waiting for something usable directly with the control dropdownlist (property, event, etc.). I think that like this it is too difficult.

Thank you again.

Member Avatar for xander85

- You want "Numbers" to be displayed as the title, when numbers are options in the dropdown list? If that is so then it will be a little code in the code behind file (.cs)

Thanks for your interest.

I have a dropdownlist, let us says that it includes cars:

Toyota
Mazda
Honda

I want to add a title to my drop downlist, so it will be like this;

Select a car
--------------
Toyota
Mazda
Honda

Should I add "Select a car" and "--------------" as the same way as I added Toyota, Mazda and Honda? In this way I should take care of the index of the selected item.
Or there is another way to add a title to my dropdownlist? Like this when I check which of these models is selected, only the index of the three models are verified.

Hope that i was clear in my question.

Thanks again

Member Avatar for xander85

Is this along the lines of what you are

<asp:DropDownList ID="Colors" AutoPostBack="True" runat="server">
    <asp:ListItem Selected="True" [B]Enabled="false"[/B] Value=""> Select </asp:ListItem>
    <asp:ListItem Value="Blue"> Blue </asp:ListItem>
    <asp:ListItem Value="Yellow"> Yellow </asp:ListItem>
    <asp:ListItem Value="Grey"> Grey </asp:ListItem>
    <asp:ListItem Value="Black"> Black </asp:ListItem>
</asp:DropDownList>

This code gives me at the execution only : Blue, Yellow, Grey and Black !!!

Good morning,

I needed to do this and I thought it was time I went about and did it.

Normally what would happen is you would set your dropdownlist (ddl) to the datasource and state the field and value items from the datasource.

ddl.DataSource = SQLcmd.ExecuteReader()
        ddl.DataValueField = "ID"
        ddl.DataTextField = "Name"
        ddl.DataBind()

to add the title you have to add an attribute. I found it was easier to traverse through the sql results and do everything all at once like so

SQLdr = SQLcmd.ExecuteReader
        While SQLdr.Read()
            liData = New ListItem
            liData.Value = SQLdr.GetValue(0).ToString
            liData.Text = SQLdr.GetValue(1).ToString
            liData.Attributes.Add("title", SQLdr.GetValue(2).ToString)
            ddl.Items.Insert(i, liData)
            i += 1
        End While
        SQLdr.Close()

Hope this helps :)

Lara

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.