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?
LVL 3
Abdu_AllahAsked:
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
0

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.
0
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 -->
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Abdu_AllahAuthor Commented:
Can you explain your idea with code please? because whenever you copy it the prevoius one will deleted!
 
0
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
0
Abdu_AllahAuthor Commented:
I want the old selected Item in SelectedIndexChanged function.
0
Jorge PaulinoIT Pro/DeveloperCommented:
Don't understand!
You just have to change:
Session("new") = Me.ListBox1.SelectedIndex
with
Session("new") = Me.ListBox1.SelectedItem.Text
 
Or do I understood it wrong ?
 
0
Abdu_AllahAuthor Commented:
>Or do I understood it wrong ?
Exactly.
0
Jorge PaulinoIT Pro/DeveloperCommented:
>> Exactly
Can you explain ?
0
tiagosalgadoCommented:
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.
0
Abdu_AllahAuthor Commented:
But by this way you cannot get the old value if you select a new one!
0
tiagosalgadoCommented:
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.
0
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.
0
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
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.