Listbox containing duplicate items - correct selection not maintained on postback?

Discussion in 'ASP .Net' started by Adam Clauss, Sep 7, 2005.

  1. Adam Clauss

    Adam Clauss Guest

    I have a page containing a list box. This list may contain duplicate
    items - in which the ORDER is important.
    ex:
    a
    b
    b
    a

    is significant as compared to:
    b
    a
    a
    b

    even though the list contains the same elements. The user has buttons on
    the page to reorder the list.

    The problem is that, say in the first example, the bottom 'a' is selected.
    Then a button is pressed that causes a postback. The re-selected item is
    now the FIRST a. Likewise, if the second 'b' is selected, on postback the
    FIRST b will be selected.

    The following page demonstrates this:

    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
    Inherits="testListbox.WebForm1" smartNavigation="True"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
    <asp:ListBox id="ListBox1" runat="server">
    <asp:ListItem Value="item1">item1</asp:ListItem>
    <asp:ListItem Value="item2">item2</asp:ListItem>
    <asp:ListItem Value="item1">item1</asp:ListItem>
    </asp:ListBox>
    </form>
    </body>
    </HTML>


    There was no code added to the codebehind page. (I just created a simple
    web app to demonstrate this).

    Is there anyway to resolve this issue such that the correct element is
    selected (by index rather than value)?
    --
    Adam Clauss
     
    Adam Clauss, Sep 7, 2005
    #1
    1. Advertising

  2. Adam,

    Yes the Listbox object has a SelectedIndex property, it gets or sets the
    SelectedIndex.

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "Adam Clauss" <> wrote in message
    news:...
    >I have a page containing a list box. This list may contain duplicate
    >items - in which the ORDER is important.
    > ex:
    > a
    > b
    > b
    > a
    >
    > is significant as compared to:
    > b
    > a
    > a
    > b
    >
    > even though the list contains the same elements. The user has buttons on
    > the page to reorder the list.
    >
    > The problem is that, say in the first example, the bottom 'a' is selected.
    > Then a button is pressed that causes a postback. The re-selected item is
    > now the FIRST a. Likewise, if the second 'b' is selected, on postback the
    > FIRST b will be selected.
    >
    > The following page demonstrates this:
    >
    > <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
    > AutoEventWireup="false" Inherits="testListbox.WebForm1"
    > smartNavigation="True"%>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    > <HTML>
    > <HEAD>
    > <title>WebForm1</title>
    > <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    > <meta name="CODE_LANGUAGE" Content="C#">
    > <meta name="vs_defaultClientScript" content="JavaScript">
    > <meta name="vs_targetSchema"
    > content="http://schemas.microsoft.com/intellisense/ie5">
    > </HEAD>
    > <body>
    > <form id="Form1" method="post" runat="server">
    > <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
    > <asp:ListBox id="ListBox1" runat="server">
    > <asp:ListItem Value="item1">item1</asp:ListItem>
    > <asp:ListItem Value="item2">item2</asp:ListItem>
    > <asp:ListItem Value="item1">item1</asp:ListItem>
    > </asp:ListBox>
    > </form>
    > </body>
    > </HTML>
    >
    >
    > There was no code added to the codebehind page. (I just created a simple
    > web app to demonstrate this).
    >
    > Is there anyway to resolve this issue such that the correct element is
    > selected (by index rather than value)?
    > --
    > Adam Clauss
    >
    >
    >
     
    S. Justin Gengo, Sep 7, 2005
    #2
    1. Advertising

  3. Adam Clauss

    Adam Clauss Guest

    Huh? Yes, I'm aware of that property.
    The problem is I'm not setting the selection - ASP.NET is automatically.

    --
    Adam Clauss

    "S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
    message news:%23v3w59%...
    > Adam,
    >
    > Yes the Listbox object has a SelectedIndex property, it gets or sets the
    > SelectedIndex.
    >
    > --
    > Sincerely,
    >
    > S. Justin Gengo, MCP
    > Web Developer / Programmer
    >
    > www.aboutfortunate.com
    >
    > "Out of chaos comes order."
    > Nietzsche
    > "Adam Clauss" <> wrote in message
    > news:...
    >>I have a page containing a list box. This list may contain duplicate
    >>items - in which the ORDER is important.
    >> ex:
    >> a
    >> b
    >> b
    >> a
    >>
    >> is significant as compared to:
    >> b
    >> a
    >> a
    >> b
    >>
    >> even though the list contains the same elements. The user has buttons on
    >> the page to reorder the list.
    >>
    >> The problem is that, say in the first example, the bottom 'a' is
    >> selected. Then a button is pressed that causes a postback. The
    >> re-selected item is now the FIRST a. Likewise, if the second 'b' is
    >> selected, on postback the FIRST b will be selected.
    >>
    >> The following page demonstrates this:
    >>
    >> <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
    >> AutoEventWireup="false" Inherits="testListbox.WebForm1"
    >> smartNavigation="True"%>
    >> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    >> <HTML>
    >> <HEAD>
    >> <title>WebForm1</title>
    >> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    >> <meta name="CODE_LANGUAGE" Content="C#">
    >> <meta name="vs_defaultClientScript" content="JavaScript">
    >> <meta name="vs_targetSchema"
    >> content="http://schemas.microsoft.com/intellisense/ie5">
    >> </HEAD>
    >> <body>
    >> <form id="Form1" method="post" runat="server">
    >> <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
    >> <asp:ListBox id="ListBox1" runat="server">
    >> <asp:ListItem Value="item1">item1</asp:ListItem>
    >> <asp:ListItem Value="item2">item2</asp:ListItem>
    >> <asp:ListItem Value="item1">item1</asp:ListItem>
    >> </asp:ListBox>
    >> </form>
    >> </body>
    >> </HTML>
    >>
    >>
    >> There was no code added to the codebehind page. (I just created a simple
    >> web app to demonstrate this).
    >>
    >> Is there anyway to resolve this issue such that the correct element is
    >> selected (by index rather than value)?
    >> --
    >> Adam Clauss
    >>
    >>
    >>

    >
    >
     
    Adam Clauss, Sep 7, 2005
    #3
  4. Yes,

    You'll have to set it yourself.

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "Adam Clauss" <> wrote in message
    news:...
    > Huh? Yes, I'm aware of that property.
    > The problem is I'm not setting the selection - ASP.NET is automatically.
    >
    > --
    > Adam Clauss
    >
    > "S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
    > message news:%23v3w59%...
    >> Adam,
    >>
    >> Yes the Listbox object has a SelectedIndex property, it gets or sets the
    >> SelectedIndex.
    >>
    >> --
    >> Sincerely,
    >>
    >> S. Justin Gengo, MCP
    >> Web Developer / Programmer
    >>
    >> www.aboutfortunate.com
    >>
    >> "Out of chaos comes order."
    >> Nietzsche
    >> "Adam Clauss" <> wrote in message
    >> news:...
    >>>I have a page containing a list box. This list may contain duplicate
    >>>items - in which the ORDER is important.
    >>> ex:
    >>> a
    >>> b
    >>> b
    >>> a
    >>>
    >>> is significant as compared to:
    >>> b
    >>> a
    >>> a
    >>> b
    >>>
    >>> even though the list contains the same elements. The user has buttons
    >>> on the page to reorder the list.
    >>>
    >>> The problem is that, say in the first example, the bottom 'a' is
    >>> selected. Then a button is pressed that causes a postback. The
    >>> re-selected item is now the FIRST a. Likewise, if the second 'b' is
    >>> selected, on postback the FIRST b will be selected.
    >>>
    >>> The following page demonstrates this:
    >>>
    >>> <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
    >>> AutoEventWireup="false" Inherits="testListbox.WebForm1"
    >>> smartNavigation="True"%>
    >>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    >>> <HTML>
    >>> <HEAD>
    >>> <title>WebForm1</title>
    >>> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    >>> <meta name="CODE_LANGUAGE" Content="C#">
    >>> <meta name="vs_defaultClientScript" content="JavaScript">
    >>> <meta name="vs_targetSchema"
    >>> content="http://schemas.microsoft.com/intellisense/ie5">
    >>> </HEAD>
    >>> <body>
    >>> <form id="Form1" method="post" runat="server">
    >>> <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
    >>> <asp:ListBox id="ListBox1" runat="server">
    >>> <asp:ListItem Value="item1">item1</asp:ListItem>
    >>> <asp:ListItem Value="item2">item2</asp:ListItem>
    >>> <asp:ListItem Value="item1">item1</asp:ListItem>
    >>> </asp:ListBox>
    >>> </form>
    >>> </body>
    >>> </HTML>
    >>>
    >>>
    >>> There was no code added to the codebehind page. (I just created a
    >>> simple web app to demonstrate this).
    >>>
    >>> Is there anyway to resolve this issue such that the correct element is
    >>> selected (by index rather than value)?
    >>> --
    >>> Adam Clauss
    >>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    S. Justin Gengo, Sep 7, 2005
    #4
  5. Adam Clauss

    Adam Clauss Guest

    In EVERY postback I do?? This page has a lot of postback's on it... that
    doesn't seem very efficient.

    --
    Adam Clauss

    "S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
    message news:eDE2eK$...
    > Yes,
    >
    > You'll have to set it yourself.
    >
    > --
    > Sincerely,
    >
    > S. Justin Gengo, MCP
    > Web Developer / Programmer
    >
    > www.aboutfortunate.com
    >
    > "Out of chaos comes order."
    > Nietzsche
    > "Adam Clauss" <> wrote in message
    > news:...
    >> Huh? Yes, I'm aware of that property.
    >> The problem is I'm not setting the selection - ASP.NET is automatically.
    >>
    >> --
    >> Adam Clauss
    >>
    >> "S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
    >> message news:%23v3w59%...
    >>> Adam,
    >>>
    >>> Yes the Listbox object has a SelectedIndex property, it gets or sets the
    >>> SelectedIndex.
    >>>
    >>> --
    >>> Sincerely,
    >>>
    >>> S. Justin Gengo, MCP
    >>> Web Developer / Programmer
    >>>
    >>> www.aboutfortunate.com
    >>>
    >>> "Out of chaos comes order."
    >>> Nietzsche
    >>> "Adam Clauss" <> wrote in message
    >>> news:...
    >>>>I have a page containing a list box. This list may contain duplicate
    >>>>items - in which the ORDER is important.
    >>>> ex:
    >>>> a
    >>>> b
    >>>> b
    >>>> a
    >>>>
    >>>> is significant as compared to:
    >>>> b
    >>>> a
    >>>> a
    >>>> b
    >>>>
    >>>> even though the list contains the same elements. The user has buttons
    >>>> on the page to reorder the list.
    >>>>
    >>>> The problem is that, say in the first example, the bottom 'a' is
    >>>> selected. Then a button is pressed that causes a postback. The
    >>>> re-selected item is now the FIRST a. Likewise, if the second 'b' is
    >>>> selected, on postback the FIRST b will be selected.
    >>>>
    >>>> The following page demonstrates this:
    >>>>
    >>>> <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
    >>>> AutoEventWireup="false" Inherits="testListbox.WebForm1"
    >>>> smartNavigation="True"%>
    >>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    >>>> <HTML>
    >>>> <HEAD>
    >>>> <title>WebForm1</title>
    >>>> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    >>>> <meta name="CODE_LANGUAGE" Content="C#">
    >>>> <meta name="vs_defaultClientScript" content="JavaScript">
    >>>> <meta name="vs_targetSchema"
    >>>> content="http://schemas.microsoft.com/intellisense/ie5">
    >>>> </HEAD>
    >>>> <body>
    >>>> <form id="Form1" method="post" runat="server">
    >>>> <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
    >>>> <asp:ListBox id="ListBox1" runat="server">
    >>>> <asp:ListItem Value="item1">item1</asp:ListItem>
    >>>> <asp:ListItem Value="item2">item2</asp:ListItem>
    >>>> <asp:ListItem Value="item1">item1</asp:ListItem>
    >>>> </asp:ListBox>
    >>>> </form>
    >>>> </body>
    >>>> </HTML>
    >>>>
    >>>>
    >>>> There was no code added to the codebehind page. (I just created a
    >>>> simple web app to demonstrate this).
    >>>>
    >>>> Is there anyway to resolve this issue such that the correct element is
    >>>> selected (by index rather than value)?
    >>>> --
    >>>> Adam Clauss
    >>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    Adam Clauss, Sep 7, 2005
    #5
  6. Adam,

    Maybe I'm misunderstanding what you're doing. Does the item order in the
    list box change with every post back. If the list box has viewstate enabled
    on it it should stay exactly the same unless you re-bind it or the user
    changes the order. Every time a post-back is done where the user changes the
    order you'll have to set the proper index if .Net can't do it itself. But
    that certainly shouldn't be on every post back.

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "Adam Clauss" <> wrote in message
    news:...
    > In EVERY postback I do?? This page has a lot of postback's on it... that
    > doesn't seem very efficient.
    >
    > --
    > Adam Clauss
    >
    > "S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
    > message news:eDE2eK$...
    >> Yes,
    >>
    >> You'll have to set it yourself.
    >>
    >> --
    >> Sincerely,
    >>
    >> S. Justin Gengo, MCP
    >> Web Developer / Programmer
    >>
    >> www.aboutfortunate.com
    >>
    >> "Out of chaos comes order."
    >> Nietzsche
    >> "Adam Clauss" <> wrote in message
    >> news:...
    >>> Huh? Yes, I'm aware of that property.
    >>> The problem is I'm not setting the selection - ASP.NET is automatically.
    >>>
    >>> --
    >>> Adam Clauss
    >>>
    >>> "S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
    >>> message news:%23v3w59%...
    >>>> Adam,
    >>>>
    >>>> Yes the Listbox object has a SelectedIndex property, it gets or sets
    >>>> the SelectedIndex.
    >>>>
    >>>> --
    >>>> Sincerely,
    >>>>
    >>>> S. Justin Gengo, MCP
    >>>> Web Developer / Programmer
    >>>>
    >>>> www.aboutfortunate.com
    >>>>
    >>>> "Out of chaos comes order."
    >>>> Nietzsche
    >>>> "Adam Clauss" <> wrote in message
    >>>> news:...
    >>>>>I have a page containing a list box. This list may contain duplicate
    >>>>>items - in which the ORDER is important.
    >>>>> ex:
    >>>>> a
    >>>>> b
    >>>>> b
    >>>>> a
    >>>>>
    >>>>> is significant as compared to:
    >>>>> b
    >>>>> a
    >>>>> a
    >>>>> b
    >>>>>
    >>>>> even though the list contains the same elements. The user has buttons
    >>>>> on the page to reorder the list.
    >>>>>
    >>>>> The problem is that, say in the first example, the bottom 'a' is
    >>>>> selected. Then a button is pressed that causes a postback. The
    >>>>> re-selected item is now the FIRST a. Likewise, if the second 'b' is
    >>>>> selected, on postback the FIRST b will be selected.
    >>>>>
    >>>>> The following page demonstrates this:
    >>>>>
    >>>>> <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
    >>>>> AutoEventWireup="false" Inherits="testListbox.WebForm1"
    >>>>> smartNavigation="True"%>
    >>>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    >>>>> <HTML>
    >>>>> <HEAD>
    >>>>> <title>WebForm1</title>
    >>>>> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    >>>>> <meta name="CODE_LANGUAGE" Content="C#">
    >>>>> <meta name="vs_defaultClientScript" content="JavaScript">
    >>>>> <meta name="vs_targetSchema"
    >>>>> content="http://schemas.microsoft.com/intellisense/ie5">
    >>>>> </HEAD>
    >>>>> <body>
    >>>>> <form id="Form1" method="post" runat="server">
    >>>>> <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
    >>>>> <asp:ListBox id="ListBox1" runat="server">
    >>>>> <asp:ListItem Value="item1">item1</asp:ListItem>
    >>>>> <asp:ListItem Value="item2">item2</asp:ListItem>
    >>>>> <asp:ListItem Value="item1">item1</asp:ListItem>
    >>>>> </asp:ListBox>
    >>>>> </form>
    >>>>> </body>
    >>>>> </HTML>
    >>>>>
    >>>>>
    >>>>> There was no code added to the codebehind page. (I just created a
    >>>>> simple web app to demonstrate this).
    >>>>>
    >>>>> Is there anyway to resolve this issue such that the correct element is
    >>>>> selected (by index rather than value)?
    >>>>> --
    >>>>> Adam Clauss
    >>>>>
    >>>>>
    >>>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    S. Justin Gengo, Sep 7, 2005
    #6
  7. Adam Clauss

    Adam Clauss Guest

    Again - try running the code/page I gave on the first post.
    I do not have to change or reorder anything to cause this problem.

    Problem in short - have a page with a button (with no event actually bound
    to it), and a listbox with contents as:
    a
    b
    b
    a

    Select the bottom a. Then press the button.
    The first a is what shows up as selected when the page reappears.

    Select the bottom b. Then press the button.
    The first b is what shows up as selected when the page reappears.

    --
    Adam Clauss

    "S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
    message news:u1VgFo$...
    > Adam,
    >
    > Maybe I'm misunderstanding what you're doing. Does the item order in the
    > list box change with every post back. If the list box has viewstate
    > enabled on it it should stay exactly the same unless you re-bind it or the
    > user changes the order. Every time a post-back is done where the user
    > changes the order you'll have to set the proper index if .Net can't do it
    > itself. But that certainly shouldn't be on every post back.
    >
    > --
    > Sincerely,
    >
    > S. Justin Gengo, MCP
    > Web Developer / Programmer
    >
    > www.aboutfortunate.com
    >
    > "Out of chaos comes order."
    > Nietzsche
    > "Adam Clauss" <> wrote in message
    > news:...
    >> In EVERY postback I do?? This page has a lot of postback's on it... that
    >> doesn't seem very efficient.
    >>
    >> --
    >> Adam Clauss
    >>
    >> "S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
    >> message news:eDE2eK$...
    >>> Yes,
    >>>
    >>> You'll have to set it yourself.
    >>>
    >>> --
    >>> Sincerely,
    >>>
    >>> S. Justin Gengo, MCP
    >>> Web Developer / Programmer
    >>>
    >>> www.aboutfortunate.com
    >>>
    >>> "Out of chaos comes order."
    >>> Nietzsche
    >>> "Adam Clauss" <> wrote in message
    >>> news:...
    >>>> Huh? Yes, I'm aware of that property.
    >>>> The problem is I'm not setting the selection - ASP.NET is
    >>>> automatically.
    >>>>
    >>>> --
    >>>> Adam Clauss
    >>>>
    >>>> "S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
    >>>> message news:%23v3w59%...
    >>>>> Adam,
    >>>>>
    >>>>> Yes the Listbox object has a SelectedIndex property, it gets or sets
    >>>>> the SelectedIndex.
    >>>>>
    >>>>> --
    >>>>> Sincerely,
    >>>>>
    >>>>> S. Justin Gengo, MCP
    >>>>> Web Developer / Programmer
    >>>>>
    >>>>> www.aboutfortunate.com
    >>>>>
    >>>>> "Out of chaos comes order."
    >>>>> Nietzsche
    >>>>> "Adam Clauss" <> wrote in message
    >>>>> news:...
    >>>>>>I have a page containing a list box. This list may contain duplicate
    >>>>>>items - in which the ORDER is important.
    >>>>>> ex:
    >>>>>> a
    >>>>>> b
    >>>>>> b
    >>>>>> a
    >>>>>>
    >>>>>> is significant as compared to:
    >>>>>> b
    >>>>>> a
    >>>>>> a
    >>>>>> b
    >>>>>>
    >>>>>> even though the list contains the same elements. The user has
    >>>>>> buttons on the page to reorder the list.
    >>>>>>
    >>>>>> The problem is that, say in the first example, the bottom 'a' is
    >>>>>> selected. Then a button is pressed that causes a postback. The
    >>>>>> re-selected item is now the FIRST a. Likewise, if the second 'b' is
    >>>>>> selected, on postback the FIRST b will be selected.
    >>>>>>
    >>>>>> The following page demonstrates this:
    >>>>>>
    >>>>>> <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
    >>>>>> AutoEventWireup="false" Inherits="testListbox.WebForm1"
    >>>>>> smartNavigation="True"%>
    >>>>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    >>>>>> <HTML>
    >>>>>> <HEAD>
    >>>>>> <title>WebForm1</title>
    >>>>>> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    >>>>>> <meta name="CODE_LANGUAGE" Content="C#">
    >>>>>> <meta name="vs_defaultClientScript" content="JavaScript">
    >>>>>> <meta name="vs_targetSchema"
    >>>>>> content="http://schemas.microsoft.com/intellisense/ie5">
    >>>>>> </HEAD>
    >>>>>> <body>
    >>>>>> <form id="Form1" method="post" runat="server">
    >>>>>> <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
    >>>>>> <asp:ListBox id="ListBox1" runat="server">
    >>>>>> <asp:ListItem Value="item1">item1</asp:ListItem>
    >>>>>> <asp:ListItem Value="item2">item2</asp:ListItem>
    >>>>>> <asp:ListItem Value="item1">item1</asp:ListItem>
    >>>>>> </asp:ListBox>
    >>>>>> </form>
    >>>>>> </body>
    >>>>>> </HTML>
    >>>>>>
    >>>>>>
    >>>>>> There was no code added to the codebehind page. (I just created a
    >>>>>> simple web app to demonstrate this).
    >>>>>>
    >>>>>> Is there anyway to resolve this issue such that the correct element
    >>>>>> is selected (by index rather than value)?
    >>>>>> --
    >>>>>> Adam Clauss
    >>>>>>
    >>>>>>
    >>>>>>
    >>>>>
    >>>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    Adam Clauss, Sep 8, 2005
    #7
  8. Adam Clauss

    Damien Guest

    Adam Clauss wrote:
    > I have a page containing a list box. This list may contain duplicate
    > items - in which the ORDER is important.
    > ex:
    > a
    > b
    > b
    > a
    >
    > is significant as compared to:
    > b
    > a
    > a
    > b
    >
    > even though the list contains the same elements. The user has buttons on
    > the page to reorder the list.
    >
    > The problem is that, say in the first example, the bottom 'a' is selected.
    > Then a button is pressed that causes a postback. The re-selected item is
    > now the FIRST a. Likewise, if the second 'b' is selected, on postback the
    > FIRST b will be selected.
    >
    > The following page demonstrates this:
    >
    > <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
    > Inherits="testListbox.WebForm1" smartNavigation="True"%>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    > <HTML>
    > <HEAD>
    > <title>WebForm1</title>
    > <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    > <meta name="CODE_LANGUAGE" Content="C#">
    > <meta name="vs_defaultClientScript" content="JavaScript">
    > <meta name="vs_targetSchema"
    > content="http://schemas.microsoft.com/intellisense/ie5">
    > </HEAD>
    > <body>
    > <form id="Form1" method="post" runat="server">
    > <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
    > <asp:ListBox id="ListBox1" runat="server">
    > <asp:ListItem Value="item1">item1</asp:ListItem>
    > <asp:ListItem Value="item2">item2</asp:ListItem>
    > <asp:ListItem Value="item1">item1</asp:ListItem>
    > </asp:ListBox>
    > </form>
    > </body>
    > </HTML>
    >

    The listbox needs to have distinct item values in order to distinguish
    the items within it.

    Try, for instance:

    <asp:ListItem Value="item1_instance1">item1</asp:ListItem>
    <asp:ListItem Value="item2_instance1">item2</asp:ListItem>
    <asp:ListItem Value="item1_instance2">item1</asp:ListItem>

    Or anything else. If you're only concerned with the textual contents of
    the listbox for everything else, the values could simply be 1, 2, 3,
    ....

    Damien
     
    Damien, Sep 8, 2005
    #8
  9. Adam Clauss

    Adam Clauss Guest

    Ah... that did it. Yeah - only the text is significant, so I was able to
    disregard the actual values (just plugged in an incrementing value).

    --
    Adam Clauss

    "Damien" <> wrote in message
    news:...
    > Adam Clauss wrote:
    >> I have a page containing a list box. This list may contain duplicate
    >> items - in which the ORDER is important.
    >> ex:
    >> a
    >> b
    >> b
    >> a
    >>
    >> is significant as compared to:
    >> b
    >> a
    >> a
    >> b
    >>
    >> even though the list contains the same elements. The user has buttons on
    >> the page to reorder the list.
    >>
    >> The problem is that, say in the first example, the bottom 'a' is
    >> selected.
    >> Then a button is pressed that causes a postback. The re-selected item is
    >> now the FIRST a. Likewise, if the second 'b' is selected, on postback
    >> the
    >> FIRST b will be selected.
    >>
    >> The following page demonstrates this:
    >>
    >> <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
    >> AutoEventWireup="false"
    >> Inherits="testListbox.WebForm1" smartNavigation="True"%>
    >> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    >> <HTML>
    >> <HEAD>
    >> <title>WebForm1</title>
    >> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    >> <meta name="CODE_LANGUAGE" Content="C#">
    >> <meta name="vs_defaultClientScript" content="JavaScript">
    >> <meta name="vs_targetSchema"
    >> content="http://schemas.microsoft.com/intellisense/ie5">
    >> </HEAD>
    >> <body>
    >> <form id="Form1" method="post" runat="server">
    >> <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
    >> <asp:ListBox id="ListBox1" runat="server">
    >> <asp:ListItem Value="item1">item1</asp:ListItem>
    >> <asp:ListItem Value="item2">item2</asp:ListItem>
    >> <asp:ListItem Value="item1">item1</asp:ListItem>
    >> </asp:ListBox>
    >> </form>
    >> </body>
    >> </HTML>
    >>

    > The listbox needs to have distinct item values in order to distinguish
    > the items within it.
    >
    > Try, for instance:
    >
    > <asp:ListItem Value="item1_instance1">item1</asp:ListItem>
    > <asp:ListItem Value="item2_instance1">item2</asp:ListItem>
    > <asp:ListItem Value="item1_instance2">item1</asp:ListItem>
    >
    > Or anything else. If you're only concerned with the textual contents of
    > the listbox for everything else, the values could simply be 1, 2, 3,
    > ...
    >
    > Damien
    >
     
    Adam Clauss, Sep 8, 2005
    #9
  10. Adam Clauss

    Adam Clauss Guest

    OK - maybe partially my fault for giving a bad example in my first post.
    Damien hit the problem.

    What the first example should have said was that:
    a
    b
    b
    a **selected

    is significant as to:
    a **selected
    b
    b
    a

    Order itself is important too, but that wasn't the actual problem.
    Apologies for being unclear and making things confusing :)

    --
    Adam Clauss

    "Adam Clauss" <> wrote in message
    news:...
    > Again - try running the code/page I gave on the first post.
    > I do not have to change or reorder anything to cause this problem.
    >
    > Problem in short - have a page with a button (with no event actually bound
    > to it), and a listbox with contents as:
    > a
    > b
    > b
    > a
    >
    > Select the bottom a. Then press the button.
    > The first a is what shows up as selected when the page reappears.
    >
    > Select the bottom b. Then press the button.
    > The first b is what shows up as selected when the page reappears.
    >
    > --
    > Adam Clauss
    >
    > "S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
    > message news:u1VgFo$...
    >> Adam,
    >>
    >> Maybe I'm misunderstanding what you're doing. Does the item order in the
    >> list box change with every post back. If the list box has viewstate
    >> enabled on it it should stay exactly the same unless you re-bind it or
    >> the user changes the order. Every time a post-back is done where the user
    >> changes the order you'll have to set the proper index if .Net can't do it
    >> itself. But that certainly shouldn't be on every post back.
    >>
    >> --
    >> Sincerely,
    >>
    >> S. Justin Gengo, MCP
    >> Web Developer / Programmer
    >>
    >> www.aboutfortunate.com
    >>
    >> "Out of chaos comes order."
    >> Nietzsche
    >> "Adam Clauss" <> wrote in message
    >> news:...
    >>> In EVERY postback I do?? This page has a lot of postback's on it...
    >>> that doesn't seem very efficient.
    >>>
    >>> --
    >>> Adam Clauss
    >>>
    >>> "S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
    >>> message news:eDE2eK$...
    >>>> Yes,
    >>>>
    >>>> You'll have to set it yourself.
    >>>>
    >>>> --
    >>>> Sincerely,
    >>>>
    >>>> S. Justin Gengo, MCP
    >>>> Web Developer / Programmer
    >>>>
    >>>> www.aboutfortunate.com
    >>>>
    >>>> "Out of chaos comes order."
    >>>> Nietzsche
    >>>> "Adam Clauss" <> wrote in message
    >>>> news:...
    >>>>> Huh? Yes, I'm aware of that property.
    >>>>> The problem is I'm not setting the selection - ASP.NET is
    >>>>> automatically.
    >>>>>
    >>>>> --
    >>>>> Adam Clauss
    >>>>>
    >>>>> "S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote
    >>>>> in message news:%23v3w59%...
    >>>>>> Adam,
    >>>>>>
    >>>>>> Yes the Listbox object has a SelectedIndex property, it gets or sets
    >>>>>> the SelectedIndex.
    >>>>>>
    >>>>>> --
    >>>>>> Sincerely,
    >>>>>>
    >>>>>> S. Justin Gengo, MCP
    >>>>>> Web Developer / Programmer
    >>>>>>
    >>>>>> www.aboutfortunate.com
    >>>>>>
    >>>>>> "Out of chaos comes order."
    >>>>>> Nietzsche
    >>>>>> "Adam Clauss" <> wrote in message
    >>>>>> news:...
    >>>>>>>I have a page containing a list box. This list may contain duplicate
    >>>>>>>items - in which the ORDER is important.
    >>>>>>> ex:
    >>>>>>> a
    >>>>>>> b
    >>>>>>> b
    >>>>>>> a
    >>>>>>>
    >>>>>>> is significant as compared to:
    >>>>>>> b
    >>>>>>> a
    >>>>>>> a
    >>>>>>> b
    >>>>>>>
    >>>>>>> even though the list contains the same elements. The user has
    >>>>>>> buttons on the page to reorder the list.
    >>>>>>>
    >>>>>>> The problem is that, say in the first example, the bottom 'a' is
    >>>>>>> selected. Then a button is pressed that causes a postback. The
    >>>>>>> re-selected item is now the FIRST a. Likewise, if the second 'b' is
    >>>>>>> selected, on postback the FIRST b will be selected.
    >>>>>>>
    >>>>>>> The following page demonstrates this:
    >>>>>>>
    >>>>>>> <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
    >>>>>>> AutoEventWireup="false" Inherits="testListbox.WebForm1"
    >>>>>>> smartNavigation="True"%>
    >>>>>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    >>>>>>> <HTML>
    >>>>>>> <HEAD>
    >>>>>>> <title>WebForm1</title>
    >>>>>>> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    >>>>>>> <meta name="CODE_LANGUAGE" Content="C#">
    >>>>>>> <meta name="vs_defaultClientScript" content="JavaScript">
    >>>>>>> <meta name="vs_targetSchema"
    >>>>>>> content="http://schemas.microsoft.com/intellisense/ie5">
    >>>>>>> </HEAD>
    >>>>>>> <body>
    >>>>>>> <form id="Form1" method="post" runat="server">
    >>>>>>> <asp:Button id="Button1" runat="server"
    >>>>>>> Text="Button"></asp:Button>
    >>>>>>> <asp:ListBox id="ListBox1" runat="server">
    >>>>>>> <asp:ListItem Value="item1">item1</asp:ListItem>
    >>>>>>> <asp:ListItem Value="item2">item2</asp:ListItem>
    >>>>>>> <asp:ListItem Value="item1">item1</asp:ListItem>
    >>>>>>> </asp:ListBox>
    >>>>>>> </form>
    >>>>>>> </body>
    >>>>>>> </HTML>
    >>>>>>>
    >>>>>>>
    >>>>>>> There was no code added to the codebehind page. (I just created a
    >>>>>>> simple web app to demonstrate this).
    >>>>>>>
    >>>>>>> Is there anyway to resolve this issue such that the correct element
    >>>>>>> is selected (by index rather than value)?
    >>>>>>> --
    >>>>>>> Adam Clauss
    >>>>>>>
    >>>>>>>
    >>>>>>>
    >>>>>>
    >>>>>>
    >>>>>
    >>>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    Adam Clauss, Sep 8, 2005
    #10
  11. Adam,

    Ok, now I understand. What's happening is that when you post back .NET is
    setting the list box based on the value selected.

    Think about how you would write the code to find which item was selected...

    1) You receive a form value sent in a post
    2) You know the post came from a certain list box
    3) You need to make certain the same value is selected when the web page is
    returned to the client.
    4) The list box has a method: SelectedItem which takes a string.
    5) Assume that SelectedItem iterates through each value in the list box
    until it finds the one that matches the string posted.

    Well, because you have the same exact text and values in your list box the
    first one found in the iteration matches and the loop is exited.

    How to correct this? Make certain that your list box has different values.

    For example, if you were adding these values to the list box by hand you
    could do this:
    ListBox1.Items.Add(New ListItem("a", "1"))
    ListBox1.Items.Add(New ListItem("b", "2"))
    ListBox1.Items.Add(New ListItem("b", "3"))
    ListBox1.Items.Add(New ListItem("a", "4"))

    If you use the code above you'll see that picking the bottom "a" and then
    posting back will keep the bottom "a" selected because we've given the code
    a way to discriminate between items.


    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "Adam Clauss" <> wrote in message
    news:...
    > OK - maybe partially my fault for giving a bad example in my first post.
    > Damien hit the problem.
    >
    > What the first example should have said was that:
    > a
    > b
    > b
    > a **selected
    >
    > is significant as to:
    > a **selected
    > b
    > b
    > a
    >
    > Order itself is important too, but that wasn't the actual problem.
    > Apologies for being unclear and making things confusing :)
    >
    > --
    > Adam Clauss
    >
    > "Adam Clauss" <> wrote in message
    > news:...
    >> Again - try running the code/page I gave on the first post.
    >> I do not have to change or reorder anything to cause this problem.
    >>
    >> Problem in short - have a page with a button (with no event actually
    >> bound to it), and a listbox with contents as:
    >> a
    >> b
    >> b
    >> a
    >>
    >> Select the bottom a. Then press the button.
    >> The first a is what shows up as selected when the page reappears.
    >>
    >> Select the bottom b. Then press the button.
    >> The first b is what shows up as selected when the page reappears.
    >>
    >> --
    >> Adam Clauss
    >>
    >> "S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
    >> message news:u1VgFo$...
    >>> Adam,
    >>>
    >>> Maybe I'm misunderstanding what you're doing. Does the item order in the
    >>> list box change with every post back. If the list box has viewstate
    >>> enabled on it it should stay exactly the same unless you re-bind it or
    >>> the user changes the order. Every time a post-back is done where the
    >>> user changes the order you'll have to set the proper index if .Net can't
    >>> do it itself. But that certainly shouldn't be on every post back.
    >>>
    >>> --
    >>> Sincerely,
    >>>
    >>> S. Justin Gengo, MCP
    >>> Web Developer / Programmer
    >>>
    >>> www.aboutfortunate.com
    >>>
    >>> "Out of chaos comes order."
    >>> Nietzsche
    >>> "Adam Clauss" <> wrote in message
    >>> news:...
    >>>> In EVERY postback I do?? This page has a lot of postback's on it...
    >>>> that doesn't seem very efficient.
    >>>>
    >>>> --
    >>>> Adam Clauss
    >>>>
    >>>> "S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
    >>>> message news:eDE2eK$...
    >>>>> Yes,
    >>>>>
    >>>>> You'll have to set it yourself.
    >>>>>
    >>>>> --
    >>>>> Sincerely,
    >>>>>
    >>>>> S. Justin Gengo, MCP
    >>>>> Web Developer / Programmer
    >>>>>
    >>>>> www.aboutfortunate.com
    >>>>>
    >>>>> "Out of chaos comes order."
    >>>>> Nietzsche
    >>>>> "Adam Clauss" <> wrote in message
    >>>>> news:...
    >>>>>> Huh? Yes, I'm aware of that property.
    >>>>>> The problem is I'm not setting the selection - ASP.NET is
    >>>>>> automatically.
    >>>>>>
    >>>>>> --
    >>>>>> Adam Clauss
    >>>>>>
    >>>>>> "S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote
    >>>>>> in message news:%23v3w59%...
    >>>>>>> Adam,
    >>>>>>>
    >>>>>>> Yes the Listbox object has a SelectedIndex property, it gets or sets
    >>>>>>> the SelectedIndex.
    >>>>>>>
    >>>>>>> --
    >>>>>>> Sincerely,
    >>>>>>>
    >>>>>>> S. Justin Gengo, MCP
    >>>>>>> Web Developer / Programmer
    >>>>>>>
    >>>>>>> www.aboutfortunate.com
    >>>>>>>
    >>>>>>> "Out of chaos comes order."
    >>>>>>> Nietzsche
    >>>>>>> "Adam Clauss" <> wrote in message
    >>>>>>> news:...
    >>>>>>>>I have a page containing a list box. This list may contain
    >>>>>>>>duplicate items - in which the ORDER is important.
    >>>>>>>> ex:
    >>>>>>>> a
    >>>>>>>> b
    >>>>>>>> b
    >>>>>>>> a
    >>>>>>>>
    >>>>>>>> is significant as compared to:
    >>>>>>>> b
    >>>>>>>> a
    >>>>>>>> a
    >>>>>>>> b
    >>>>>>>>
    >>>>>>>> even though the list contains the same elements. The user has
    >>>>>>>> buttons on the page to reorder the list.
    >>>>>>>>
    >>>>>>>> The problem is that, say in the first example, the bottom 'a' is
    >>>>>>>> selected. Then a button is pressed that causes a postback. The
    >>>>>>>> re-selected item is now the FIRST a. Likewise, if the second 'b'
    >>>>>>>> is selected, on postback the FIRST b will be selected.
    >>>>>>>>
    >>>>>>>> The following page demonstrates this:
    >>>>>>>>
    >>>>>>>> <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
    >>>>>>>> AutoEventWireup="false" Inherits="testListbox.WebForm1"
    >>>>>>>> smartNavigation="True"%>
    >>>>>>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    >>>>>>>> <HTML>
    >>>>>>>> <HEAD>
    >>>>>>>> <title>WebForm1</title>
    >>>>>>>> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    >>>>>>>> <meta name="CODE_LANGUAGE" Content="C#">
    >>>>>>>> <meta name="vs_defaultClientScript" content="JavaScript">
    >>>>>>>> <meta name="vs_targetSchema"
    >>>>>>>> content="http://schemas.microsoft.com/intellisense/ie5">
    >>>>>>>> </HEAD>
    >>>>>>>> <body>
    >>>>>>>> <form id="Form1" method="post" runat="server">
    >>>>>>>> <asp:Button id="Button1" runat="server"
    >>>>>>>> Text="Button"></asp:Button>
    >>>>>>>> <asp:ListBox id="ListBox1" runat="server">
    >>>>>>>> <asp:ListItem Value="item1">item1</asp:ListItem>
    >>>>>>>> <asp:ListItem Value="item2">item2</asp:ListItem>
    >>>>>>>> <asp:ListItem Value="item1">item1</asp:ListItem>
    >>>>>>>> </asp:ListBox>
    >>>>>>>> </form>
    >>>>>>>> </body>
    >>>>>>>> </HTML>
    >>>>>>>>
    >>>>>>>>
    >>>>>>>> There was no code added to the codebehind page. (I just created a
    >>>>>>>> simple web app to demonstrate this).
    >>>>>>>>
    >>>>>>>> Is there anyway to resolve this issue such that the correct element
    >>>>>>>> is selected (by index rather than value)?
    >>>>>>>> --
    >>>>>>>> Adam Clauss
    >>>>>>>>
    >>>>>>>>
    >>>>>>>>
    >>>>>>>
    >>>>>>>
    >>>>>>
    >>>>>>
    >>>>>
    >>>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    S. Justin Gengo, Sep 8, 2005
    #11
    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. vilas h via .NET 247
    Replies:
    1
    Views:
    536
    John Saunders
    Jun 5, 2004
  2. Chris Kettenbach

    Listbox selection to populate another listbox?

    Chris Kettenbach, Jun 15, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    1,654
    Chris Kettenbach
    Jun 16, 2005
  3. divya
    Replies:
    1
    Views:
    1,093
    Munna
    May 28, 2008
  4. Santel

    Selected state not maintained on postback

    Santel, Jun 12, 2007, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    909
    Teemu Keiski
    Jun 13, 2007
  5. Ramakrishna

    listbox control removing duplicate items

    Ramakrishna, Nov 4, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    160
    Ramakrishna
    Nov 4, 2003
Loading...

Share This Page