Asp. NET application skill: JS completes the interactive instance of the ListBox content

Source: Internet
Author: User
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!



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.