drop down selected index

I

igotyourdotnet

In my web app i have a drop down on my pages. I pass the selected item in
the drop down from page to page. The issue is when I go back to page one I
see the data in there twice. How can I have the data item once but selected?

example:
page 1 dropdown
Sell
Buy
Trade

if I select Sell then go to page 2 Sell is shown in the drop down but listed
twice, like this
Sell
Sell
Buy
Trade

I want to show whatever item is selected from page 1
How can I accomplish this?
 
I

igotyourdotnet

I have this when the page loads

if(Session["carmake"] != null)
{
ddCars.SelectedItem.Text = Session["carmake"].tostring();
}

and the carmake shows up twice in the drop down, though the correct one is
showing, it just shows twice.
 
M

Mark Rae

I have this when the page loads

if(Session["carmake"] != null)
{
ddCars.SelectedItem.Text = Session["carmake"].tostring();
}

and the carmake shows up twice in the drop down, though the correct one is
showing, it just shows twice.

Well there you are then!

ddCars.SelectedValue = Session["carmake"].tostring();
 
I

igotyourdotnet

I did this:
ddCars.SelectedValue = Session["carmake"].tostring();
and it still shows up twice.




Mark Rae said:
I have this when the page loads

if(Session["carmake"] != null)
{
ddCars.SelectedItem.Text = Session["carmake"].tostring();
}

and the carmake shows up twice in the drop down, though the correct one
is showing, it just shows twice.

Well there you are then!

ddCars.SelectedValue = Session["carmake"].tostring();
 
I

igotyourdotnet

when the page loads it looks like this:

if(!page.isPostback())
{
if(Session["carmake"] !=null;
{
getCars(); //if this is not called i get an 'object not reference
error'
ddCars.SelectedItem.Text = Session["carmake"].tostring();
}
}

and the car makes are listed but the one passed back to the page is listed
in the drop down twice.
am i missing something or executing the code in the wrong manner?

Mark Rae said:
I did this:
ddCars.SelectedValue = Session["carmake"].tostring();
and it still shows up twice.

Hmm - OK then - so how is ddCars getting populated...?
 
M

Mark Rae

ddCars.SelectedItem.Text = Session["carmake"].tostring();

You do realise, don't you, that the above code doesn't actually select a
different item in the DropDownList...?

Look at it closely - it says: set the text of the currently selected item to
the value of Session["carmake"]

That's why I suggested you should use SelectedValue instead.

If your DropDownList looks like this:

<blank>
Sell
Buy
Trade

and the first item (i.e. <blank>) is currently selected, running
ddCars.SelectedItem.Text = Session["carmake"].tostring(); will set the text
of the <blank> item to the value of the Session object. If the value of the
Session object is "Sell" at the time your code is run, your DropDownList
will look like this:

Sell
Sell
Buy
Trade

Once again, unless you say how the DropDownList is being populated, it will
be almost impossible to help you any further...
 
W

Walter Wang [MSFT]

Hi igotyourdotnet,

Based on my understanding, the scenario is:
1) You have two DropDownList with same content (maybe bound to the same
data source) on two pages
2) When navigate to second page from first page, you store the first
DropDownList's SelectedItem.Text in session; in second page, you want to
set the second DropDownList's selected item to the one stored in session.

Please correct me if I've misunderstood anything.

I suggest you use a for-loop to iterate within the second DropDownList's
Items collection to find the one matches and set SelectedIndex accordingly:

string s = Session["carmake"].ToString();
for (int i = 0; i < ddlCars.Items.Count; i++)
{
if (ddlCars.Items.Text == s)
{
ddlCars.SelectedIndex = i;
break;
}
}



Hope this helps.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top