I'm having a hard time figuring out how to move items between a left and right listbox server-side.
I have a populated listbox from the database on the left and I'm trying to add items to the box on the right and save those values to the database.
Can anyone help me? I've searched everywhere and haven't found any code that's helpful!
I've tried doing something like this, but when I hit the submit button, it never saves the list. I'm so lost.
Also, here's the javascript... I'm trying to see if there's a way to do it without JS maybe with Ajax, but I'm a beginner with Ajax.
I have a populated listbox from the database on the left and I'm trying to add items to the box on the right and save those values to the database.
Can anyone help me? I've searched everywhere and haven't found any code that's helpful!
Code:
protected void Submit_Click(object sender, EventArgs e) { //foreach (ListItem item in SecondList.Items) // { // string sql1 = "INSERT into sch_relations_ref VALUES (@relation_id, @resource_id)"; // SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["BayNetConnectionString"].ConnectionString); // SqlCommand command = new SqlCommand(sql1, con); // SqlParameter relation_id = new SqlParameter("@relation_id", SqlDbType.Int); // command.Parameters.Add(@relation_id); // SqlParameter resource_id = new SqlParameter("@resource_id", item.Value); // command.Parameters.Add(@resource_id); // con.Open(); // command.ExecuteNonQuery(); // con.Close(); //string value1 = hid_Items.Value; //Response.Write(value1); // } }
Also, here's the javascript... I'm trying to see if there's a way to do it without JS maybe with Ajax, but I'm a beginner with Ajax.
Code:
// Begin --> function move(fbox, tbox) { var arrFbox = new Array(); var arrTbox = new Array(); var arrLookup = new Array(); var i; for (i = 0; i < tbox.options.length; i++) { arrLookup[tbox.options[i].text] = tbox.options[i].value; arrTbox[i] = tbox.options[i].text; } var fLength = 0; var tLength = arrTbox.length; for (i = 0; i < fbox.options.length; i++) { arrLookup[fbox.options[i].text] = fbox.options[i].value; if (fbox.options[i].selected && fbox.options[i].value != "") { arrTbox[tLength] = fbox.options[i].text; tLength++; } else { arrFbox[fLength] = fbox.options[i].text; fLength++; } } arrFbox.sort(); arrTbox.sort(); fbox.length = 0; tbox.length = 0; var c; for (c = 0; c < arrFbox.length; c++) { var no = new Option(); no.value = arrLookup[arrFbox[c]]; no.text = arrFbox[c]; fbox[c] = no; } for (c = 0; c < arrTbox.length; c++) { var no = new Option(); no.value = arrLookup[arrTbox[c]]; no.text = arrTbox[c]; tbox[c] = no; } } function selectAll(box) { for (var i = 0; i < box.length; i++) { box[i].selected = true; } } // End -->
Comment