473,793 Members | 2,865 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with multiple values of Listbox

Hi,

I want to get all the selected values of a listbox in several labels..
With this code here below, when i click on e.g. "option 2", i get '2' in
label2. That's ok.
But if i then click on "option 3", "option 3" is selected and "option 2" is
deselected. So i get '3' in label3 but also still '2' in label2. Same with
"option 1".
And when i deselect everything, i still get '2' and '3'. I can't get rid of
them.

When selecting "option 3" and so deselecting "option2", this line should be
False:
" If ListBox1.Items( 1).Selected = True" =should not execute "Label2.Tex t =
ListBox1.Items( 1).Value".

So why does that happen?
How to get only the selected values?

Thanks for help
Bob
aspx file:
<div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack=tr ue
SelectionMode=M ultiple>
<asp:ListItem Value=1 Text="option 1"></asp:ListItem>
<asp:ListItem Value=2 Text="option 2"></asp:ListItem>
<asp:ListItem Value=3 Text="option 3"></asp:ListItem>
</asp:ListBox>
<asp:Label ID="Label1" runat="server"> </asp:Label>
<asp:Label ID="Label2" runat="server"> </asp:Label>
<asp:Label ID="Label3" runat="server"> </asp:Label>

code-behind:
Protected Sub ListBox1_Select edIndexChanged( ByVal sender As Object, ByVal e
As System.EventArg s) Handles

ListBox1.Select edIndexChanged
If ListBox1.Items( 0).Selected = True Then Label1.Text =
ListBox1.Items( 0).Value
If ListBox1.Items( 1).Selected = True Then Label2.Text =
ListBox1.Items( 1).Value
If ListBox1.Items( 2).Selected = True Then Label3.Text =
ListBox1.Items( 2).Value
End Sub
May 24 '07 #1
9 2193
On May 24, 8:00 pm, "Bob Malcoprs" <b...@nm.dfwrot e:
Hi,

I want to get all the selected values of a listbox in several labels..
With this code here below, when i click on e.g. "option 2", i get '2' in
label2. That's ok.
But if i then click on "option 3", "option 3" is selected and "option 2" is
deselected. So i get '3' in label3 but also still '2' in label2. Same with
"option 1".
And when i deselect everything, i still get '2' and '3'. I can't get rid of
them.

When selecting "option 3" and so deselecting "option2", this line should be
False:
" If ListBox1.Items( 1).Selected = True" =should not execute "Label2.Tex t =
ListBox1.Items( 1).Value".

So why does that happen?
How to get only the selected values?

Thanks for help
Bob

aspx file:
<div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack=tr ue
SelectionMode=M ultiple>
<asp:ListItem Value=1 Text="option 1"></asp:ListItem>
<asp:ListItem Value=2 Text="option 2"></asp:ListItem>
<asp:ListItem Value=3 Text="option 3"></asp:ListItem>
</asp:ListBox>
<asp:Label ID="Label1" runat="server"> </asp:Label>
<asp:Label ID="Label2" runat="server"> </asp:Label>
<asp:Label ID="Label3" runat="server"> </asp:Label>

code-behind:
Protected Sub ListBox1_Select edIndexChanged( ByVal sender As Object, ByVal e
As System.EventArg s) Handles

ListBox1.Select edIndexChanged
If ListBox1.Items( 0).Selected = True Then Label1.Text =
ListBox1.Items( 0).Value
If ListBox1.Items( 1).Selected = True Then Label2.Text =
ListBox1.Items( 1).Value
If ListBox1.Items( 2).Selected = True Then Label3.Text =
ListBox1.Items( 2).Value
End Sub
Labels keep the value after postback (AutoPostBack=t rue)

To solve your problem set an initial state for the labels

Label1.Text = ""
Label2.Text = ""
Label3.Text = ""

May 24 '07 #2
Yes, that's it. Thanks

"Alexey Smirnov" <al************ @gmail.comschre ef in bericht
news:11******** **************@ g4g2000hsf.goog legroups.com...
On May 24, 8:00 pm, "Bob Malcoprs" <b...@nm.dfwrot e:
>Hi,

