Niranjan Godbole Niranjan Godbole - 4 years ago 100
jQuery Question

How to set caption for multiselect dropdownlist in jquery?

Hi I am developing one application with jquery. I have one dropdownlistbox with multiselect options. On page load i want to display Select Any in dropodwnlistbox and currently first value(I am binding from database) is displaying.

I am binding values to dropdown in server side as below.

ddlvendorss.DataSource = VendorsDropDownDT;
ddlvendorss.DataTextField = "Name";
ddlvendorss.DataValueField = "Vendor_ID";

This is my dropdownlistbox.

<b>Awarded To:</b>&nbsp;&nbsp;<asp:ListBox ID="ddlvendorss" runat="server" SelectionMode="Multiple" class="limitedNumbSelect"></asp:ListBox>

This is my jquery code.

$(function () {

My problem is I want to give some caption such as Select any name but my dropdown displaying database value. May I know how can i fix it? Any help would be appreciated. Thank you.

Answer Source

You can do it like this:

$("#ddlvendorss").prepend("<option value='' selected='selected'>select any name</option>");

In case of IE =< 8 you should do it like this:

$("#ddlvendorss").prepend("<option value='' selected='selected'>select any name</option>");
$("#ddlvendorss")[0].options[0].selected = true;

And if you want to do it from code behind:

ddlvendorss.Items.Insert(0, new ListItem("select any name", ""));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download