drop down selected index

Discussion in 'ASP .Net' started by igotyourdotnet, Dec 10, 2006.

  1. 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?
     
    igotyourdotnet, Dec 10, 2006
    #1
    1. Advertising

  2. igotyourdotnet

    Mark Rae Guest

    "igotyourdotnet" <> wrote in message
    news:...

    > How can I accomplish this?


    Is there a specific reason that you haven't provided your code so that we
    can take a look at and solve your problem...?
     
    Mark Rae, Dec 10, 2006
    #2
    1. Advertising

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




    "Mark Rae" <> wrote in message
    news:...
    > "igotyourdotnet" <> wrote in message
    > news:...
    >
    >> How can I accomplish this?

    >
    > Is there a specific reason that you haven't provided your code so that we
    > can take a look at and solve your problem...?
    >
     
    igotyourdotnet, Dec 10, 2006
    #3
  4. igotyourdotnet

    Mark Rae Guest

    "igotyourdotnet" <> wrote in message
    news:...

    >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();
     
    Mark Rae, Dec 10, 2006
    #4
  5. I did this:
    ddCars.SelectedValue = Session["carmake"].tostring();
    and it still shows up twice.




    "Mark Rae" <> wrote in message
    news:...
    > "igotyourdotnet" <> wrote in message
    > news:...
    >
    >>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();
    >
     
    igotyourdotnet, Dec 10, 2006
    #5
  6. igotyourdotnet

    Mark Rae Guest

    "igotyourdotnet" <> wrote in message
    news:...

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


    Hmm - OK then - so how is ddCars getting populated...?
     
    Mark Rae, Dec 10, 2006
    #6
  7. 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" <> wrote in message
    news:%...
    > "igotyourdotnet" <> wrote in message
    > news:...
    >
    >>I did this:
    >> ddCars.SelectedValue = Session["carmake"].tostring();
    >> and it still shows up twice.

    >
    > Hmm - OK then - so how is ddCars getting populated...?
    >
     
    igotyourdotnet, Dec 10, 2006
    #7
  8. igotyourdotnet

    Mark Rae Guest

    "igotyourdotnet" <> wrote in message
    news:...

    > am i missing something or executing the code in the wrong manner?


    Who can tell...?

    Let me ask you a second time - how is ddCars getting populated...?
     
    Mark Rae, Dec 11, 2006
    #8
  9. igotyourdotnet

    Mark Rae Guest

    "igotyourdotnet" <> wrote in message
    news:...

    > 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...
     
    Mark Rae, Dec 11, 2006
    #9
  10. 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 (, 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.
     
    Walter Wang [MSFT], Dec 11, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J. Babe

    Drop down list selected index

    J. Babe, Aug 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    9,564
    Steve C. Orr, MCSD
    Aug 5, 2003
  2. Anthony Judd

    Drop Down List Selected Index!!!

    Anthony Judd, Nov 1, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    427
    =?Utf-8?B?c3Jpbmk=?=
    Nov 1, 2004
  3. Replies:
    0
    Views:
    4,896
  4. DCC700
    Replies:
    0
    Views:
    954
    DCC700
    Jul 14, 2006
  5. Replies:
    0
    Views:
    192
Loading...

Share This Page