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

i have truoble deleting and cookie i am setting in jquery, i have and dynamic accordian with an asp repeater and i am using Jquery cookie to remember state on post backs.

But i when i click "back button" i want to clear that cookie. I try using C#, and Jquery but no matter what it's keeps the cookie. i the W3 schools and microsoft ways to remove cookies, but it's ignorede

I am not the best Jquery guy, so maybe someone can see what is going wrong.

Frontend .Net
<asp:Repeater ID="Repeater4" runat="server" DataSourceID="XmlDataSourceDriverApp">
               <ItemTemplate>
                   <div class="accordion_container">

                   <div data-node='<%#XPath ("@no")%>' class=" accordion_head">

                       <div style="text-align: left;">  <%#XPath ("@date")%>:  <%#XPath ("@day")%> </div>


                   </div>
                   <asp:Literal ID="litimg" runat="server" />

                   <div style="padding: 0px; height: 100% auto !important;" class="card card-body accordion_body">

                       <asp:Repeater ID="childRepeaterToday" runat="server" OnItemDataBound="childRepeaterToday_ItemDataBound" OnItemCommand="_postsRepeater_ItemCommand" EnableViewState="true" DataSource='<%# XPathSelect ("Tour") %>'>
                           <ItemTemplate>
                               <div class="accordion_container">
                               <div data-node='<%#XPath ("@no")%>' class='accordion_head' style="width: 100%; padding: 0px;">


                                           <asp:Label runat="server" ID="lblHead" Text='<%# XPath("TourHeaders/TourHeader") %>'></asp:Label>

                                           "margin-right: 10px; float: right">
                                               <asp:Label runat="server" ID="lblHeadChild" Text='<%# XPath("TourStatus") %>'></asp:Label>

                                               <asp:Label Visible="false" ID="lblIcon" runat="server" />
                                           



                               </div>

                                 <div class="accordion_body">
                               <table style="padding: 10px !important;" class="table table-responsive">
                                   <tr>
                                       <tr>
                                           <td>Turnr:

                                                <asp:Label runat="server" ID="lblTourID" Text='<%# XPath("TourID") %>'></asp:Label>
                                               <asp:HiddenField runat="server" ID="lblCustomerPhone" Value='<%# XPath("CustomerPhone") %>' />
                                               <asp:HiddenField runat="server" ID="lblRef" Value='<%# XPath("Reference") %>' />

                                               <asp:HiddenField  runat="server" ID="lblLineCount" Value='<%# XPath("LineCount") %>' />
                                           </td>
                                       </tr>

                                       <tr>
                                           <td>
                                               <asp:Label runat="server" ID="lblCustomerName" Text='<%# XPath("CustomerName") %>'></asp:Label>
                                           </td>
                                       </tr>



                                       <tr>
                                           <td>
                                               <%-- <asp:Label runat="server" ID="Label2" Text='<%# XPath("Remarks/Remark") %>'></asp:Label>--%>

                                               <%--<asp:Literal ID="litRemark" Text='<%# XPath("Remark") %>' runat="server" />--%>





                                               <asp:HiddenField Value='<%# XPath("NaviAdr") %>' ID="hdnAdr" runat="server" />

                                                 <asp:HiddenField Value='<%# XPath("Longitude") %>' ID="hdnLat" runat="server" />
                                                <asp:HiddenField Value='<%# XPath("Latitude") %>' ID="hdnLng" runat="server" />

                                               <%--  <asp:Repeater ID="rptRemark" runat="server" DataSource='<%# XPathSelect ("Remarks") %>'>
                                                   <ItemTemplate>


                                                         <asp:Literal ID="litRemark" Text='<%# XPath("Remark") %>' runat="server" />


                                                   </ItemTemplate>
                                               </asp:Repeater>--%>

                                           </td>
                                       </tr>

                                       <td>
                                           <div class="btn-group btn-group-justified">
                                               <asp:LinkButton UseSubmitBehavior="false" CommandArgument=' <%#XPath ("TourID")%>' CommandName="accept" class="btn btn-warning" Text="Tilbm" runat="server" />



                                               <asp:LinkButton Target="_blank" class="btn btn-success" CommandName="maps" Text="Navi" ID="lnkBtnMaps" runat="server"></asp:LinkButton>


                                               <asp:LinkButton CommandArgument=' <%#XPath ("TourID")%>' CommandName="info" class="btn btn-info" Text="Info" runat="server" />

                                           </div>
                                       </td>


                                   </tr>


                               </table>
                                     </div></div>
                           </ItemTemplate>

                       </asp:Repeater>
                       </div>
                 </div>
               </ItemTemplate>
           </asp:Repeater>


