asp.net|js| Skills | interactive
The operation effect is as follows:
I'm doing a website about university rankings these days, it encountered the problem in the above diagram, need to add a new school group to an object, which used a ListBox, and to do two of the two listbox interaction, there is no doubt that there are two ways: one is on the server side and the client Non-stop postback, This will undoubtedly greatly reduce efficiency. This is the embodiment of JavaScript in the client advantage. The following introduction, but also insufficient place hope everybody points out!
Here is the JS code: with four functions corresponding to four buttons, complete the function.
<script type= "Text/javascript" >
function SelectAll ()
{
var Lst1=window.document.getelementbyid ("<%=lb_sourse.clientid%>");
var length = Lst1.options.length;
var string = window.document.getElementById ("<%=hf_newname.clientid%>")
for (Var i=0;i<length;i++)
{
var v = lst1.options[i].value;
var t = Lst1.options[i].text;
var Lst2=window.document.getelementbyid ("<%=lb_newname.clientid%>");
Lst2.options[i] = new Option (t,v,true,true);
String.value+=v;
}
}
function Delall ()
{
var Lst2=window.document.getelementbyid ("<%=lb_newname.clientid%>");
var length = Lst2.options.length;
for (Var i=length;i>0;i--)
{
Lst2.options[i-1].parentnode.removechild (Lst2.options[i-1]);
}
}
function SelectOne ()
{
var string = window.document.getElementById ("<%=hf_newname.clientid%>")
var Lst1=window.document.getelementbyid ("<%=lb_sourse.clientid%>");
var Lst2=window.document.getelementbyid ("<%=lb_newname.clientid%>");
var Lstindex=lst1.selectedindex;
var length = Lst2.options.length;
var isexists = false;
if (lstindex<0)
Return
else if (length!= null)
{
for (var i=0;i < length; i++)
{
if (Lst2.options[i].text = = Lst1[lstindex].text&&lst2.options[i].value = = lst1[lstindex].value)
{
Isexists = true;
}
}
}
Else
{
Return
}
if (isexists = = False)
{
var v = lst1.options[lstindex].value;
var t = Lst1.options[lstindex].text;
Lst2.options[lst2.options.length] = new Option (t,v,true,true);
String.value+=v;
}
Else
{
Alert ("The selected entry already exists");
return false;
}
}
function DeLone ()
{
var Lst2=window.document.getelementbyid ("<%=lb_newname.clientid%>");
var Lstindex=lst2.selectedindex;
if (lstindex>=0)
{
var v = lst2.options[lstindex].value+ ";";
Lst2.options[lstindex].parentnode.removechild (Lst2.options[lstindex]);
}
}
</script>
Need to explain is because the JS script is executed on the client, so the server-side control is not able to call JS, because the ID can not be found, but with the <%=lb_newname.clientid%> method cleverly solve the problem, is the ASP control has the client ID, So that we can call the.
Hope to help you!