asp.net C # Two ways to double-click events in the ListBox

Source: Internet
Author: User

ASP tutorial. NET C # Double-click events for the ListBox two ways

The listbox's double-click Event, on the foreground page the listbox is not double-clicked, so it is added through the script as follows:

Place a <asp:listbox runat= "server" id= "lsb_**" ></asp:listbox> control on the foreground page, and manually add a few values to this control in a simple test. And put a hidden field in the foreground to store the values you double-click:

<input type= "hidden" id= "Hdvalue" name= "Hdvalue" value= "false" runat= "Server"/>, and is represented by true and false.

Add on the foreground

<script type= "Text/vbscript" >

Option Explicit

Sub Lsb_**_ondblclick ()

Form1.hdvalue.value= "true"

Form1.submit ()

End Sub

</script>

It's written in the background:

In the Pageload event, write as follows:

if (IsPostBack) {

if (Hdvalue.value = = "true")

Lsb_**_dbclick ();

}

Then add the Lsb_**_dbclick () method in the background. A label above the foreground shows whether the item you double-click is the one you chose. Such as:

protected void Lsb_**_dbclick () {

This.label1.text = "Double click" +lsb_**.selecteditem.tostring ();

}

This allows you to add a double-click event for the listbox.


Method Two

How do you implement it by double-clicking in a ListBox and adding the selected item to another ListBox, but the ListBox control itself does not have that event? I thought of it. Client Script Javascrit, by consulting the relevant data, finally solved the problem, now write to share with you, hope to be helpful to everyone.
Here are three questions:
First: Double-click the Web page you want to perform the special effects code?
Note: The syntax of JavaScript code is correct, that is, each line should end with ";"
function Change ()
{
var addoption=document.createelement ("option");
var index1;
if (document.form1.listbox1.length==0) return (false);
Index1=document.form1.listbox1.selectedindex;
if (index1<0) return (false);
Addoption.text=document.form1.listbox1.options (index1). text;
Addoption.value=document.form1.listbox1.value;
Document.form1.listbox2.add (AddOption);
Document.form1.listbox1.remove (INDEX1);
}
Second: How do I convert JavaScript code to C # code?
public static void Listbox_dblclick (page Page,system.web.ui.webcontrols.webcontrol webcontrol,string Sourcecontrolname,string targetcontrolname)
{
Sourcecontrolname = "Document.form1." + sourcecontrolname;
Targetcontrolname = "Document.form1." + targetcontrolname;
String js = "<script language=javascript> function Change (sourcecontrolname,targetcontrolname)";
JS + + "{";
JS + + "var addoption=document.createelement" (' option '); n ";
JS + + "var index1; n ";
JS + = "if (sourcecontrolname.length==0) return (false); n";
JS + + "INDEX1=SOURCECONTROLNAME.SELECTEDINDEX; n ";
JS + = "if (index1<0) return (false); n";
JS + + "addoption.text=sourcecontrolname.options (index1). text; n ";
JS + + "Addoption.value=sourcecontrolname.value; n ";
JS + + "Targetcontrolname.add (addoption); n ";
JS + + "Sourcecontrolname.remove (index1) n";
JS + = "}";
JS + + "</script>";
Register the JavaScript;
Page.registerstartups Tutorial Cript ("", JS);
Adds a double-click event to the control;
Webcontrol.attributes.add ("OnDblClick", "Change" ("+ Sourcecontrolname +", "+ Targetcontrolname + ");");
}
In this method, Sourcecontrolname is the control that is to bind the double-click event, Targetcontrolname is the control that receives the selected item by double-clicking the event.
Here's the question of how to pass the object as a parameter to the JavaScript change function, where I use Sourcecontrolname, targetcontrolname to pass the name of the two ListBox, and then " Document.form1. " A change function that is combined into a string to pass to JavaScript, i.e.
Sourcecontrolname = "Document.form1." + sourcecontrolname;
Targetcontrolname = "Document.form1." + targetcontrolname;


Third: How do I add a double-click event for a control?
With Controlname.attributes.add ("Property name", "Function name or code");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.