ListBox and SelectedItem.Value

M

Mike

Hello,

I have a web form with a Listbox. When trying to read the value of the last selected item with the statement below, I always get the first item, even if the user selected another item:
lstGrants.SelectedItem.Value)


I am new to ASP.NET, so I don't know whether AutoPostback should be set to true. This is a subject that it is still not clear to me. Also, when setting Autopostback to true, there is some data transfer between client and server (if I am not wrong) that I would like to avoid.

Any suggestion would be very welcome. I am having a hard time learning ASP.NET!

Thank you very much.
Mike
 
R

Raterus

sounds like you are rebinding the data to the listbox on every page_load, use..

if not page.ispostback then
'bind data
end if

Hello,

I have a web form with a Listbox. When trying to read the value of the last selected item with the statement below, I always get the first item, even if the user selected another item:
lstGrants.SelectedItem.Value)


I am new to ASP.NET, so I don't know whether AutoPostback should be set to true. This is a subject that it is still not clear to me. Also, when setting Autopostback to true, there is some data transfer between client and server (if I am not wrong) that I would like to avoid.

Any suggestion would be very welcome. I am having a hard time learning ASP.NET!

Thank you very much.
Mike
 
D

Davide Vernole [MVP]

Mike said:
Hello,

I have a web form with a Listbox. When trying to read the value of
the last selected item with the statement below, I always get the
first item, even if the user selected another item:
lstGrants.SelectedItem.Value)

Alternative you can use 1stGrant.SelectedValue also.

Where do you bind the ListBox ? In the load_Page method ? If yes, Do you
bind the ListBox everytime the page is loaded or only for the fist call ?
The right answer is only for the first call.

if(!IsPostBack)
{
......bind the list box
}
I am new to ASP.NET, so I don't know whether AutoPostback should be
set to true. This is a subject that it is still not clear to me.

In the properties page of the control you can find this property.
Also, when setting Autopostback to true, there is some data transfer
between client and server (if I am not wrong) that I would like to
avoid.

Yes sure. The post back mechanism send to the server the page content. For
more informations see this link:
http://msdn.microsoft.com/library/d...on/html/vbconwebformspageprocessingstages.asp
Any suggestion would be very welcome. I am having a hard time
learning ASP.NET!

For a fast begin use the quickstart tutorial ;-)
http://www.asp.net/Tutorials/quickstart.aspx
 
M

Mike

Thanks.


Davide Vernole said:
Hello,

I have a web form with a Listbox. When trying to read the value of
the last selected item with the statement below, I always get the
first item, even if the user selected another item:
lstGrants.SelectedItem.Value)

Alternative you can use 1stGrant.SelectedValue also.

Where do you bind the ListBox ? In the load_Page method ? If yes, Do you
bind the ListBox everytime the page is loaded or only for the fist call ?
The right answer is only for the first call.

if(!IsPostBack)
{
.....bind the list box
}
I am new to ASP.NET, so I don't know whether AutoPostback should be
set to true. This is a subject that it is still not clear to me.

In the properties page of the control you can find this property.
Also, when setting Autopostback to true, there is some data transfer
between client and server (if I am not wrong) that I would like to
avoid.

Yes sure. The post back mechanism send to the server the page content. For
more informations see this link:
http://msdn.microsoft.com/library/d...on/html/vbconwebformspageprocessingstages.asp
Any suggestion would be very welcome. I am having a hard time
learning ASP.NET!

For a fast begin use the quickstart tutorial ;-)
http://www.asp.net/Tutorials/quickstart.aspx
 
M

Mike

Thanks.


sounds like you are rebinding the data to the listbox on every page_load, use..

if not page.ispostback then
'bind data
end if

Hello,

I have a web form with a Listbox. When trying to read the value of the last selected item with the statement below, I always get the first item, even if the user selected another item:
lstGrants.SelectedItem.Value)


I am new to ASP.NET, so I don't know whether AutoPostback should be set to true. This is a subject that it is still not clear to me. Also, when setting Autopostback to true, there is some data transfer between client and server (if I am not wrong) that I would like to avoid.

Any suggestion would be very welcome. I am having a hard time learning ASP.NET!

Thank you very much.
Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top