How to add a "dummy" dropdown list item

Last post 07-19-2007 11:56 AM by PeterBrunone. 13 replies.

Sort Posts:

  • How to add a "dummy" dropdown list item

    10-26-2006, 12:19 AM
    • Loading...
    • sg2000
    • Joined on 05-04-2003, 3:34 PM
    • Posts 153

    My Web form contains a dropdown list which is populated by data from the database table; the dropdown list will thus display a list of items from the DB. Since this particular field is optional, I want the dropdown list to be able to show a blank item that the user may select when the field is to be left blank. As a matter of fact, the blank item should be the default selection. How can I do this?

     Thanks in advance.

    sg2000

  • Re: How to add a "dummy" dropdown list item

    10-26-2006, 12:26 AM
    Answer

    In .NET 2.0, you can do the following:

    <asp:DropDownList ID="MyDropDownList" AppendDataBoundItems="true" runat="server">
        <asp:ListItem Text="" Value="" />
    </asp:DropDownList>

    Or you can do it the good old fasion way:

    MyDropDownList.Items.Insert(0, new ListItem())
     

    Ryan Olshan
    ASPInsider | Microsoft MVP, ASP.NET
    http://ryanolshan.com

    How to ask a question
  • Re: How to add a "dummy" dropdown list item

    10-26-2006, 12:38 AM

    hi,

    from code behind also you can do it after binding to datasource this way

    ListItem it = new listitem("text", value);

    ddl.items.add(it);

    hope it helps,

    regards,

    satish.

    Kind Attn: If a reply to your post helped you, kindly mark it as Answered.
    __________________________________________________
    Please save Animals Help World Society For Protection Of Animals,
    Protect these speechless creatures of GOD
  • Re: How to add a "dummy" dropdown list item

    10-26-2006, 11:48 AM
    • Loading...
    • PeterBrunone
    • Joined on 06-19-2002, 5:15 AM
    • I'm standing behind you.
    • Posts 3,663
    • TrustedFriends-MVPs

    ...of course ELB does this automatically; nothing is selected by default (so no need for a bogus item) and you can even have Prompt Text for the user when the selection is empty.

    </shamelessPlug>

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
  • Re: How to add a &quot;dummy&quot; dropdown list item

    10-26-2006, 12:22 PM
    • Loading...
    • sg2000
    • Joined on 05-04-2003, 3:34 PM
    • Posts 153

    StrongTypes:

    Thanks. As you suggested, it is now working.

    sg2000

  • Re: How to add a "dummy" dropdown list item

    10-26-2006, 12:23 PM
    • Loading...
    • sg2000
    • Joined on 05-04-2003, 3:34 PM
    • Posts 153

    satish:

    Thanks. As you suggested, it is now working.

    sg2000

  • Re: How to add a "dummy" dropdown list item

    10-26-2006, 12:29 PM
    • Loading...
    • PeterBrunone
    • Joined on 06-19-2002, 5:15 AM
    • I'm standing behind you.
    • Posts 3,663
    • TrustedFriends-MVPs

    Good work, both of you guys.
     

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
  • Re: How to add a "dummy" dropdown list item

    10-26-2006, 3:12 PM
    Do I get a gold star for this or perhaps a good mark on my report card?
    Ryan Olshan
    ASPInsider | Microsoft MVP, ASP.NET
    http://ryanolshan.com

    How to ask a question
  • Re: How to add a "dummy" dropdown list item

    10-26-2006, 3:30 PM
    • Loading...
    • PeterBrunone
    • Joined on 06-19-2002, 5:15 AM
    • I'm standing behind you.
    • Posts 3,663
    • TrustedFriends-MVPs

    How about a pony?

    I think I have a pony around here somewhere... 

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
  • Re: How to add a "dummy" dropdown list item

    10-26-2006, 9:44 PM

    no worries sg

    thanks to RYAN, PETER also Smile.

    regards,

    satish.

    Kind Attn: If a reply to your post helped you, kindly mark it as Answered.
    __________________________________________________
    Please save Animals Help World Society For Protection Of Animals,
    Protect these speechless creatures of GOD
  • Re: How to add a "dummy" dropdown list item

    07-18-2007, 2:22 PM
    • Loading...
    • chris555
    • Joined on 11-23-2004, 6:05 PM
    • Posts 48

    I stumbled on this post and thought it might be helpful to add what I did:

    I added this to the datagrid control:

    <asp:DropDownList CssClass="txtbox" OnDataBound="addSpace" DataSourceID="SqlDataSource4"

    DataTextField="XXXXXXXX" ID="ddUserIDs" runat="server">

    </asp:DropDownList>

    And on code behind:

    Protected Sub addSpace(ByVal sender As Object, ByVal e As System.EventArgs)

    'add blank to user id ddlist

    ddUserIDs.Items.Insert(0, "")

    End Sub

    I also wrote this to make the default value what my user was logged in:

    Try

    ddUserIDs.SelectedIndex = ddUserIDs.Items.IndexOf(ddUserIDs.Items.FindByValue(userName1))

    Catch ex As Exception

    End Try

  • Re: How to add a "dummy" dropdown list item

    07-18-2007, 2:52 PM
    • Loading...
    • PeterBrunone
    • Joined on 06-19-2002, 5:15 AM
    • I'm standing behind you.
    • Posts 3,663
    • TrustedFriends-MVPs

    If you don't assign a DataValueField, will you be able to FindByValue?

    If you're stuck in ASP.NET 1.x, then you'll have to take the approach you mentioned here, but otherwise you have the SelectedValue property at your disposal and the AppendDataBoundItems property as mentioned above.

    Cheers, 

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
  • Re: How to add a "dummy" dropdown list item

    07-19-2007, 8:18 AM
    • Loading...
    • chris555
    • Joined on 11-23-2004, 6:05 PM
    • Posts 48

    I did not check to see if 1.1 has this available. Should not be dependant on assigning the DataValueField.

    Stuck in 1.1, use approach here? I'm using 2.0. Can you show what you mean in a code example? Thanx.

  • Re: How to add a "dummy" dropdown list item

    07-19-2007, 11:56 AM
    • Loading...
    • PeterBrunone
    • Joined on 06-19-2002, 5:15 AM
    • I'm standing behind you.
    • Posts 3,663
    • TrustedFriends-MVPs

    Chris,

        If you don't have a DataValueField assigned, how can you find by value?  Unless of course it uses the DataTextField in the absence of the DataValueField...

        As for what I mean in 2.0, see Ryan's (StrongTypes) post above.  It's a much simpler way to add static items, er... statically... in the current release.

    Cheers,


     

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
Page 1 of 1 (14 items)