Hi,
How to show dropdownlist first item blank . can i set the index property to -1 instead of 0.

Dani AI

Generated

— setting SelectedIndex = -1 is not the right way to force a visible blank entry. is correct for plain HTML, and for ASP.NET server controls there are a few reliable patterns that avoid odd runtime behavior and keep validation simple.

  • Add a blank item in the DropDownList markup so the control renders a first empty choice:
<asp:DropDownList ID="ddl" runat="server">
    <asp:ListItem Text="" Value="" />
</asp:DropDownList>
  • If the list is data-bound, either insert the blank item after binding:
ddl.DataSource = mySource;
ddl.DataBind();
ddl.Items.Insert(0, new ListItem(String.Empty, String.Empty));

or preserve static items during binding by enabling AppendDataBoundItems and adding the blank item before calling DataBind().

  • In ASP.NET MVC you can supply an option label when building the select helper:
@Html.DropDownListFor(m => m.Id, Model.SelectList, "")

Common pitfalls and tips:

  • Do not rely on SelectedIndex = -1 to produce a blank UI; browsers will still show something if options exist and server-side behavior is inconsistent.
  • If you need to force the user to pick a non-empty value, use a RequiredFieldValidator (set InitialValue to the blank value) or equivalent client/server validation.
  • If a data-bind is wiping out inserted items, check AppendDataBoundItems or re-insert the placeholder after binding.

These approaches keep the UI predictable, make validation straightforward, and work whether the dropdown is static or data-bound.

Recommended Answers

All 2 Replies

This is an HTML question as far as I am concerned:

<select name="name">
<option value=''></option>
<!-- All other option fields here using your asp as required-->
</select>

Thanks simplypixie it's wonderfull i can use same in server control also.

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.