I would like to be able to set a drop down list to null, but prevent the user from selecting a null value. In my windows app I was able to use:
This doesn't appear to be working with my ASP page. ???VB Code:
dropdownlist.selectedindex = -1
Printable View
I would like to be able to set a drop down list to null, but prevent the user from selecting a null value. In my windows app I was able to use:
This doesn't appear to be working with my ASP page. ???VB Code:
dropdownlist.selectedindex = -1
orCode:ddl.SelectedItem.Selected = false;
Code:foreach(ListItem li in ddl.Items)
{
if(li.Selected) li.Selected = !li.Selected;//aka false
}
The proper way:
You should realize that a dropdownlist allows you to set a null value listItem on the frontside aspx.Code:dropdownlist1.ClearSelection();
Then in the code behind, you can check whether the user selected an item by testing for an empty string value.Code:<asp:dropdownlist id="dropdownlist1" runat="server">
<asp:listitem value="">--Select A Color--</asp:listitem>
<asp:listitem value="red">Red</asp:listitem>
<asp:listitem value="blue">Blue</asp:listitem>
</asp:dropdownlist>
Code:if (dropdownlist1.SelectedValue.Length == 0)
Response.Redirect("http://www.google.com");
very nice to know this .ClearSelection is.....