Is there a way to dynamically update CSS based on user input in ASP Razor?

Hey there, I want my users to be able to change the theme on the index page. Currently, I have implemented a code that works with a form select and submit button, but it's not very user-friendly. I would like it so that when a user selects option x, the corresponding CSS files for x are loaded, and if they choose option y, the styles for y are loaded without needing to click on a submit button. The current code is in the razor layout.csthml file.

 var totalMessage = "";

    if (IsPost)
    {
        var num = Request["number"];

        if (num.AsInt() == 1)
        {
            totalMessage = "dark mode";


            @Styles.Render("~/Styles/defaultdark.css")
            @Styles.Render("~/Styles/regaldark.css")
            @Styles.Render("~/Styles/globaldark.css")




        }
        if (num.AsInt() == 2)
        {
            totalMessage = "white mode";
            @Styles.Render("~/Styles/1/defaultwhite.css")
            @Styles.Render("~/Styles/1/regalwhite.css")
            @Styles.Render("~/Styles/1/globalwhite.css")


        }

The HTML form code I am currently using:

    <form action="" method="post>
        <p>
            <select name="number">
                <option value="">Dear user, please select your favorite theme</option>
                <option value="1">>Dark Mode</option>
                <option value="2">>White Mode</option>

            </select>

        </p>

        <p><input type="submit" value=" Add "></p>
    </form>
    <p>@totalMessage</p>

Everything is functioning as intended, but I am looking to make the user experience more streamlined. Ideally, I would like the code to automatically apply the selected theme without the need for the user to click on a submit button.

Answer №1

If you're looking to enhance the functionality, consider implementing an onchange event on the select element

    <form action="" method="post">
        <p>
            <select name="number" onchange="themeSwitch()">
                <option value="">Hello there! Please choose your preferred theme</option>
                <option value="1">Dark mode</option>
                <option value="2">Light mode</option>

            </select>

        </p>

        <p><input type="submit" id="submit" value="Apply"></p>
    </form>
    <p>@totalMessage</p>

    <script>
        function themeSwitch() {
            document.getElementById("submit").click()
        }
    </script>

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

Issues with CSS3 height transitions in a specific scenario

Check out this simplified version of my code on CodePen: http://codepen.io/anon/pen/pjZXob I am attempting to create a smooth transition in the height of the .destinationsTopicContent div, from 0 to auto. However, it is nested within a div that has visibi ...

Place the logo in the top right corner of the div

Why isn't my logo showing up in the top right corner of my div? .service-title { width: 100%; font-size: 24px; line-height: 1.1em; padding: 1em; background-position: right top; background: orange; background-image: url('http://ve ...

Create a layout of div elements aligned to the right, utilizing a parent container div that automatically adjusts its size to fit the

My attempt at this design: https://jsfiddle.net/j75hxxa2/1/ The goal is to have the block positioned on the right side and eliminate the extra gray area. By applying float: right; To the parent element, the child elements become very small. Trying ...

What is the best way to define coordinates or set margins for the autoTable component in jsPdf when using React js?

While working with the jsPdf library to create a PDF in React, I am encountering an issue where I am unable to set margins for the autoTable when there are multiple tables on a single page. Below is the code snippet: import React from "react"; ...

Issue with jQuery where it returns "undefined" when trying to access the CSS display property

On my page, there are several divs with default display styles set in an external .css file. When a certain condition is met, I use jQuery to turn one on and the others off using the fadeIn() and fadeOut() methods. However, I've noticed that in Firef ...

"The Vuetify calendar widget is cutting off some dates at the end of the month

Currently, I am using the vuetify date picker in my project. The API that I am interfacing with will showcase data within a specific date range. I have utilized the date picker component along with the range prop to achieve this functionality successfully. ...

What steps should I take to resolve problems with Font Awesome icons?

I got the fontawesome-free-5.8.2-web package from fontawesome.com and added the css and webfonts files to my project root. I included it in index.html, but when I try to use the icons, all I see is an empty square. What should I do? Thank you Here is the ...

Combining asp.net mvc and asp.net WebForm for seamless integration

I am currently facing a challenge with having two applications running simultaneously. One is built on ASP.NET MVC and the other on ASP.NET WebForm. I am seeking advice on possible ways to integrate these two applications together. Any suggestions, ideas f ...

Quick way to include id="" and class="" attributes to HTML elements using Sublime Text 2

Is there a way to configure Sublime Text 2 where inputting a . (dot) automatically generates class=" " and adding a # (hash) creates id=" " while typing an HTML opening tag? ...

Update the foreign key reference to point towards a different table

After searching online, I have found the explanations to be quite complicated, involving dropping and adding constraints. In my Transaction table, I currently have a foreign key that references the Users table using the primary key UserName. This is used ...

Is there a Pug file that can connect to a Websocket and show the incoming values?

Check out this basic pug file snippet: p Hello The back end consists of typical components such as node, express, passport, and sql. You can set up a route like this: app.get('/example', (request, response) => { response.render('exam ...

Is it achievable to modify the appearance of the "Saved Password" style on Firefox?

After creating a login page that initially had a certain aesthetic, I encountered an issue when saving login data in Firefox. The saved login page took on a yellow-ish tint that was not present in my original design: https://i.sstatic.net/LURG3.png I am ...

Using jQuery with ASP.NET DropdownList allows for dynamic manipulation of dropdown lists

Currently, I have multiple dropdown lists and I am looking to change the event handler for each one. Specifically, when a particular item is selected from a dropdown list, I want the event to be triggered only for that specific dropdown list. Below are two ...

The color of the links in the Bootstrap navbar refuses to budge

I'm having trouble changing the color of my navbar links to white and spacing them out evenly. I've written some code to do this, but for some reason, it keeps getting overridden. The background of the navbar is an image, and when I remove the im ...

Is it possible to implement a custom icon for pagination in Material UI?

Can I use a custom icon for pagination in material ui? Find more information on Material UI Pagination in the official documentation here, and check out the CodeSandbox Demo here I am interested in changing the default left and right chevron icons for na ...

Leveraging the css child selector within Ruby's Selenium-Webdriver to locate multiple elements

I prefer to minimize the usage of XPath in webdriver when locating elements, but still want the ability to access child elements from previously identified ones. For example: Consider the following HTML structure: <div id="myelement"> <table ...

How come my Bootstrap container columns are not remaining evenly divided into four parts?

Struggling to split a section of my project into 4 equal parts using the bootstrap col attribute. Unfortunately, every time I try, the last container ends up breaking and shifting below the other 3, creating an unsightly code layout. index.html <sec ...

Best Practices for Properly Disposing of an SQL Connection in a Web API

I have designed a straightforward Web API controller: public class CarsController : ApiController { public SqlConnection Db { get; set; } public CarsController() { Db = new SqlConnection("XXX"); } public ...

RadButton does not function with a single click when used within a RadGrid EditItemTemplate

Within the RadGrid, there is a nested structure consisting of a RadComboBox and an asp Button in the EditItemTemplate. Here is the current code snippet: <telerik:GridTemplateColumn UniqueName="AccountCode" HeaderText="Account Code"> <ItemTemp ...

Troubleshooting IE's alignment issue on HTML pages without DTD validation

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <body> <div id="container"> <div id="menu"> <ul> <li><a > ...