How to get the changed order after using JavaScript to change the values and order of items such as the ListBox control

Source: Internet
Author: User
Control methods:
Each time you change the order or change the value, all the item in the current listbox is saved in the Hidden tab through a for () loop, and this hidden label can only be an HTML control (remember: You cannot use the server control textbox.


Because the visible is set to false, it will not have this element on the page, and JavaScript will also because there is no corresponding element and error, and finally loopback to the server through a decomposition function to decompose the correct order and value


Specific:
Script
<script language= "JavaScript" >
<!--
function Remembernow ()
{
Record the current selection and save the values and order in the current list in hidden
Lstresult is the server control listbox
Txtselecteditems is <input type=hidden>.
var count;
var str= "";
Count=form1.lstresult.options.length;
for (Var i=0;i<count;i++)
{
Str+=form1.lstresult.options[i].value + "," + Form1.lstresult.options[i].text + ";";
}
form1.txtselecteditems.value=str;//For example: 1, Leng 2, gently flying;
}
-->
</script>
Server-side code:
private void Page_Load (object sender, System.EventArgs e)
{
Try
{
Fills the modified order and values into the target listbox
The Parseit function is used to decompose the correct values and order
Lstresult.datasource=parseit (Txtselecteditems.value);
Lstresult.datavaluefield= "ID";
Lstresult.datatextfield= "Name";
Lstresult.databind ();
}
catch (Exception ex)
{
Response.Write (ex. message);
}
}
Private ICollection Parseit (String str)
{
DataTable dtl=new DataTable ();
Dtl. Columns.Add (New DataColumn ("ID"));
Dtl. Columns.Add (New DataColumn ("Name"));
int i=0;
Int J;
while (J=str. IndexOf (";", I)) >=0)
{
string tmp;
Tmp=str. Substring (i,j-i);
String Parsenumber,parsename= "";
Parsenumber=tmp. Substring (0,tmp. IndexOf (","));
Parsename=tmp. Substring (TMP. IndexOf (",") +1,tmp. Length-tmp.indexof (",")-1);
Try
{
DataRow Dr=dtl. NewRow ();
dr["ID"]=convert.toint32 (Parsenumber);
dr["Name"]=parsename;
Dtl. Rows.Add (DR);
}
Catch
{}
i=j+1;
}
Dtl. AcceptChanges ();
Return DTL. DefaultView;
}



Related Article

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.