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?
 
Luis PérezConnect With a Mentor Software 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
 
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
 
the_7th_kingAuthor Commented:
than you very much!
0
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.

All Courses

From novice to tech pro — start learning today.