I want to get all the selected values of a listbox in several labels..
With this code here below, when i click on e.g. "option 2", i get '2' in
label2. That's ok.
But if i then click on "option 3", "option 3" is selected and "option 2"
is
deselected. So i get '3' in label3 but also still '2' in label2. Same
with
"option 1".
And when i deselect everything, i still get '2' and '3'. I can't get rid
of
them.

When selecting "option 3" and so deselecting "option2", this line should
be
False:
" If ListBox1.Items( 1).Selected = True" =should not execute
"Label2.Tex t =
ListBox1.Items (1).Value".

So why does that happen?
How to get only the selected values?

Thanks for help
Bob

aspx file:
<div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack=tr ue
SelectionMode= Multiple>
<asp:ListIte m Value=1 Text="option 1"></asp:ListItem>
<asp:ListIte m Value=2 Text="option 2"></asp:ListItem>
<asp:ListIte m Value=3 Text="option 3"></asp:ListItem>
</asp:ListBox>
<asp:Label ID="Label1" runat="server"> </asp:Label>
<asp:Label ID="Label2" runat="server"> </asp:Label>
<asp:Label ID="Label3" runat="server"> </asp:Label>

code-behind:
Protected Sub ListBox1_Select edIndexChanged( ByVal sender As Object,
ByVal e
As System.EventArg s) Handles

ListBox1.Selec tedIndexChanged
If ListBox1.Items( 0).Selected = True Then Label1.Text =
ListBox1.Items (0).Value
If ListBox1.Items( 1).Selected = True Then Label2.Text =
ListBox1.Items (1).Value
If ListBox1.Items( 2).Selected = True Then Label3.Text =
ListBox1.Items (2).Value
End Sub

Labels keep the value after postback (AutoPostBack=t rue)

To solve your problem set an initial state for the labels

Label1.Text = ""
Label2.Text = ""
Label3.Text = ""

May 24 '07 #3
Alexey,

I also tried with textbox instead of labels with the same problem., But so
far i know, the texbox doesn't keep tha value after postback, or am i wrong?

"Alexey Smirnov" <al************ @gmail.comschre ef in bericht
news:11******** **************@ g4g2000hsf.goog legroups.com...
On May 24, 8:00 pm, "Bob Malcoprs" <b...@nm.dfwrot e:
>Hi,

I want to get all the selected values of a listbox in several labels..
With this code here below, when i click on e.g. "option 2", i get '2' in
label2. That's ok.
But if i then click on "option 3", "option 3" is selected and "option 2"
is
deselected. So i get '3' in label3 but also still '2' in label2. Same
with
"option 1".
And when i deselect everything, i still get '2' and '3'. I can't get rid
of
them.

When selecting "option 3" and so deselecting "option2", this line should
be
False:
" If ListBox1.Items( 1).Selected = True" =should not execute
"Label2.Tex t =
ListBox1.Items (1).Value".

So why does that happen?
How to get only the selected values?

Thanks for help
Bob

aspx file:
<div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack=tr ue
SelectionMode= Multiple>
<asp:ListIte m Value=1 Text="option 1"></asp:ListItem>
<asp:ListIte m Value=2 Text="option 2"></asp:ListItem>
<asp:ListIte m Value=3 Text="option 3"></asp:ListItem>
</asp:ListBox>
<asp:Label ID="Label1" runat="server"> </asp:Label>
<asp:Label ID="Label2" runat="server"> </asp:Label>
<asp:Label ID="Label3" runat="server"> </asp:Label>

code-behind:
Protected Sub ListBox1_Select edIndexChanged( ByVal sender As Object,
ByVal e
As System.EventArg s) Handles

ListBox1.Selec tedIndexChanged
If ListBox1.Items( 0).Selected = True Then Label1.Text =
ListBox1.Items (0).Value
If ListBox1.Items( 1).Selected = True Then Label2.Text =
ListBox1.Items (1).Value
If ListBox1.Items( 2).Selected = True Then Label3.Text =
ListBox1.Items (2).Value
End Sub

