Toggle button dilemma in asp.net: switching between mobile and desktop sites but having trouble with styles getting 'stuck'

Issue with Master Page Styles

I am facing an issue with the styles when switching master pages via a button event in asp.net 4.0. Even though the new master page is displayed, the CSS from the old master page seems to persist. The styles are defined within the head of the old master page, and I can see that the new master page should have a different set of styles based on the markup. Viewing the source also shows all the new CSS declarations in the head. How can I force a "refresh" or "reload" to apply the correct styles?

Implementation Details

I am working on implementing a mobile version for my asp.net site. When a mobile device is detected, a cookie is set to switch the master page to a mobile-friendly one in the preinit method. This functionality works correctly:

protected virtual void Page_PreInit(Object sender, EventArgs e)
{
    if (IsMobile)
        this.Page.MasterPageFile = "m-" + this.Page.MasterPageFile;
}

There is a button at the bottom of the page that allows users to toggle between the mobile and desktop view by changing the value in the cookie. Upon redirecting back to the page, the relevant master page is rendered based on the cookie value. However, even though the correct master page is displayed in the markup, the styles from the mobile version persist when switching to the desktop view. Redirecting was intended to solve this issue.

// Event handler for desktop/mobile site toggle button click
protected void viewMobileButton_Click(Object sender, EventArgs e)
{
    HttpCookie isMobileCookie = Cookies.snatchCookie("isMobile");

    if (bool.Parse(isMobileCookie.Value))
        Cookies.bakeCookie("isMobile", "false");
    else
        Cookies.bakeCookie("isMobile", "true");

    Response.Redirect(Request.RawUrl);
}

This is my first attempt at such implementation, and I am unsure if I am approaching it correctly, or how to troubleshoot further. Any assistance would be greatly appreciated.

Update

After some investigation, I discovered that the issue is related to the JQuery Mobile scripts. It appears that JQuery Mobile links pages together in a way that affects CSS registration. Disabling it allows the master page to switch correctly with the CSS included. I am currently exploring ways to disable JQuery Mobile before the redirect, although I am not sure how to achieve this yet.

Answer №1

The issue was identified to be related to the handling of JQuery Mobile AJAX for page transitions. After the initial request, JQuery Mobile does not reload the document head on subsequent page requests.

When switching from the mobile master to the desktop master, the document head failed to load, resulting in the styles not being applied. There are several ways to address this problem:

One solution is to disable AJAX completely, which resolves the issue but eliminates the benefits of using AJAX:

<form data-ajax="false">

Another method involves programmatically turning off AJAX, but it may not work with an event after JQuery Mobile initialization, thus preventing the benefits of AJAX:

$.mobile.ajaxEnabled = false;

While the above solutions may work if you redirect through a page first using an onclick event and an event handler, a more effective approach is to add "rel=external" to the link to specify it as an outgoing link to JQM:

<a href="myself.com?mobile=true" rel="external" >

In my case, I passed a query string parameter because I couldn't execute certain code to change the cookie. The solution involved checking the parameter on preinit, setting the cookie accordingly, and switching the master based on the cookie value during preinit.

Below is the complete solution I implemented for reference. Due to aliasing on my website, I had to read Request.RawUrl and parse it manually since Request.QueryString did not contain the values I passed:

(previously shown code snippet omitted for brevity)

To handle the situation, I used a dictionary object to evaluate and reconstruct the URL with the opposite mobile value to dynamically set the toggle link's href attribute.

(previously shown code snippet omitted for brevity)

During pageinit, I checked the query string first, then the cookie. If neither were present, I ran a mobile detection method to create a cookie and set an interface bool property for easy access to conditionals dependent on it.

(previously shown code snippet omitted for brevity)

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

What is the best way to post to the Facebook application instead of the mobile website version?

This is the Facebook share button I'm using on my mobile website version. <a class="fbs" href="#" onclick="window.open('https://www.facebook.com/sharer/sharer.php?u='+encodeURIComponent('http://website.com'),'facebook-shar ...

Implementing Session Variables in C# with AJAX

Can someone please assist me? I am trying to add a variable into the session state using AJAX, but it doesn't seem to be working. When I click the button, it redirects to Travellerinfo.aspx page. Here is my test.aspx: <%@ Page Language="C#" Auto ...

What could be the reason my Bootstrap webpage is not optimized for mobile devices?

My website is currently hosted at "nateshmbhat.github.io". I have implemented mdboostrp's row and col for the structure of my website, but unfortunately, it is not very mobile-friendly as it displays a lot of unnecessary background space. If you woul ...

Shuffle various div elements in a random pattern using jQuery

