Get the prevoius selected item for the ListBox in SelectedIndexChanged function

Hi, Is it possible to get the selected prevoius selected(the one before the current one) item for the ListBox in SelectedIndexChanged function?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jorge PaulinoIT Pro/DeveloperCommented:
Store that on a session and you can know it

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Abdu_AllahAuthor Commented:
It will be change also once the the user select anothe item.
Jorge PaulinoIT Pro/DeveloperCommented:
>> It will be change also once the the user select anothe item.
Why ?
When you click on a item you store that value(index) on a session. After you do all your work, you copy to another one (the previous selected item).
This way you will have the actual and the old one.
<!-- richText -->
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Abdu_AllahAuthor Commented:
Can you explain your idea with code please? because whenever you copy it the prevoius one will deleted!
Jorge PaulinoIT Pro/DeveloperCommented:

In the SelectedIndexChanged event you can do:

Session("old") = Session("new")
Session("new") = Me.ListBox1.SelectedIndex ' or you can use a value
This way you store the previous value (or in this case index) of the listbox and the new one.

PS: Don't forget to set the AutoPostBack in the listbox to true
Abdu_AllahAuthor Commented:
I want the old selected Item in SelectedIndexChanged function.
Jorge PaulinoIT Pro/DeveloperCommented:
Don't understand!
You just have to change:
Session("new") = Me.ListBox1.SelectedIndex
Session("new") = Me.ListBox1.SelectedItem.Text
Or do I understood it wrong ?
Abdu_AllahAuthor Commented:
>Or do I understood it wrong ?
Jorge PaulinoIT Pro/DeveloperCommented:
>> Exactly
Can you explain ?
1. Add a ASP.NET Hidden Field to store your old value.
2. Add this line to Page_Load ListBox1.Attributes.Add("onfocus", String.Format("{0}.setAttribute('value',this.value)", hidden.ClientID));

Access to hidden field value and get your previous selected item.
Abdu_AllahAuthor Commented:
But by this way you cannot get the old value if you select a new one!
Yes you can. You're storing to hidden field the value selected when your listbox get focus. When you click to another one, if you get the hidden field value you get the previous item value.
Rupesh PCommented:
Add a hidden field in the page.

protected void DropDownList1_SelectedIndexChanged(object sender,
                  EventArgs e)
             //current value
             int current = DropDownList1.SelectedIndex;
             int oldValue = hiddenField1.value;
             hiddenField1.value = current;

now you have the previously selected value in the oldValue variable.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.