Asp tutorial. net c # Two Methods for double-click events to listbox
The double-click event of listbox. The listbox event is not double-clicked on the front-end page. Therefore, you must use the script to add the event as follows:
Place a <asp: listbox runat = "server" id = "lsb _ **"> </asp: listbox> control on the front-end page, in a simple test, manually add several values to the control and put a hidden field on the foreground to store the values that you double-click, for example:
<Input type = "hidden" id = "hdvalue" name = "hdvalue" value = "false" runat = "server"/>, expressed by true and false.
Add
<Script type = "text/vbscript">
Option explicit
Sub lsb _ ** _ ondblclick ()
Form1.hdvalue. value = "true"
Form1.submit ()
End sub
</Script>
Write this in the background:
Write the following in the pageload event:
If (ispostback ){
If (hdvalue. value = "true ")
Lsb _ ** _ dbclick ();
}
Then add the lsb _ ** _ dbclick () method in the background. A label at the top of the front-end is used to show whether the double-click item is selected. For example:
Protected void lsb _ ** _ dbclick (){
This. label1.text = "double-click" + lsb _ **. selecteditem. tostring ();
}
In this way, you can add a double-click event for listbox.
Method 2
By double-clicking in a listbox, you can add the selected item to another listbox, But The listbox control does not have this event. How can this problem be achieved? I thought of the client script mongocrit. After reading the relevant information, I finally solved this problem. Now I want to share it with you, hoping to help you.
There are three problems:
1. What is the special webpage effect code to be executed by double-clicking?
Note: the syntax of javascript code should be correct, that is, each line should end;
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 to convert javascript code to c # code?
Public static void listbox_dblclick (page, system. web. ui. webcontrols. 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 example ("", js );
// Add a double-click event for the control;
Webcontrol. attributes. add ("ondblclick", "change (" + sourcecontrolname + "," + targetcontrolname + ");");
}
In this method, sourcecontrolname is the control to bind the double-click event, and targetcontrolname is the control to receive the selected items of double-click events.
Here is a question about how to pass an object as a parameter to the change function in javascript. Here I use sourcecontrolname and targetcontrolname to pass the names of two listboxes, and then. form1. "combine them into a string to pass the change function to javascript, that is
Sourcecontrolname = "document. form1." + sourcecontrolname;
Targetcontrolname = "document. form1." + targetcontrolname;
Third, how do I add double-click events for controls?
Use controlname. attributes. add ("attribute name", "function name or code ");