Results 1 to 4 of 4

Thread: DropDownList.SelectedIndex = -1

  1. #1

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    DropDownList.SelectedIndex = -1

    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:
    VB Code:
    1. dropdownlist.selectedindex = -1
    This doesn't appear to be working with my ASP page. ???

  2. #2
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Re: DropDownList.SelectedIndex = -1

    Code:
    ddl.SelectedItem.Selected = false;
    or
    Code:
    foreach(ListItem li in ddl.Items)
    {
        if(li.Selected) li.Selected = !li.Selected;//aka false
    }
    Magiaus

    If I helped give me some points.

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: DropDownList.SelectedIndex = -1

    The proper way:
    Code:
    dropdownlist1.ClearSelection();
    You should realize that a dropdownlist allows you to set a null value listItem on the frontside aspx.
    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>
    Then in the code behind, you can check whether the user selected an item by testing for an empty string value.
    Code:
    if (dropdownlist1.SelectedValue.Length == 0)
    				Response.Redirect("http://www.google.com");

  4. #4
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Re: DropDownList.SelectedIndex = -1

    very nice to know this .ClearSelection is.....
    Magiaus

    If I helped give me some points.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width