• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 516
  • Last Modified:

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#
0
the_7th_king
Asked:
the_7th_king
  • 3
  • 2
1 Solution
 
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
 
the_7th_kingAuthor Commented:
than you very much!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now