problem with multiple values of Listbox

  • Thread starter Thread starter Bob Malcoprs
  • Start date Start date
B

Bob Malcoprs

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.Text =
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=true
SelectionMode=Multiple>
<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_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles

ListBox1.SelectedIndexChanged
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
 
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.Text =
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=true
SelectionMode=Multiple>
<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_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles

ListBox1.SelectedIndexChanged
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=true)

To solve your problem set an initial state for the labels

Label1.Text = ""
Label2.Text = ""
Label3.Text = ""
 
Yes, that's it. Thanks

Alexey Smirnov said:
Labels keep the value after postback (AutoPostBack=true)

To solve your problem set an initial state for the labels

Label1.Text = ""
Label2.Text = ""
Label3.Text = ""
 
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,

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 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
 
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.aspx

<%@ Page Language="vb" %>
<script runat=server>
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
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.
 
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.
 
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.IPostBackDataHandler.LoadPostData (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
IPostBackDataHandler 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=select, 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
 
Thanks

Alexey Smirnov said:
This is little bit tricky thing. The TextBox keeps its value because
this is a control that implements
System.Web.UI.IPostBackDataHandler.LoadPostData (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
IPostBackDataHandler 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=select, 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
 
Back
Top