Monday, March 26, 2012

listbox ondblclick attribute

Hi

I am trying to invoke a listboxes double click attribute to utilize the Ajax.Net functionality.

I have a listbox in an UpdatePanel, and using the OnSelectedIndexChanged event I successfully perform client side actions using Ajax.
I now want to perform an action when a user double clicks on an item of the listbox. (remove this item)

I tried copying the OnSelectedIndexChanged attribute value generated by dot net and adding it through the code:

lbMyListBox.Attributes.Add("ondblclick","javascript:setTimeout('__doPostBack(\\'DoubleClick\\',\\'\\')', 0)");

This however keeps causing a page post back.
Is it possible to use the double click attribute?
I have searched everywhere without much luck

TIA

You will need to subclass the ListBox control and create the ondblclick event for postback usage... and remove the existing onSelectedIndexChanged event. This topic deals with fairly advanced .Net web control programming.

Here's an example that is quick and dirty... and does not fully take care of all possible scenarios.

public class MyListBox : ListBox, IPostBackEventHandler, IPostBackDataHandler {public event EventHandler OnDoubleClick;#region IPostBackEventHandler Membersvoid IPostBackEventHandler.RaisePostBackEvent(string eventArgument) {if (eventArgument =="ondblclick") {if (OnDoubleClick !=null)this.OnDoubleClick(this, EventArgs.Empty); } }#endregion protected override void OnPreRender(EventArgs e) {base.OnPreRender(e);this.Attributes.Add("ondblclick", Page.ClientScript.GetPostBackEventReference(this,"ondblclick")); }protected override void Render(HtmlTextWriter writer) {//Here we will remove the onchange event through string manipulation. StringWriter sw =new StringWriter(); HtmlTextWriter newWriter =new HtmlTextWriter(sw);base.Render(newWriter); sw.Flush();string html = sw.ToString();int indexofOnChg=html.IndexOf("onchange=\""); sw.Close(); html = html.Remove(indexofOnChg,html.IndexOf(" ", indexofOnChg) - indexofOnChg); writer.Write(html); } }

Thanks for your advice

Will give it a wirl

No comments:

Post a Comment