jqueryasp.netmodalpopups

Jquery- fetch checkbox selected values and save it to cookies at modal close button click


Spent almost two days trying to figure out this problem. I have modal popup with dynamic checbox list (generated from database) and close button on modal popup. What I want is that when user click 'Close' button, then modal popup is closed but the same time I want to get all selected checboxes values to save in string.

<asp:Button ID="btnHide" runat="server" Text="Close" OnClick="btnHide_Click" OnClientClick="HideModalPopup()"/>

protected void btnHide_Click(object sender, EventArgs e)
        {
           string names=checkCheckboxes(); //calling WORKING function which reutrns all selected checboxes from modal popup in a string, splited by comma
           Response.Write(names); //trying to write 'names' variabile which contains all selected checboxes on screen
        }

jQuery function:

function HideModalPopup() {
    $find("mpe").hide();
    return false;
}

Problem is that when I click close button, modal popup disapeared, but no values are saved to variabile 'names' in btnHide_Click method. How can I get selected checkboxes from modal popup to this function? What is wrong with this code?

Please help me out.


Solution

  • You can do it via ajax call. In your clientside function where you hide modal box before that fetch all the checkbox checked value using .each() jquery n then call ajax request and save it to ur database.

    function HideModalPopup() { 
         var fetchAllChkbox= $("input[name=chkboxName]:ch ecked").map( function () {return this.value;}).get().join(",");
    
             $find("mpe").hide(); 
            // $.ajax  call to save to database etc
            return false;
         } 
    

    Note- herechkboxName is name of chkboxss,


    Updated Answer : Get all selected checkbox values in a variable and create cookie session and set selected value to it.

    JQuery:

    $(document).ready(function () {
       $("#clkme").click(function () {
            var getValue = ""; // get vall checkbox selected values
            $("[id*=CheckBoxList1] input:checked").each(function () {
            var Value = $(this).val();
            getValue += Value + ",";
            });
           $.cookie('your_cookie_name', getValue); // set cookies
           alert($.cookie('your_cookie_name')); // read cookies and alert it
        });
      });
    

    HTml markup:

    <asp:CheckBoxList ID="CheckBoxList1" runat="server">
    </asp:CheckBoxList>
      <div id="clkme">CLICK ME</div>
    

    Note: just import this cookie.js https://github.com/carhartl/jquery-cookie