Post back with by double clicking item in listbox

ImDaFrEaK

Well-known member
Joined
Jan 7, 2006
Messages
416
Location
California
Programming Experience
5-10
How can I get a postback when someone double clicks an item in a listbox.

Sorry I am new to ASP and trying to get it down pat.
 
Add client side javascript for ondblclick event to the html element, in script you can make a submit.
Example form load code:
VB.NET:
[SIZE=2]ListBox1.Attributes.Add([/SIZE][SIZE=2][COLOR=#800000]"ondblclick"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"javascript:dbl()"[/COLOR][/SIZE][SIZE=2])[/SIZE]
Example javascript tag:
HTML:
<script type ="text/javascript">
function dbl() {document.forms[0].submit()}
</script>
Note that even though you have set the Listbox AutoPostback property false (default) you can still use the SelectedIndexChanged event handler (just doublelclick the listbox to generate the sub) to handle the listbox postback caused by the above client script. This still requires the event Handles clause, but postback will not trigger by selecting different listbox index unless doubleclicked or setting autopostback true.
 
this works great but now i have another problem and i know it's something simple that I just don't know or have thought of.

I have two links on my page that both redirect the user to another site that installs music software. When I click them they work great. When I first load the site and type a name in the field the listbox loads and the dblclick code you gave me via js works great as well. Here's the thing. If I click the link to install the player then double click the listbox it thinks I am clicking on the install link for the player again. I have the response.redirect code in the click event of the LinkButton and not in the load event or anything. Why would just reposting via that js code make it think I clicked the link again?
 
Changing the Javascript postback method worked. New code that makes postback from the specific Listbox control:
Example form load code:
VB.NET:
[SIZE=2][SIZE=2]ListBox1.Attributes.Add([/SIZE][SIZE=2][COLOR=#800000]"ondblclick"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"javascript:dbl("[/COLOR][/SIZE][SIZE=2] & ListBox1.ClientID & [/SIZE][SIZE=2][COLOR=#800000]")"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[/SIZE]
Example javascript tag:
HTML:
<script type ="text/javascript">
function dbl(ctl) {__doPostBack(ctl, "");}
</script>
 
dang it... ok... here's the delimma. All that works and I thought I understood it but I was wrong.

I have a listbox that displays multiple files. Currently when the user selects an item and then clicks the Get File button they recieve the saveas dialog box like they should. I want them to also be able to just dblclick the listbox to accomplish the same thing. The examples above fine. The problem I am having is there is no double click or click event to capture with the listbox during postback. I was just pasting the code in the Load or LoadComplete events but this was causing the listbox item to fire everytime a postback happens. So if they click a seperate link and the page reloads they get the same thing, a dialog box to save that file. If I knew a way to make the doubleclick of the listbox perform those functions or the dblclick of the listbox to cause the button to perform click or something I would be set i think. I just need a seperate event other than the load events. I bet this is an easy task for you but I am getting a headache thinking this ASP through. Web Porgramming is harder than windows programming by far... at least for now.

I tried your code above except I replace Listbox1.ClientID with Button1.ClientID
 
Like I said, use the ListBox.SelectedIndexChanged event. Even if that controls autopostback is false, the event will trigger by the manual postback. This requires that there actually has been a change in selected index, so initially there should be no index selected, also when you have handled that event you should reset it back to 'unselected' by setting LB.SelectedIndex = -1, this will ensure that if user doubleclicks same item again the event will trigger again when you postback.
 
Actually, while the above setup works perfectly, the more I think about the user interface here, the more I lend to opting for a simple autopostback=true on the listbox and no doubleclicking feature, also no 'get file' linkbutton necessary. You could just label the listbox "select/click file to download:". User may cancel download and select same 'index' again without problem because you reset selectedindex=-1 in the changed event. Doubleclicking the listbox to download file isn't intuitive UI, all active 'links' in webpages use single mouseclick (refer anchor tags behaviour (<a href...)).
 
Ok, I will try that but I think I had it that way to start with and people were clicking files options they didn't want and getting annoyed. But I didn't have it that way long. Let me try again. I am gonna send you the site private message so you can see it and maybe give me some advice.
 
Ok, i tried putting this code only in the listbox selected index changed event and everytime the page re-loads it tries to send the same file over and over again.

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Protected[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ListBox1_SelectedIndexChanged([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] ListBox1.SelectedIndexChanged
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] IsPostBack [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ListBox1.SelectedIndex < 0 [/SIZE][SIZE=2][COLOR=#0000ff]OrElse[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ListBox1.SelectedItem.ToString = [/SIZE][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]Response.Clear()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] FI [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FileInfo([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ListBox1.SelectedItem.ToString)
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Title = FI.Name
Response.AppendHeader([/SIZE][SIZE=2][COLOR=#800000]"content-disposition"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"attachment; filename="[/COLOR][/SIZE][SIZE=2] & FI.Name)
Response.ContentType = [/SIZE][SIZE=2][COLOR=#800000]"text/plain"
[/COLOR][/SIZE][SIZE=2]Response.WriteFile([/SIZE][SIZE=2][COLOR=#800000]"C:\Music\"[/COLOR][/SIZE][SIZE=2] & [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ListBox1.SelectedItem.ToString)
Response.Flush()
Response.End()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ListBox1.SelectedIndex = -1
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]

 
Back
Top