Thursday 2 August 2007

ASP.NET - Double click a ListBox

Ever been frustrated by a ListBox not having an OnDoubleClick event? Do not worry, all you have to do is add this simple line to the Page_Load event:



ListBox1.Attributes.Add("ondblClick", "{document.getElementById('" + Button1.UniqueID + "').click();return false;} ")



Now when you double click an item in the listbox, it will do the actions in the Button1_OnClick event. If you wanted to process something with the selected value in the ListBox, simply use the ListBox1.SelectedValue property within the Button1_OnClick event. A similar technique can also be used for other controls.

Simple but useful!

No comments: