473,473 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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

Nov 19 '05 #1
10 3261
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" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
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

Nov 19 '05 #2
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:%2******************@tk2msftngp13.phx.gbl...
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" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
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


Nov 19 '05 #3
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" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
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:%2******************@tk2msftngp13.phx.gbl...
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" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
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



Nov 19 '05 #4
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:eD**************@TK2MSFTNGP09.phx.gbl...
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" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
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:%2******************@tk2msftngp13.phx.gbl...
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" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
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




Nov 19 '05 #5
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" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
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:eD**************@TK2MSFTNGP09.phx.gbl...
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" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
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:%2******************@tk2msftngp13.phx.gbl...
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" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
>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
>
>
>



Nov 19 '05 #6
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:u1**************@TK2MSFTNGP09.phx.gbl...
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" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
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:eD**************@TK2MSFTNGP09.phx.gbl...
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" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
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:%2******************@tk2msftngp13.phx.gbl...
> 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" <ca*****@tamu.edu> wrote in message
> news:11*************@corp.supernews.com...
>>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
>>
>>
>>
>
>



Nov 19 '05 #7
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

Nov 19 '05 #8
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" <Da*******************@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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

Nov 19 '05 #9
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" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
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:u1**************@TK2MSFTNGP09.phx.gbl...
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" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
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:eD**************@TK2MSFTNGP09.phx.gbl...
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" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
> 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:%2******************@tk2msftngp13.phx.gbl...
>> 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" <ca*****@tamu.edu> wrote in message
>> news:11*************@corp.supernews.com...
>>>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
>>>
>>>
>>>
>>
>>
>
>



Nov 19 '05 #10
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" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
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" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
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:u1**************@TK2MSFTNGP09.phx.gbl...
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" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
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:eD**************@TK2MSFTNGP09.phx.gbl...
> 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" <ca*****@tamu.edu> wrote in message
> news:11*************@corp.supernews.com...
>> 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:%2******************@tk2msftngp13.phx.gbl...
>>> 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" <ca*****@tamu.edu> wrote in message
>>> news:11*************@corp.supernews.com...
>>>>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
>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>



Nov 19 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Irene | last post by:
I'm developing a Web application where a user selects from a listbox which can have many items. The initial display only shows about 10 items. After a postback, the listbox is automatically...
2
by: collie | last post by:
Hi, I have 2 listboxes. The first gets populated from the db as soon as the page loads. The second listbox get populated based on the user's selection from the first listbox. However,...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
4
by: Ron | last post by:
I've got a listbox that holds a list of groups. Users can select a group, hit the remove button and the group should be removed from the listbox. The only problem is that no matter which group you...
7
by: Ohad Asor | last post by:
Hello all, I have an ASP.NET page I've written using VS.NET2003, which have a ListBox in it. When I press a button in the form, I try to get the selected item in the list by calling...
5
by: Dave H | last post by:
I have an asp:listbox, allowing multiple selections, is there a quick check to see if there's more than one selected, or do I need to go through the whole list? Thanks, Dave
3
by: Lenard Gunda | last post by:
Hi, I have the following problem. On a page I have a multiple selection listbox. It contains a lot of items. When items near the bottom are selected, and I do a postback, the control jumpst to...
0
by: shotta | last post by:
I have two list boxes where a user can make a selection click a add button within a wizard control and it add it to another listbox in the same wizard. My problem is the add goes fine the second...
9
by: zdrakec | last post by:
Hello all: Clearly, I'm not getting it! Here is the scenario: On a web page, I have two list boxen and a text box. The first listbox is populated at page load time (if it is not a postback)....
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.