Jquery
$(document).ready(function() {
    if (document.cookie.indexOf('collapsed_Nodes') >= 0) {
        var getCollapsed = Cookies.get('collapsed_Nodes');
        var checkCollapsed = getCollapsed.split('|');
        var arrayLength = checkCollapsed.length;
        for (var i = 0; i < arrayLength; i++) {
            $('[data-node="#' + checkCollapsed[i] + '"]').next('.accordion_body').addClass('hide');
            $('[data-node="#' + checkCollapsed[i] + '"]').children('.plusminus').children('img').attr('src', './icon/plus.svg');
        }
    }

    var adjustCookie = function() {
        var tags = [];
        $('.accordion_container .hide').each(function() {
            var tag = $(this).prev('.accordion_head').data('node');
            tags.push(tag.replace('#', ''));
        });

        if (tags.length) {
            Cookies.set('collapsed_Nodes', tags.join('|'), {
                expires: 1,
                path: window.location.pathname
            });
        } else {
            Cookies.remove('collapsed_Nodes', {path: window.location.pathname});
        }
    }

    $('.accordion_head').click(function() {
        if ($(this).next('.accordion_body').is(':visible')) {
            $(this).next('.accordion_body').slideUp(300);
            $(this).next('.accordion_body').addClass('hide');
            $(this).children('.plusminus').children('img').attr('src', './icon/plus.svg');
        } else {
            $(this).next('.accordion_body').slideDown(300);
            $(this).next('.accordion_body').removeClass('hide');
            $(this).children('.plusminus').children('img').attr('src', './icon/minus.svg');
        }
        adjustCookie();
    });
});


C# Button click
protected void back1_Click(object sender, EventArgs e)
   {

       Response.Redirect("Menu.aspx?&Username=" + Request.QueryString["UserName"] + "&Webprefix=" + Request.QueryString["WebPrefix"] + "&ChfName=" + Request.QueryString["ChfName"] + "&adminemail=" + Request.QueryString["adminemail"] + "");


   }


What I have tried:

I try C#
if (Request.Cookies["collapsed_Nodes"] != null)
{
    Response.Cookies["collapsed_Nodes"].Expires = DateTime.Now.AddDays(-1);   
}


and in Jquery
Cookies.remove('collapsed_Nodes', {path: window.location.pathname});
I can not use session abandon because it redirects login.
Posted
Updated 5-Mar-19 7:24am
Comments
Richard Deeming 5-Mar-19 16:33pm    
Since you don't seem to be using the cookies on the server, you might do better to use local storage[^] or session storage[^] instead.

1 solution

First thing I would do is to make sure that all of your html renders out fine, and then use the browser's debug tools (eg Firebug) to check for errors when you are clicking the button.
Trying to go through your WebForms code I did see a couple of HTML errors and I stopped.
HTML
<div class="accordion_container">
  <div data-node='<%#XPath ("@no")%>' class='accordion_head' style="width: 100%; padding: 0px;">
    <asp:Label runat="server" ID="lblHead" Text='<%# XPath("TourHeaders/TourHeader") %>'></asp:Label>
    "margin-right: 10px; float: right">
    <asp:Label runat="server" ID="lblHeadChild" Text='<%# XPath("TourStatus") %>'></asp:Label>

And another
HTML
<div class="accordion_body">
  <table style="padding: 10px !important;" class="table table-responsive">
    <tr>
      <tr>
        <td>Turnr:
 
Share this answer
 
Comments
Martin Lauritsen 7-Mar-19 9:28am    
You dont have alle the code, but it's wiered you see errors my console is empty :) and visuel studio is not showing errors . But thanks :)
MadMyche 7-Mar-19 10:09am    
I didn't post all the code as I stopped when I hit the second HTML error which may not show up in VS. The first block I posted may not be an HTML error, but it is a mistake. The second block has two <TD> elements back to back, which is invalid HTML

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