Labels keep the value after postback (AutoPostBack=t rue)

To solve your problem set an initial state for the labels

Label1.Text = ""
Label2.Text = ""
Label3.Text = ""

May 24 '07 #4
On May 24, 9:44 pm, "Bob Malcoprs" <ssd@qs;qqswrot e:
Alexey,

I also tried with textbox instead of labels with the same problem., But so
far i know, the texbox doesn't keep tha value after postback, or am i wrong?
By default, ASP.NET maintains the ViewState for all server controls.
Each control has the EnableViewState property, which is "true" by
default. When EnableViewState = true and a form is submitted, ASP.NET
keeps the value. This is the reason why you get your values back.

Note, ViewState can be disabled

<%@ Page EnableViewState ="false" %>
<asp:TextBox EnableViewState ="false" ...

May 24 '07 #5
May i conclude this: when clikcing on 'submit':
the label value is not postbacked (because not an element of a form), so any
change in its value is not kept
the texbox value is postbacked but viewstate (which is by default true)
keeps the old value
"Alexey Smirnov" <al************ @gmail.comschre ef in bericht
news:11******** **************@ h2g2000hsg.goog legroups.com...
On May 24, 9:44 pm, "Bob Malcoprs" <ssd@qs;qqswrot e:
>Alexey,

I also tried with textbox instead of labels with the same problem., But
so
far i know, the texbox doesn't keep tha value after postback, or am i
wrong?

By default, ASP.NET maintains the ViewState for all server controls.
Each control has the EnableViewState property, which is "true" by
default. When EnableViewState = true and a form is submitted, ASP.NET
keeps the value. This is the reason why you get your values back.

Note, ViewState can be disabled

<%@ Page EnableViewState ="false" %>
<asp:TextBox EnableViewState ="false" ...

May 24 '07 #6
On May 25, 12:29 am, "Bob Malcoprs" <s...@qscq.qqcw rote:
May i conclude this: when clikcing on 'submit':
the label value is not postbacked (because not an element of a form), so any
change in its value is not kept
If I look at your first post, it seems that the changes in the labels
are kept :-)

the texbox value is postbacked but viewstate (which is by default true)
keeps the old value
Label is not an element of a form, it's a server control. When
EnableViewState is true, ASP.net keeps its value as well as for other
controls, such as a TextBox.

I've made a simple example for you. When you run it, you will see how
it is working.

Save this code into TestViewState.a spx

<%@ Page Language="vb" %>
<script runat=server>
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s)
Handles Me.Load
If Not Page.IsPostBack Then
Label1.Text = DateTime.Now
End If
End Sub
</script>
<html>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"
EnableViewState ="true"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="PostBack" /></
div>
</form>
</body>
</html>

Note, the Label has EnableViewState ="true". When you call the page for
the first time, it will output the current time string. Once you
clicked on the PostBack button, nothing going to be changed, because
of the ViewState. It keeps the value.

It keeps the value in the hidden field named __VIEWSTATE (look at the
source code of the page - right click, View Source)

Now, change the value of the EnableViewState to false and run the
sample again.

Don't hesitate to ask if I was not clear.

May 25 '07 #7
Thanks you for your good explanation. I tried your example.

so, if i understand good, viewstate keeps the actual value of a server
control (label, textbox, dropdownlist ...) at the moment of clicking on a
button (postback).
Unless it's changed by code or manually in case of a textbox, that value
remains unchanged.

I tried your example with textbox and even when viewstate is false, the
first datetime is shown (while it's not true with the label of your
example).
Is the reason that the value of a textbox (form element) is postback while
the value of a label is not?
Is here no kind of 'contradiction' between postback (sending and reusing
actual value of textbox) and viewstate=false (not retaining actual value)?

Thanks for your time.

"Alexey Smirnov" <al************ @gmail.comschre ef in bericht
news:11******** *************@p 47g2000hsd.goog legroups.com...
On May 25, 12:29 am, "Bob Malcoprs" <s...@qscq.qqcw rote:
>May i conclude this: when clikcing on 'submit':
the label value is not postbacked (because not an element of a form), so
any
change in its value is not kept

