包含重复项目的列表框 - 回发时未维护正确的选择? [英] Listbox containing duplicate items - correct selection not maintained on postback?

查看:60
本文介绍了包含重复项目的列表框 - 回发时未维护正确的选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含列表框的页面。此列表可能包含重复的

项目 - 其中ORDER很重要。

ex:

a

b

b

a

与以下相比非常重要:

b

a

a

b


即使列表包含相同的元素。用户在页面上有按钮来重新排序列表。


问题是,在第一个例子中,底部是'a''选择。

然后按下一个按钮,导致回发。重新选择的项目是

现在是第一个。同样,如果选择了第二个b,则在回发时将选择

FIRST b。


以下页面演示了这一点:


<%@ Page language =" c#"代码隐藏= QUOT; 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"方法= QUOT;交" runat =" server">

< asp:Button id =" Button1" RUNAT = QUOT;服务器" 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>

代码隐藏页面中没有添加任何代码。 (我刚刚创建了一个简单的

网络应用程序来演示这个)。


无论如何都要解决这个问题,以便正确的元素是
选择
(按指数而非价值)?

-

Adam Clauss

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,


是的,Listbox对象有一个SelectedIndex属性,它获取或设置

SelectedIndex。


-

此致,


S. Justin Gengo,MCP

Web开发人员/程序员

www.aboutfortunate.com

混乱就是订单。

Nietzsche

" Adam Clauss" < CA ***** @ tamu.edu>在消息中写道

news:11 ************* @ corp.supernews.com ...
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...
我有一个页面包含列表框。此列表可能包含重复的
项目 - 其中ORDER很重要。






与以下相比具有重要意义:即使列表包含相同的元素,也很重要。



用户在页面上有按钮以重新排序列表。

问题在于,在第一个示例中,选择了底部的a。
然后按下一个按钮,导致回发。重新选择的项目现在是第一个。同样,如果选择了第二个b,则在回发时将选择
第一个b。

以下页面演示了这一点:

< %@ 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"方法= QUOT;交" runat =" server">
< asp:Button id =" Button1" RUNAT = QUOT;服务器" 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>

代码隐藏页面中没有添加任何代码。 (我刚刚创建了一个简单的
网络应用程序来演示这个)。

无论如何都要解决这个问题,以便选择正确的元素(通过索引而不是值) ?
-
Adam Clauss
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



嗯?是的,我知道那个属性。

问题是我没有设置选择 - ASP.NET是自动的。


- -

Adam Clauss


" S. Justin Gengo < sjgengo @ [no_spam_please] aboutfortunate.com>写在

消息新闻:%2 ****************** @ tk2msftngp13.phx.gbl ...
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,

是的Listbox对象有一个SelectedIndex属性,它获取或设置
SelectedIndex。

-
真诚的,

S. Justin Gengo,MCP
Web开发人员/程序员

www.aboutfortunate.com

出于混乱而来的顺序。
Nietzsche
Adam Clauss < CA ***** @ tamu.edu>在消息中写道
新闻:11 ************* @ corp.supernews.com ...
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...
我有一个包含列表框的页面。此列表可能包含重复的
项目 - 其中ORDER很重要。






与以下相比具有重要意义:即使列表包含相同的元素,也很重要。



用户在页面上有按钮以重新排序列表。

问题在于,在第一个示例中,选择了底部的a。然后按下一个按钮,导致回发。
重新选择的项目现在是第一个。同样,如果选择第二个''b'',则在回发时将选择FIRST b。

以下页面演示了这一点:

< %@ 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"方法= QUOT;交" runat =" server">
< asp:Button id =" Button1" RUNAT = QUOT;服务器" 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>

代码隐藏页面中没有添加任何代码。 (我刚刚创建了一个简单的
网络应用程序来演示这个)。

无论如何都要解决这个问题,以便选择正确的元素(通过索引而不是值) ?
-
Adam Clauss
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,MCP

网站开发人员/程序员

www.aboutfortunate.com


混乱无序。

Nietzsche

" Adam Clauss" < CA ***** @ tamu.edu>在消息中写道

新闻:11 ************* @ corp.supernews.com ...
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...
嗯?是的,我知道那个属性。
问题是我没有设置选择 - ASP.NET是自动的。

-
Adam Clauss

S。 Justin Gengo < sjgengo @ [no_spam_please] aboutfortunate.com>在
消息新闻中写道:%2 ****************** @ tk2msftngp13.phx.gbl ...
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,

是的,Listbox对象有一个SelectedIndex属性,它获取或设置
SelectedIndex。

-
真诚的,

S. Justin Gengo,MCP
网站开发人员/程序员

www。 aboutfortunate.com

出于混乱而来的秩序。Nietzsche
Adam Clauss < CA ***** @ tamu.edu>在消息中写道
新闻:11 ************* @ corp.supernews.com ...
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...
我有一个包含列表框的页面。此列表可能包含重复的
项目 - 其中ORDER很重要。






与以下相比具有重要意义:即使列表包含相同的元素,也很重要。



用户在页面上有按钮
以重新排序列表。

问题在于,在第一个示例中,选择了底部的a。然后按下一个按钮,导致回发。
重新选择的项目现在是第一个。同样,如果选择第二个''b'',则在回发时将选择FIRST b。

以下页面演示了这一点:

< %@ 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"方法= QUOT;交" runat =" server">
< asp:Button id =" Button1" RUNAT = QUOT;服务器" 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>

代码隐藏页面中没有添加任何代码。 (我刚刚创建了一个简单的Web应用程序来演示这个)。

无论如何都要解决这个问题,以便选择正确的元素(通过索引而不是值) ?
-
Adam Clauss
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





这篇关于包含重复项目的列表框 - 回发时未维护正确的选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