0

So, I have a form design section here, where there is a "save" button and a "slot grid" button. AsyncPostBackTrigger is implemented on Save button for update panel. My problem is that after selecting values from listbox which gets binded on page load,whenever i click "btnshowslotgrids" button, the listbox looses all selected values.what should i do to prevent it?

  <asp:UpdatePanel ID="Upd" runat="server" UpdateMode="Conditional">
<ContentTemplate>
    <table style="width: 100%; margin: auto; min-height: 500px;">
     <tr>
       <td>
           <asp:ListBox SelectionMode="Multiple" ClientIDMode="Static" 
           ID="lbEduType" runat="server"
           CssClass="multiselect form-control" DataValueField="Id" 
           DataTextField="Name" multiple="multiple"></asp:ListBox>
       </td>
        <td>
         <asp:Button ID="btnshowslotgrids" Text="Slot Grids" runat="server" 
         CssClass="btn btn-primary" OnClick="btnshowslotgrids_Click"/>
       </td>
     </tr>
    </table>  
<table id="tblAssSubjSave" runat="server">
  <tr>
    <td>
   <asp:Button runat="server" ID="btnSaveAssessment"  Text="Save Slot" 
    ValidationGroup="vgAssessments" OnClick="btnSaveAssessment_Click" />
   <asp:Button runat="server" ID="btnCancelAssessment" Text="Reset" 
   CausesValidation="false"    OnClick="btnCancelAssessment_Click" />
  </td>
  </tr>
 </table>
  </ContentTemplate>
         <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btnSaveAssessment" />
        </Triggers>
  </asp:UpdatePanel>
ankita25
  • 35
  • 1
  • 9
  • Can you show what is happening inside Page_Load? I suspect you are binding the data to the ListBox without checking for `IsPostBack` – VDWWD Jul 02 '18 at 12:21
  • @VDWWD...I am binding listbox in IsPostBack event inside Page_Load and its going inside it only once. – ankita25 Jul 03 '18 at 02:36

2 Answers2

0

You should bind your listbox inside the !IsPostback event.

if (!IsPostBack)
{
    //bind your listbox here
}

What happen in your case, Whenever you click on button, firstly it will call the page load event and it will re-bind your listbox source therefore your selected values will be lost.

Jitendra G2
  • 1,196
  • 7
  • 14
0

You can store the value in a hidden field and then repopulate the listbox after the page postback.

Debabrata
  • 124
  • 1
  • 10