I inherited an Web Forms project and am still somewhat new to Web development. On this page, we have 2 listboxes: lstCaseLoad, that is populated with with "Caseloads" (ID numbers) and lstAssignedCaseLoad, that is populated with Caseloads selected by the Form User. The user can select Caseloads from lstCaseLoad and hit a button to remove and transfer them to lstAssignedCaseLoad, which is done clientside with JavaScript. The user can also do the opposite, removing from lstAssigned to lstCaseLoad on clientside. The user has to hit a Submit button which will Post and save the changes to SQL.
Adding new CaseLoads from lstCaseloads to lstAssigned and submitting works fine. Removing from lstAssigned is where I'm facing problems. lstAssigned seems to become null when Submitting and therefore can't save. Code below.
Listboxes:
<asp:ListBox ID="lstCaseLoad" runat="server" SelectionMode="Multiple" />
<asp:ListBox ID="lstAssignedCaseLoad" runat="server" Height="175px" SelectionMode="Multiple" />
//Adding Caseload to Assigned
$("#btnAddCaseLoad").bind("click", function () {
var options = $("[id*=lstCaseLoad] option:selected");
$("[id*=lstAssignedCaseLoad]").append($(options).clone());
$(options).remove();
return false;
});
//Removing Caseload from Assigned
$("#btnRemoveCaseLoad").bind("click", function () {
var options = $("[id*=lstAssignedCaseLoad] option:selected");
$("[id*=lstCaseLoad]").append($(options).clone());
$(options).remove();
return false;
});
Code Behind
protected void Page_Load()
{
if (!IsPostBack)
{
//Retrieving and Binding listboxes. No issues here
GetData();
BindData();
}
else
{
var test= Request.Form.AllKeys;
//Temp variable to check all elements for debugging. lstAssignedCaseLoad is missing when deleting. Is fine when adding new caseload
//Read listAssignedCaseLoad items to comma seperated strings. Becomes null when trying to remove items
string strCaseLoad = Request.Form[lstAssignedCaseLoad.UniqueID];
//Transfer string strCaseload to (field)List<string> for saving. Works fine when string is not empty
userCaseLoadList = ProcessListString(strCaseLoad, lstAssignedCaseLoad);
}
}
//Submit button method. Works as intended
protected void EditUser_Click(object sender, EventArgs e)
{
identityMgr.EditUser(Email.Text, userRoleList, userCaseLoadList);
}
I tried searching and can't quite find the same issue. Thanks for your time and help!
This is because of "selected" attribute of option in listbox. If you select the option in "lstCaseLoad" which doesn't have the "selected" attribute and click on remove button then it will add to "lstAssigned" but the added option also dont have this "selected" attribute because we are cloning. So if you want to do the reverse process then you have to select(click) that option again from "lstAssigned" before clicking on remove. You can observe that "selected" attribute behavior by inspecting the options before button click.