<asp:ListItem Value="0">blank choice</asp:ListItem> ?

Discussion in 'ASP .Net' started by Jim in Arizona, Nov 21, 2006.

  1. Using this:
    <asp:ListItem Value="0"> </asp:ListItem>

    as long as I have a value listed and leave the area between the tags
    blank, when viewed the DropDownList will show a 0 as a choice. I want it
    to be blank.

    I've tried doing this:
    <asp:ListItem Value="0">&nbsp;</asp:ListItem>

    and tried this:
    <asp:ListItem Value="0"> </asp:ListItem>

    and putting nothing in there:
    <asp:ListItem Value="0"> </asp:ListItem>

    In all cases, a zero shows up as the first selection.

    How can I retain the value of zero but keep the choice blank?

    Thanks,
    Jim
     
    Jim in Arizona, Nov 21, 2006
    #1
    1. Advertising

  2. Jim in Arizona

    Karl Seguin Guest

    Unfortunetly, you're best bet will be to add this value in codebehind...

    myDropDown.Items.Insert(0, New ListItem("", 0))

    Karl
    --
    http://www.openmymind.net/
    http://www.fuelindustries.com/


    "Jim in Arizona" <> wrote in message
    news:...
    > Using this:
    > <asp:ListItem Value="0"> </asp:ListItem>
    >
    > as long as I have a value listed and leave the area between the tags
    > blank, when viewed the DropDownList will show a 0 as a choice. I want it
    > to be blank.
    >
    > I've tried doing this:
    > <asp:ListItem Value="0">&nbsp;</asp:ListItem>
    >
    > and tried this:
    > <asp:ListItem Value="0"> </asp:ListItem>
    >
    > and putting nothing in there:
    > <asp:ListItem Value="0"> </asp:ListItem>
    >
    > In all cases, a zero shows up as the first selection.
    >
    > How can I retain the value of zero but keep the choice blank?
    >
    > Thanks,
    > Jim
     
    Karl Seguin, Nov 21, 2006
    #2
    1. Advertising

  3. Karl Seguin wrote:
    > Unfortunetly, you're best bet will be to add this value in codebehind...
    >
    > myDropDown.Items.Insert(0, New ListItem("", 0))
    >
    > Karl


    Well, that's a lot easier and cleaner than what I ended up doing:


    Dim strddlList As String = ""

    If ddlList.SelectedItem.Text = "" Then
    strddlList = "0"
    Else
    strddlList = ddlList.SelectedValue
    End If


    Thanks for your response.

    Jim
     
    Jim in Arizona, Nov 21, 2006
    #3
  4. "Karl Seguin" <> wrote in
    message news:%...
    > Unfortunetly, you're best bet will be to add this value in codebehind...
    >
    > myDropDown.Items.Insert(0, New ListItem("", 0))
    >
    > Karl



    Karl,

    Would your example work in C# ?

    It looks like it could be adapted somewhat to help me with a small problem.

    I want to create a list as follows:

    <asp:ListItem Value="100">100</asp:ListItem>
    <asp:ListItem Value="200">200</asp:ListItem>
    <asp:ListItem Value="300">300</asp:ListItem>
    And so on, until I get to
    <asp:ListItem Value="10000">10000</asp:ListItem>

    I dont want to have to create it manually, as I will no doubt have an error
    crop up somewhere.

    Gary.
     
    Badass Scotsman, Nov 21, 2006
    #4
  5. "Karl Seguin" <> wrote in
    message news:%...
    > Unfortunetly, you're best bet will be to add this value in codebehind...
    >
    > myDropDown.Items.Insert(0, New ListItem("", 0))
    >
    > Karl



    I have used your example to try and do the auto list:

    ==================================
    protected override void OnLoad(EventArgs e)
    {
    int a = 0;
    while (a < 100)
    {
    MyDropDown.Items.Insert(0, new ListItem(""+a+"", ""+a+""));
    a++;
    }
    }
    ==================================

    Do you know how I might go about increasing by an increment of 100 at a time
    instead of 1?

    New to .NET.

    Ta,

    Gary.
     
    Badass Scotsman, Nov 21, 2006
    #5
  6. Jim in Arizona

    Karl Seguin Guest

    you could do a+=100

    but a for loop is probably better for what you want (only a bit though...no
    big deal if you prefer a while loop...)

    Also, you can use Add instead of Insert (insert places it at the index
    specified in the first parameter...my guess is you just wanna append them
    one after another)

    for (int i = 100; i < 10000; i+=100)
    {
    MyDropDow.Items.Add(i.ToString());
    }

    I started at 100 'cuz that's what your example looked like you wanted...you
    could do int i = 0 if you want a 0 in there...
    also note that I'm not creating a new ListItem, the Add method also accepts
    a single string when the text and value are the same thing...just a
    convinience...


    Karl

    --
    http://www.openmymind.net/
    http://www.codebetter.com/


    "Badass Scotsman" <> wrote in message
    news:8rL8h.4717$...
    >
    > "Karl Seguin" <> wrote in
    > message news:%...
    >> Unfortunetly, you're best bet will be to add this value in codebehind...
    >>
    >> myDropDown.Items.Insert(0, New ListItem("", 0))
    >>
    >> Karl

    >
    >
    > I have used your example to try and do the auto list:
    >
    > ==================================
    > protected override void OnLoad(EventArgs e)
    > {
    > int a = 0;
    > while (a < 100)
    > {
    > MyDropDown.Items.Insert(0, new ListItem(""+a+"", ""+a+""));
    > a++;
    > }
    > }
    > ==================================
    >
    > Do you know how I might go about increasing by an increment of 100 at a
    > time instead of 1?
    >
    > New to .NET.
    >
    > Ta,
    >
    > Gary.
    >
     
    Karl Seguin, Nov 22, 2006
    #6
  7. Thanks Karl,

    Will let you know how I get on.

    Gary.


    "Karl Seguin" <> wrote in
    message news:...
    > you could do a+=100
    >
    > but a for loop is probably better for what you want (only a bit
    > though...no big deal if you prefer a while loop...)
    >
    > Also, you can use Add instead of Insert (insert places it at the index
    > specified in the first parameter...my guess is you just wanna append them
    > one after another)
    >
    > for (int i = 100; i < 10000; i+=100)
    > {
    > MyDropDow.Items.Add(i.ToString());
    > }
    >
    > I started at 100 'cuz that's what your example looked like you
    > wanted...you could do int i = 0 if you want a 0 in there...
    > also note that I'm not creating a new ListItem, the Add method also
    > accepts a single string when the text and value are the same thing...just
    > a convinience...
    >
    >
    > Karl
    >
    > --
    > http://www.openmymind.net/
    > http://www.codebetter.com/
    >
    >
    > "Badass Scotsman" <> wrote in message
    > news:8rL8h.4717$...
    >>
    >> "Karl Seguin" <> wrote in
    >> message news:%...
    >>> Unfortunetly, you're best bet will be to add this value in codebehind...
    >>>
    >>> myDropDown.Items.Insert(0, New ListItem("", 0))
    >>>
    >>> Karl

    >>
    >>
    >> I have used your example to try and do the auto list:
    >>
    >> ==================================
    >> protected override void OnLoad(EventArgs e)
    >> {
    >> int a = 0;
    >> while (a < 100)
    >> {
    >> MyDropDown.Items.Insert(0, new ListItem(""+a+"", ""+a+""));
    >> a++;
    >> }
    >> }
    >> ==================================
    >>
    >> Do you know how I might go about increasing by an increment of 100 at a
    >> time instead of 1?
    >>
    >> New to .NET.
    >>
    >> Ta,
    >>
    >> Gary.
    >>

    >
     
    Badass Scotsman, Nov 22, 2006
    #7
  8. "Karl Seguin" <> wrote in
    message news:...
    > you could do a+=100
    >
    > but a for loop is probably better for what you want (only a bit
    > though...no big deal if you prefer a while loop...)
    >
    > Also, you can use Add instead of Insert (insert places it at the index
    > specified in the first parameter...my guess is you just wanna append them
    > one after another)
    >
    > for (int i = 100; i < 10000; i+=100)
    > {
    > MyDropDow.Items.Add(i.ToString());
    > }
    >
    > I started at 100 'cuz that's what your example looked like you
    > wanted...you could do int i = 0 if you want a 0 in there...
    > also note that I'm not creating a new ListItem, the Add method also
    > accepts a single string when the text and value are the same thing...just
    > a convinience...
    >
    >
    > Karl


    Works like a charm, thanks for your help - you guys don't know how much it
    means. I like to think of Usenet as an interactive "learn as you go" type
    of book, one that takes you through your specific problem in simple and easy
    to understand terms. Without people willingto help, I would be much further
    behind in my (two week old) knowledge...

    Gary.
     
    Badass Scotsman, Nov 22, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mad Scientist Jr
    Replies:
    2
    Views:
    469
    Mad Scientist Jr
    Jun 29, 2004
  2. Mickey Segal
    Replies:
    0
    Views:
    894
    Mickey Segal
    Feb 2, 2004
  3. kai
    Replies:
    1
    Views:
    282
    Larry Bates
    May 5, 2006
  4. miles.jg
    Replies:
    16
    Views:
    884
    Alf P. Steinbach
    Nov 14, 2007
  5. Randy
    Replies:
    4
    Views:
    169
    Jeffrey Tan[MSFT]
    Sep 23, 2003
Loading...

Share This Page