Maintain Expanded Div visibility Across Postback

My panel features multiple collapsible filters that expand when a button is clicked. However, after each postback, the div collapses again. How can I ensure that the current state of the div remains consistent across postbacks?


<h4 class="contentFilterHeader">Start Date</h4>
<button type="button" class="btn" data-toggle="collapse" data-target="#startDate"></button>
<div id="startDate" class="collapse">
    <label for="from">Before:</label>
    <div class="form-group">
        <div class="date">
            <div class="input-group">
                <input type="text" class="form-control" id="startPicker" runat="server" name="date" />
                <span class="input-group-addon><span class="glyphicon glyphicon-calendar"></span></span>
            </div>
        </div>
    </div>

Answer №1

Utilize a HiddenField element to retain the status of the panel.

Answer №2

To dynamically show the #startDate div in your C# code-behind, you can add the attribute runat=server to it and then set its Class property after checking if it is a postback. Alternatively, you can also inject some JavaScript to open it when the page loads.

startDate.innerHtml = startDate.innerHtml + "<script> $(document).ready(function(){ $('#startDate').collapse('show') }) </script>";

This approach assumes you are utilizing C# and Bootstrap.

Additionally, you can explore this code example for further guidance: http://codepen.io/jammer99/pen/eZyqEN

var t,
  // Check any previously opened panel using cookie
  earlierPanel = readCookie("openpanel");
  
if (earlierPanel) {
  $('#accordion .panel-collapse').removeClass("in");
  $("#" + earlierPanel).addClass("in");
}

$('#accordion').on('shown.bs.collapse', function(w) {
  t = w.target;
  createCookie("openpanel", $(t).attr("id"));
})

$('#accordion').on('hide.bs.collapse', function(w) {  
  createCookie("openpanel","");
})

function createCookie(name, value) {
  document.cookie = name + "=" + value + "; path=/";
}

function readCookie(name) {
  var e = name + "=";
  var a = document.cookie.split(";");
  for (var d = 0; d < a.length; d++) {
    var f = a[d];
    while (f.charAt(0) == " ") {
      f = f.substring(1, f.length)
    }
    if (f.indexOf(e) == 0) {
      return f.substring(e.length, f.length)
    }
  }
  return null
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to emphasize a clicked hyperlink in AngularJS

Here's the HTML code I've been working on: <div id="Navigation" class="md-dialog-full"> <div class="products-options"> <a ng-click='addType("Physical")' href="javascript:void(0);"> < ...

Multiple sources are able to access and modify the data stored in one data.json file

I am aiming to utilize the data from a JSON file ("data.json") in both my Java application and node.js application. The Java application will read the JSON file, make any necessary changes, and rewrite the file with updated or new JSON objects. Similarly ...

Tablet-friendly dropdown menu

Seeking advice on implementing @media queries for tablets with a width of 991px. Currently, the CSS file is optimized for mobile devices but needs adjustments for tablet responsiveness in the dropdown menu. I attempted the following CSS code: @media (max ...

Why are the elements not found by their id when I check them, even though they were created dynamically using an ajax-generated form with php-inserted values through a

How can I prepopulate form fields displayed in a modal view using jQuery after retrieving the form HTML with an AJAX query? I am trying to set the values through a JavaScript function that retrieves PHP-generated values via document.getElementById. However ...

Wrapping text around an image using two distinct Angular components

Can text wrap around an image in Angular even if they are in separate components? Or do the text and image have to be within the same component for this to work, regardless of whether the image is on the left or right side? https://i.stack.imgur.com/hdlxD ...

Angular 8 project experiencing issues with Bootstrap dropdown functionality

I have successfully installed the packages listed below: npm install --save bootstrap@3 npm install --save popper.js angular-popper npm install jquery --save Afterwards, I included the styles and scripts in the angular.json file in the exact order as sho ...

React JS makes it simple to create user-friendly cards that are optimized

I have a collection of objects that include a name and description. The name is short and consists of only a few characters, while the description can vary in length. I am looking to showcase this data within Cards in my React project, and have tried using ...

Issue with strange behavior of CSS cursor with images

Is there something blatantly obvious that I'm missing here? My goal is to replace the cursor css property with a png, as shown in this example here I was able to get it working with the 'happy.png' demo, but for some reason it won't wo ...

Guide on placing the back arrow button in React Navigation

I recently started working with react-native and I have been experimenting with react-navigation. One issue that I am facing is the positioning of the back-arrow in the navigation tab. I want to target the back-arrow so that I can adjust its position. Her ...

What is the impact of sending a JavaScript request to a Rails method every 15 seconds in terms of efficiency?

I have a method that scans for notifications and initiates a js.erb in response. Here is the JavaScript code triggering this method: setInterval(function () { $.ajax({ url: "http://localhost:3000/checkNotification", type: "GET" }); }, 15000); ...

Conflict between multiple jQuery files

Currently, I am in the process of creating a test website for an upcoming project. However, I have encountered some issues with jQuery. This is a static HTML5 website that includes a social widget. The problem arises when I remove a particular jQuery lin ...

Increasing the identifier of duplicated input fields using jQuery

There is a specific section in my form that consists of a select box and three checkboxes, which needs to be duplicated on request. While I have successfully cloned the div and incremented its specific div, I am facing challenges in incrementing the checkb ...

Displaying numerous information panels on Google Maps by extracting data from MySQL

Help needed! I've been struggling with this code for a while now. I can show multiple markers on the map but can't figure out how to display info details in a pop up box when they are clicked. Currently, I'm just trying to make it say "Hey!" ...

Generate user-customized UI components from uploaded templates in real-time

Summary: Seeking a solution to dynamically generate UI pages using user-provided templates that can be utilized for both front-end and back-end development across various use cases. Ensuring the summary is at the top, I am uncertain if this question has b ...

What causes the oninput event to act differently in Angular as opposed to regular JavaScript?

As I delve into learning Angular with TypeScript, I've encountered some inconsistencies compared to JavaScript that are puzzling me. Take for example this function that works flawlessly with pure JavaScript, where it dynamically sets the min and max a ...

A flex container containing two divs each with a white-space:nowrap element set at 50% width

How can I create a parent div that contains two child divs, each with a width of 50% that adjusts based on content? The issue arises when one of the child divs has an h3 element with white-space:nowrap;, causing it to exceed the 50% width. I attempted to ...

What is the best way to locate all mesh faces that are being lit up by a SpotLight

I am working with a THREE.Mesh that consists of a THREE.BufferGeometry containing "position" and "normal" THREE.BufferAttributes. This mesh is being lit by a THREE.SpotLight (which is a cone-shaped light source). Is there a method to ...

The absence of req.body in the app reflects an undefined state

I'm encountering an issue with my app and I believe showing you my code is the best way to explain the problem: var Meetup = require('./models/meetup'); module.exports.create = function (req, res) { var meetup = new Meetup(req.body); c ...

Automatically submitting forms without having to refresh the page

I have been searching for answers online, but none of them seem to help me. Additionally, I am struggling to grasp the concept of Ajax. I need everything to happen on a single page without any refreshing until the entire form is submitted. <form id=" ...

Guide to displaying pages on the dashboard in a React application with the help of Material UI

I am currently facing a slight difficulty in managing my dashboard using material-UI along with other components. The workflow of the application includes opening the login form first and then navigating to the dashboard. My goal is to only change the righ ...