Updating the CSS link href in ASP.NET using code behind

I'm struggling with changing the CSS href in the code-behind of my .ASPX page. I've tried various methods but haven't found a solution that works as intended.

HTML Markup:

<link id="linkCSS" runat="server" href='/css/1.css' rel="stylesheet" type="text/css" />

This is what I have attempted in Code behind:

If (mobile)
{
    HtmlLink link = (HtmlLink)this.FindControl(linkCSS.UniqueID);
    link.Href = "/css/2.css";
}

Unfortunately, I am encountering the following exception:

Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlGenericControl' to type 'System.Web.UI.HtmlControls.HtmlLink'.

Any suggestions or ideas would be greatly appreciated. Thank you.

Answer №1

Instead of searching for css, you can easily modify it like this.

If (mobile)
{
  linkCSS.Attributes["href"] = "~/css/2.css";
}

I came across This Helpful Resource.

Answer №2

To achieve more control over a control that cannot be directly accessed and modified, you can utilize a placeholder. Here's an example:

<asp:PlaceHolder ID="headPlaceHolder" runat="server" />

Then in your server-side code:

HtmlLink link = new HtmlLink();
link.Href = "/css/2.css";
headPlaceHolder.Controls.Add(link);

Give it a try to see if it solves your issue :)

Answer №3

Implement this script in your backend system

HtmlGenericControl link = this.FindControl(linkCSS.UniqueID) as HtmlGenericControl;

link.Attributes["href"] = "stylesheet/2.css";

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

When using multiple select tags with *ngFor in Angular, modifying the value of the first select tag will have an

<table id="DataTable" border="1" ALIGN="center"> <tr ALIGN="center"> <th>name</th> <th>address</th> <th>number</th> <th>type</th> </tr> <tr class="tcat" *ngFor ...

What is the best way to add a dropdown menu in front of a button?

I have a div containing a dropdown list of items and I want the user to be able to add more dropdown lists by clicking a button. Although I managed to do this, the issue I'm encountering is that the new list gets added after the button instead of ben ...

What is the method to extract the content located within the <body> element using requests.get()?

I attempted to utilize: from json import loads from requests import get text_inside_body_tag = loads(get('https://Clicker-leaderboard.ge1g.repl.co').content) However, it keeps throwing an error related to loads expecting a bytes object or when ...

Tips for preventing an AJAX response from populating a select dropdown

https://i.sstatic.net/gA1VE.gif In the example above, there is a placeholder (an option without value) that says something like Select Subcategory. When I change the category value, the subcategories are displayed. But what I want is for the subcategory ...

Protecting Asp.net Web Form Application

Seeking advice on enhancing security for an ASP.NET application. Specifically interested in ways to safeguard sensitive information like database connection details stored in the web.config file. Are there methods, such as cryptography or other features, ...

Remove a div element with Javascript when a button is clicked

I am working on a project where I need to dynamically add and remove divs from a webpage. These divs contain inner divs, and while the functionality to add new divs is working fine for me, I'm facing some issues with removing them. The code snippet b ...

Changing the text link color on IOS can be achieved by modifying the CSS properties

Recently, I encountered an issue with the text link color of my website. While I had set the link color to red for desktop view, it inexplicably changed to blue when viewed on an iPhone. Even after trying to use !important, the problem persisted. Can som ...

Issue with transition delays on specific elements within a containing element

I'm currently developing an intro animation for a website, where the children are supposed to slide up from the bottom (margin-top: 80px). The container is set with a fixed height and overflow:hidden to create a smooth appearance. Each child has a tr ...

How to adjust the banner image placement in Squarespace

I need the banner image to shift position on smaller screens, specifically those with a max-width of 414px. I want the focus to be on showcasing a specific product (a building) located on the right side of the image. This building should only appear on wid ...

Tips for overlaying text onto a MaterialUI icon

https://i.stack.imgur.com/cFr5Y.pngI am currently working on a project that requires me to overlay a number on top of an icon. Specifically, I am utilizing the MaterialUI ModeComment icon and attempting to display text over it. Despite trying the following ...

Converting Text into HTML using Node.js

Currently, I am utilizing nodemailer to send emails from my node server. The content for these emails is fetched from a MSSQL SQL server and is formatted in plain text format, including newline characters. However, when sending the email using nodemailer, ...

How can I properly parse the date format "Mon Oct 07 00:00:00 EDT 2013" using C#?

Currently, I am encountering an issue when trying to convert a json message from a server into C# objects using the RestSharp Deserializer. The problem lies in one field that is not properly converting into a datetime: The string value of the problematic ...

Troubleshooting padding problem in mobile gallery view with Bootstrap

Having a problem with the gallery layout in Bootstrap. It looks great on desktop view, but on mobile, there's no space between images on the same row. On mobile, the images stack vertically without any spacing between them within a row. However, ther ...

SwipeJS is not compatible with a JQuery-Mobile web application

I am currently attempting to integrate SwipeJS (www.swipejs.com) into my JQuery-Mobile website. <script src="bin/js/swipe.js"></script> <style> /* Swipe 2 required styles */ .swipe { overflow: hidden; ...

What can be done to eliminate the narrow gap between my <input> and <span> components?

Here is what I have: <input style="width: 20px" data-ng-model="row.number" type="text" /> <span style="width: 20px">-</span> Is there a way to eliminate the small gap between the <input> and <span>, as well as centering the ...

Customizing the background color in TurnJSIncorporating

Recently, I encountered a problem that has left me puzzled. Despite searching online for help, I have not been able to find a solution. The issue lies with the basic code provided on the official page of the TurnJS script. <div id="flipbook"> <di ...

The Transform feature doesn't seem to be functioning as expected within the Bootstrap

I was experimenting with creating a simple transform transition animation using css and js, so I wrote the following code: (test page) <!DOCTYPE html> <html> <head> <style type="text/css"> *{ ...

Utilizing jQuery to incorporate a radio input function in a POST request

When the Index.php page button is pressed, data is sent to the Resultx.php script page, which then responds with an asynchronous call back on the same Index.php page. index.php <script> $(document).ready(function() { $("#input_form").subm ...

Implementing Object Value Assignment to an Element Using jQuery

I recently received a set of data via an API request: https://i.stack.imgur.com/YGe2D.jpg In order to display this data as selectable options, I included the following code snippet in my AJAX script: $.each($(value.routes), function(index, route){ $ ...

Is there a way to convert the structure of HTML/CSS into JSON format?

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> .collapsible { background-color: #004c97; color: white; cursor: pointer; padding: 18px; width: 100%; border: none; text ...