Check out this jQuery fiddle I'm using for my project: http://jsfiddle.net/BREvn/5/ Currently, I am creating a bird shooting game where the number of birds increases with each level. These birds are essentially represented by div elements, but I am ...

Is it more effective to consolidate similar styles into a single class and utilize it, or is it better to go the opposite route?

Which code snippet below is more efficient? First Example .a { color: #000; width: 20px; -webkit-transition: 0.3s all; -moz-transition: 0.3s all; -o-transition: 0.3s all; transition: 0.3s all; } .b { color: #333; width: 40px; -webkit-transition: 0.3s a ...

Creating a custom design for an asp.net dropdown list

Hey there, I have an ASP dropdown control that I'm trying to style with CSS. I've applied this class: select { padding: 1px; margin: 0; background: #f8f8f8; color: #888; border-style: ridge; outline: none; display: inline-block; ...

Utilizing Bootstrap to arrange table cells in a shifted manner

I'm new to utilizing bootstrap in web development. I've been exploring various examples of bootstrap templates that include tables, and I noticed that when I resize my browser window, the table adjusts accordingly. Now, I'm curious to know ...

Issues with transitioning Jquery mobile menu integration from mobile to desktop resolution

Having difficulties incorporating a jquery menu for mobile with the provided code (see below). The issue arises when activating the mobile menu and then resizing the browser window to desktop size, where the menu's class selector has a display:none at ...

Tips for setting a uniform width for all bars in apexcharts bar column width

When working with dynamic data, the length of the data should also be variable. For instance, if the data has a length of 24, the column width should be 35px; similarly, even if the data has a length of 2, the column width should still be 35px. Apexchart ...

The issue of the background image not expanding to cover the entire page when resized is problematic

Hello there, I'm currently working on making my website responsive and I've run into a problem with GIF images. No matter what I do, they just won't fill the entire page. When I try to make them smaller, it leaves white spaces at the top and ...

Troublesome Bootstrap Tooltip CSS not functioning properly with AJAX-loaded dynamic content

I'm experiencing an issue with bootstrap tooltips. The tooltips work in terms of functionality (showing on hover, positioning to the left, etc.), but they are not styled as expected. I have an AJAX request that fetches data and populates a table. Thi ...

"The Divine Chalice" trichotomy format employing flexbox styling

My goal is to create a three-column layout known as the "holy grail" using the new display: flex syntax, as outlined in this ALA article. The setup requirements are: A header and footer with three columns in between The outer columns have fixed widths T ...

What is the best way to add a vertical line to the left of the selected menu item?

I'm trying to add a vertical line to the left of the active menu item by giving it border-left: 4px and padding-left: 50px;. However, this causes the active link to no longer be positioned below all the other links. Can anyone suggest a different appr ...

Creating a bordered triangle using only CSS

Looking to create a message holder with a triangular tip bordered shape? I've successfully crafted the tip using two triangles: #triangle-border { width: 0px; height: 0px; border-style: solid; border-width: 0 100px 80px 100px; bor ...

Showing Different HTML content based on User Role in ASP.NET MVC

Is there a way to improve the method of showing/hiding controls based on the user's role, rather than using User.IsInRole() directly in a view? A response on this thread suggests that it goes against separation of concerns. What other approaches can b ...

Hide the text if there is not enough space to show it fully

Currently working on a website where quotes are featured within a specific section. I am seeking a solution for ensuring that if the text cannot be fully displayed within the div (leading to words being cut off), the entire div becomes invisible. I would ...

css - Issue with Margins Not Aligning Correctly Despite Using Inline-Block and Parent Element with a Fixed Width

I'm having trouble centering a pair of buttons inside a form. These buttons are enclosed within a <div> with display: inline-block, so the <div> is only as wide as the buttons themselves. The parent element to this <div> is a <for ...

Arranging Text and Images in HTML/CSS to Ensure Optimal Placement When the Window Size Changes

Hello fellow coders! I've recently started diving into the world of website development and I have a query regarding positioning elements and handling window resizing. Can anyone help me out? I'm trying to center an image, a message, and a passw ...

Is it feasible to position an iFrame in the bottom right corner of the browser using CSS?

I'm trying to align an iFrame to the bottom right corner of the browser window using CSS. Initially, the following code seems to do the trick. However, if the page containing the iFrame has a horizontal scroll bar, the iFrame does not adjust its vert ...

The issue with the CSS combination of :after and :hover:after across different HTML elements

Encountering an issue while attempting to create a rollover effect using CSS :after and :hover pseudo-elements. Check out the clock and facebook icons on the right side by visiting: See below for the CSS code: .icon { background: url('. ...