I have a DropDownList that is populated from a datasource. After it is bound, I place an empty field at the top of the list so that it appears blank to the user (creating a sort of ‘default item’). I have some code behind handling the SelectedIndexChanged event, but it doesn’t really need to be executed if the user were to select the empty ListItem.
Here is my code:
.aspx
<asp:DropDownList ID="dropDownList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="dropDownList_SelectedIndexChanged">
</asp:DropDownList>
C# Codebehind adding the blank ListItem
dropDownList.Items.Insert(0, new ListItem(String.Empty, String.Empty));
dropDownList.SelectedIndex = 0;
Since I don’t need the page to do a postback when the user clicks just this specific index, I want to disable postback entirely for just this listitem. Is this even possible? If so, how?
Set
disabled="disabled", this will make the item not selectable (and not do postback)Alternatively if you want to be able to select the first (empty) item but not do postback, do this: