ASP.Net - <select> elements are not sent back after adding jQuery list

I have an ASP.Net ListBox that I am trying to populate through jQuery using the following snippet:

$("#MyList_btnAddAll").click(function(e) { e.preventDefault(); $('#MyList_lstAll option').appendTo('#MyList_lstSelected'); }); 

There are two ListBoxes in the code, one of which is the "source" and the other is the "destination". As you can say above, ListBoxes are MyList_lstAll and MyList_lstSelected. They appear in the browser as elements, as you would expect.

jQuery works fine, items move from one ListBox to another, the DOM is updated, but when I submit my page, postback does not indicate any changes to the ListBox. I know there are gotchas involving jQuery and ASP.Net postbacks, but can anyone reflect on what is happening and how can I make this work?

[EDIT]

Upon request, several more ASP.Net and HTML appear here. ListBox declarations and buttons in the ascx control that contains them are listed below:

  <GLP:ListBox ID="lstAll" CssClass="LIST_BOX_MEDIUM" runat="server" SelectionMode="Multiple"/> <asp:LinkButton ID="lnkAddAll2" CssClass="LIST_SELECT" runat="server" OnClick="btnAddAll_Click"/> <GLP:ListBox ID="lstSelected" CssClass="LIST_BOX_MEDIUM" runat="server" SelectionMode="Multiple"/> 

And the resulting HTML:

 <select class="LIST_BOX_MEDIUM" id="MyList_lstAll" multiple="multiple" name="MyList:lstAll" size="4"> <option value="641">Item1</option><option value="598">Item2</option> </select> <input type="submit" class="BUTTON_SMALL_N0_IMAGE" id="MyList_btnAddAll" value="Add All" name="MyList:btnAddAll" style="color: rgb(0, 0, 0);"> <select class="LIST_BOX_MEDIUM" id="MyList_lstSelected" multiple="multiple" name="MyList:lstSelected" size="4"> <option value="642">Item3</option><option value="599">Item4</option> </select> 

I know that changes to the jQuery / ListBox are not reflected in the ViewState, but since they are in the DOM when the page is published, they will not be included in the postback data and then matched by the appropriate controls?

+4
source share
3 answers

I think you will need to get the seletcted item list directly from the request, but rather thsn from asp.net control properties, for example:

 string results = Request.Form[list_box.UniqueID]; 
+3
source

IIRC, you cannot get this to work because ASP.Net expects the ListBox to postback. The way I used in the past is to create a hidden input field using runat = "server" and fill in the selected identifier there, separated by a comma.

The hidden input value will be available after the postback.

+4
source

The problem is that your control is recreated in the postback using the view state (or control state), which, of course, does not reflect your changes. One possibility is to access mail directly using the Server-side Request object (as Ray said before)

+1
source

All Articles