Is DropdownList's SelectedIndex useless?

B

bogdan

I need to pass a dropdownlist's selected value from one page to another. I
thought that handling DataBound() and SelectedIndexChanged() events of the
dropdownlist is all I needed to do in order to 'capture' the currently
selected value - the former handler would 'capture' the initial value and
the latter one would deal with any selection changes due to user UI on the
client side. Well, this approach worked in some cases only. I found out
that the only reliable way to 'capture' the selected value is to get it in
Page_Load() if IsPostBack instead of SelectedIndexChanged(). So, the
working solution in this case seems to be a combination of DataBound() and
Page_Load() (only if IsPostBack). If this is the case then why would anyone
use SelectedIndexChanged()?

A simple example to illustrate the problem with a dropdownlist containing
Item0...Item4:
1) On PageA select Item2 and move to PageB, handle OnSelectedIndex() and
pass selected value in a query string
2) On PageB press browser's back button to get back to PageA (note that
Item2 is appears selected in the list), proceed as in step 1) but select
Item0

Note that SelectedIndexChanged() will not execute when you post back PageA
in step 2). As a result your query will end up with Item2's value.

Am I missing something obvious?

Thanks,
Bogdan
 
J

Just Me

Its a bit unclear as to what you are doing. Im with you until you say
1) On PageA select Item2 and move to PageB, handle OnSelectedIndex() and
pass selected value in a query string

Are you doing a response.redirect on Page A's postback to Page B ? and then
simply pressing the browsers back button ?

Or could this have been a postback on item2 and then simply clicking a link
to another site , coming back and selecting item0 and noting selectindex
hadnt changed ?

I added a ddl with four items and set autopostback and breakpointed the
selectIndexchanged event

1.) Run the app OK
2.) Select item 3 OK - Sees this on SelectIndexChanged as Index being this
item
3.) Run OK
4.) Navigate to another site. OK
5.) Back button OK
6.) Select Item 0 OK
7.) Breakpoint sees Index now being '0' which is correct. OK

So im not sure what's really causing the problem for you ?

Cheers
 
B

bogdan

Thanks for the reply.

Let me clarify my original post...

PageA has a dropdownlist and a linkbutton. The dropdownlist's autopostback
is set to false. The linkbutton's click event response.redirects to PageB.
So, the steps are:
1) PageA: Select Item2 in the dropdownlist and press the linkbutton, handle
OnSelectedIndex() (no problem here), handle linkbutton's Click() event and
response.redirect to PageB
2) PageB: press browser's "Back" button
3) PageA: (Item2 still showing as selected) select Item0 and press the
linkbutton, OnSelectedIndex() is not called!

Note that this problem occurs only if I select Item0 in step 3). Selecting
any other item generates OnSelectedIndex(). It almost looks like
dropdownlist assumes the currently selected index as 0 - regardless of what
is actually showing - when I navigate back to PageA.

Does this make any sense?

Thanks,
Bogdan
 
M

Microsoft Newsserver

Actually, when you think about it. This makes perfect sense to me. Afterall,
when you use the browsers back button you will end up doing a GET which will
initialise your select index to 0.
 
B

bogdan

But why would the browser display Item2 as selected on the page? To an
end-user this simply does not make any sense.
 
M

Microsoft Newsserver

OK, I think I understand what happeing here.

The reason you are experiencing an odd behaviour, is that the browser never
gets the page posted back for pageA when the buttonLink is pressed, it only
has the pre-postback page, however, when you back button your doing a GET
which willl set the reference to item 0. But this cofuses things because you
have viewstate available.

Not sure how to get around this, but, you may want to set the selectedIndex
on page load, to preserve the correct position.
 
B

bogdan

Thanks for the reply.

I gave up on OnSelectedIndex() and moved the logic to Page_Load().
Because of that I can't imagine why would anyone use OnSelectedIndex, and
that was the reason for my question in the title.

Thanks,
Bogdan
 

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,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top