want to detect selected item in asp.net listbox

i have an asp.net listbox named lstMembers

on page load, i have added attribute to this listbox which calls a javascript function on double clicking

lstMembers.Attributes.Add("ondblclick", "NewDivMessenger('" + lstMembers.SelectedIValueToString() + "')");

this will invoke the javascript function NewDivMessenger which accepts 1 argument

i want this argument to be the selected value in the listbox

i require to do this without postbacks

the above code does not work

someone please guide me

PS: codebehind is C#
LVL 2
the_7th_kingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

Luis PérezSoftware Architect in .NetCommented:
It doesn't work because the code that you execute in Page_Load looks for selected value "in that moment", not in the moment that the user makes double click.

It's not needed that you pass the argument to the NewDivMessenger javascript function. You can detect the selected value inside that function instead.

function NewDivMessenger() {
    var list = document.getElementById('<%=lstMembers.ClientID%>');
    var item = list.options[list.selectedIndex];
    //now you got item, which is a OPTION html element and has text and value properties
    //and, of course, is the selected item of lstMembers
}
0
the_7th_kingAuthor Commented:
didn't work

an alert box displaying the 'item' just shows [object] inside it ... not the actual selected item
0
the_7th_kingAuthor Commented:
btw, i want the selected value

selected index is of no use to me
0
Luis PérezSoftware Architect in .NetCommented:
As I said, what you get is the OPTION html tag, what has text and value properties.

If you want to go deeper, then:

function NewDivMessenger() {
    var list = document.getElementById('<%=lstMembers.ClientID%>');
    var item = list.options[list.selectedIndex];
    //now you got item, which is a OPTION html element and has text and value properties
    //and, of course, is the selected item of lstMembers

    var selected_text = item.text;
    var selected_value = item.value;
}
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
the_7th_kingAuthor Commented:
than you very much!
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.