Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All ,

I have listbox to select multiple items . I need to save the listbox value in selected order rather than its unique id order . Anyone please help .

What I have tried:

<pre><asp:ListBox ID="ddlnonmonetory" SelectionMode="Multiple" class="form-control multiselectddl req" runat="server"></asp:ListBox> 

SetMultiBD(ddlnonmonetory, PL.dt.Rows[0]["NonMonetoryApprovalHeirarchy"].ToString());


void SetMultiBD(ListBox ddl, string ids)
   {
       ddl.SelectedIndex = -1;
       foreach (var item in ids.Split(','))
       {
           foreach (ListItem item2 in ddl.Items)
           {
               if (item2.Value == item)
               {
                   item2.Selected = true;
                   break;
               }
           }
       }
   }

void GetMonitoryApprovals()
   {
       MenuMasterPL PL = new MenuMasterPL();
       PL.OpCode = 103;
       MenuMasterDL.returnTable(PL);
       ddlmonetory.DataValueField = "id";
       ddlmonetory.DataTextField = "Name";
       ddlmonetory.DataSource = PL.dt;
       ddlmonetory.DataBind();

       ddlnonmonetory.DataValueField = "id";
       ddlnonmonetory.DataTextField = "Name";
       ddlnonmonetory.DataSource = PL.dt;
       ddlnonmonetory.DataBind();
   }




 void SaveUpdateData()
    {
        DesignationPL PL = new DesignationPL();
        PL.DesignationId = hidOutid.Value;
        PL.name = txtDesignation.Text;
        PL.Department = ddldepartment.SelectedValue;
        PL.SubDepartment = ddlSubDepartment.SelectedValue;
        PL.SubMenu = Request.Form[ddlSubMenu.UniqueID];
        PL.Associates = Request.Form[ddlassociates.UniqueID];
        PL.Sequence = txtOrder.Text;
        PL.Category = ddl_category.SelectedValue;
        PL.MonetoryApprovals = Request.Form[ddlmonetory.UniqueID];
        PL.NonMonetoryApprovals = Request.Form[ddlnonmonetory.UniqueID]; // here the order is based on unique id . I need to get it based on selected order .
}
Posted
Updated 17-May-23 22:04pm

1 solution

A ListBox is rendered as a <select> element[^]. That element submits the selected items in the order they were declared in the source, not the order in which the user selects them.

If you need to know the order in which the user selected the items, then you will need to use JavaScript. You will need to monitor the list's change event; get the list of selected options; compare it to the previously selected options to find out which option has just been selected; then store that list in a hidden field. You can then use the hidden field on the server to retrieve the ordered list.

For example:
JavaScript
document.addEventListener("DOMContentLoaded", () => {
    "use strict";
    const options = document.getElementById("options");
    const orderedOptions = document.getElementById("orderedOptions");
    
    const getAllSelectedOptions = () => [...options.options].filter(o => o.selected).map(o => o.value);
    
    const getAndValidateInitialSelectedOptions = () => {
        const initialValue = orderedOptions.value.split(",");
        const selectedOptions = getAllSelectedOptions();
        if (initialValue.some(x => !selectedOptions.includes(x))) {
            console.debug("Selected options does not match ordered options", initialValue, selectedOptions);
            return selectedOptions;
        }
        if (selectedOptions.some(x => !initialValue.includes(x))) {
            console.debug("Selected options does not match ordered options", initialValue, selectedOptions);
            return selectedOptions;
        }
        
        return initialValue;
    };
    
    let selectedOptions = getAndValidateInitialSelectedOptions();
    console.debug("Initial selected values", selectedOptions);
    
    options.addEventListener("change", () => {
        const allSelectedOptions = getAllSelectedOptions();
        
        // Remove any options which were de-selected;
        selectedOptions = selectedOptions.filter(x => allSelectedOptions.includes(x));
        
        // Add any options which were selected:
        allSelectedOptions.forEach(value => {
            if (!selectedOptions.includes(value)) {
                selectedOptions.push(value);
            }
        });
        
        orderedOptions.value = selectedOptions.join(",");
    });
});
Demo[^]
 
Share this answer
 
Comments
Member 12926744 18-May-23 4:17am    
Thank you Richard for the solution . I have tried it and its working .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900