If I look at your first post, it seems that the changes in the labels
are kept :-)

>the texbox value is postbacked but viewstate (which is by default true)
keeps the old value

Label is not an element of a form, it's a server control. When
EnableViewState is true, ASP.net keeps its value as well as for other
controls, such as a TextBox.

I've made a simple example for you. When you run it, you will see how
it is working.

Save this code into TestViewState.a spx

<%@ Page Language="vb" %>
<script runat=server>
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s)
Handles Me.Load
If Not Page.IsPostBack Then
Label1.Text = DateTime.Now
End If
End Sub
</script>
<html>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"
EnableViewState ="true"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="PostBack" /></
div>
</form>
</body>
</html>

Note, the Label has EnableViewState ="true". When you call the page for
the first time, it will output the current time string. Once you
clicked on the PostBack button, nothing going to be changed, because
of the ViewState. It keeps the value.

It keeps the value in the hidden field named __VIEWSTATE (look at the
source code of the page - right click, View Source)

Now, change the value of the EnableViewState to false and run the
sample again.

Don't hesitate to ask if I was not clear.

May 25 '07 #8
On May 25, 11:26 am, "Bob Malcoprs" <xwrote:
Thanks you for your good explanation. I tried your example.

so, if i understand good, viewstate keeps the actual value of a server
control (label, textbox, dropdownlist ...) at the moment of clicking on a
button (postback).
Unless it's changed by code or manually in case of a textbox, that value
remains unchanged.

I tried your example with textbox and even when viewstate is false, the
first datetime is shown (while it's not true with the label of your
example).
Is the reason that the value of a textbox (form element) is postback while
the value of a label is not?
Is here no kind of 'contradiction' between postback (sending and reusing
actual value of textbox) and viewstate=false (not retaining actual value)?
This is little bit tricky thing. The TextBox keeps its value because
this is a control that implements
System.Web.UI.I PostBackDataHan dler.LoadPostDa ta (MSDN: "Loads the
posted text box content if it is different from the last posting.")

In simple words, it maintains the state using the HTTP POST Form
Collection and another trick - a ControlState (new in ASP.NET 2.0)

The ControlState is not a ViewState, and it's not affected when the
view state is disabled at the Page level using EnableViewState .

This is the same for all other controls that implement the
IPostBackDataHa ndler interface, they keep a values using HTTP POST.

For example, a TextBox Control renders a HTML form element. When you
submitted the form, it is posted to the HTTP request, using the POST
method. ASP.NET set all fields (like TextBox=input,
DropDownList=se lect, etc) on an HTML form that are available from an
HTTP POST using the Form collection.

Add a trace to my example, and you will see the Form Collection

<%@ Page Language="vb" Trace="true" %>

Submit the page. The Label will not have a value (no form field), but
the value of TextBox will be inside the Form Collection.

Hope it helps

May 25 '07 #9
Thanks

"Alexey Smirnov" <al************ @gmail.comschre ef in bericht
news:11******** **************@ k79g2000hse.goo glegroups.com.. .
On May 25, 11:26 am, "Bob Malcoprs" <xwrote:
>Thanks you for your good explanation. I tried your example.

so, if i understand good, viewstate keeps the actual value of a server
control (label, textbox, dropdownlist ...) at the moment of clicking on a
button (postback).
Unless it's changed by code or manually in case of a textbox, that value
remains unchanged.

I tried your example with textbox and even when viewstate is false, the
first datetime is shown (while it's not true with the label of your
example).
Is the reason that the value of a textbox (form element) is postback
while
the value of a label is not?
Is here no kind of 'contradiction' between postback (sending and reusing
actual value of textbox) and viewstate=false (not retaining actual
value)?

This is little bit tricky thing. The TextBox keeps its value because
this is a control that implements
System.Web.UI.I PostBackDataHan dler.LoadPostDa ta (MSDN: "Loads the
posted text box content if it is different from the last posting.")

In simple words, it maintains the state using the HTTP POST Form
Collection and another trick - a ControlState (new in ASP.NET 2.0)

The ControlState is not a ViewState, and it's not affected when the
view state is disabled at the Page level using EnableViewState .

This is the same for all other controls that implement the
IPostBackDataHa ndler interface, they keep a values using HTTP POST.

For example, a TextBox Control renders a HTML form element. When you
submitted the form, it is posted to the HTTP request, using the POST
method. ASP.NET set all fields (like TextBox=input,
DropDownList=se lect, etc) on an HTML form that are available from an
HTTP POST using the Form collection.

Add a trace to my example, and you will see the Form Collection

<%@ Page Language="vb" Trace="true" %>

Submit the page. The Label will not have a value (no form field), but
the value of TextBox will be inside the Form Collection.

Hope it helps

May 25 '07 #10

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

Similar topics

2
6601
by: Jen F. | last post by:
I have inherited a medical database in which there are multiple values stored in a single field (ie. "Current Conditions" field might contain 1-20 different conditions, separated by comma (ie. "Heart Disease,Hyper Tyroid,Cancer" etc. I would like to search via combo box for any one or more than one value in this field, ie-what patients have Heart Disease or Cancer by selecting these disorders off the combo box list. Any help would be...
1
469
by: NYprmr | last post by:
Hi All, I'm trying to get value from Listbox where Multi Select property is set to Simple, meaning multiple values can be selected. Doing regular loop like following, retuns NULL value for each data item. ========================================== lCount = Me!lstProblemType.ListCount For varItem = 0 To lCount If Me!lstProblemType.Selected(varItem) = True Then strDescr = strDescr & "," &
2
2735
by: DC Gringo | last post by:
I have two listboxes, the first of which is an autopostback=true that allows multiple row selection. When I select multiple values (by holding down CTL) in the first one, it should query the second one. I seem unable to do this as it only sends back the first item I select whether I have the CTL key down or not. Upon the return trip, I can select another one, but it still only sends value for the last one I selected. -- MY LISTBOX --...
1
2249
by: sab | last post by:
Hi, We have a web form with a listbox. The listbox is a multiple select listbox and has data that looks something like: ALL Unit 1 Unit 2 Unit 3 Note: "ALL" is always the first item in the list.
8
4616
by: Chris A via AccessMonster.com | last post by:
I have an interesting problem that I have yet to come accross that I can't change data structure on because it is an export from filemaker I am reformatting for another dept. anyway. I have a table like so... Table 1 Field1 Field2 Field3 E1 April 2006 AA, BB, CC E2 April 2006 AA, BB, CC,DD, EE E3 April 2006 AA, BB
0
2820
by: lrobo01 | last post by:
I'm having a problem with Crystal report.net with ASP.NET. The problem occurs when exporting the report. The report uses a discrete parameter with multiple values. When the report is loaded into the crystal report view it shows all the values from the discrete parameter. But when I export the report it only shows the values from the last discrete parameter value enter. My code is listed below:
4
2921
by: Phille | last post by:
Hi I have a form with a few textboxes and some listboxes with multiple values allowed. I would like to use an append query to insert everything into a table but for some reason it just gives me no values at all for the listboxes (the textboxes are ok). Is there something that could be done. Thanks in advance
8
3899
by: smoky_flame via DotNetMonster.com | last post by:
hi, is it possible to take multiple values(int) seperated by commas as input from textbox in C# and draw a figure using those values. e.g. drawing a simple path using 1,2,3,4 values. -- Message posted via http://www.dotnetmonster.com
3
13887
by: questionit | last post by:
Hi How do i write multiple values in a single column, using INSERT INTO I have a listbox and i want all the selected values from that listbox inserted into a table column INSERT INTO Orders(OrderID, Items) VALUES ____?????________ Do i need to run the above statement in a loop?
1
3670
pbala
by: pbala | last post by:
I have a multiple select Listbox.When I click on it then it post backs and scroll back to the top of the Listbox. Is there any property to prevent it?
0
9518
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10433
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10212
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6777
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.