Issues with Css not being functional in ASP.NET using C#?

I am facing an issue with my CSS file in my web development project. Despite having three files - html, css, and javascript - my CSS is not working correctly. I'm unable to figure out the reason behind this issue. Below is a snippet of my aspx code:

<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server">
<form id="form" runat="server">
<div id="editor" contenteditable="true" runat="server"></div>
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</form>
</asp:Content>

Below is the content of StyleSheet.css file:

#editor {
width: 100px;
height: 500px;
background-color:#444;
color: white;
font-size: 14px;
font-family: monospace;
display: block;
}
.statement {
color: #00FF00;
} 

And here is the content of JScript.js file:

$(function () {
$("div[ID$='editor']").on("keydown keyup", function (e) {
    if (e.keyCode == 32) {
        var text = $(this).text().replace(/[\s]+/g, " ").trim();
        var word = text.split(" ");
        var newHTML = "";

        $.each(word, function (index, value) {
            switch (value.toUpperCase()) {
                case "SELECT":
                case "FROM":
                case "WHERE":
                case "LIKE":
                case "BETWEEN":
                case "NOT LIKE":
                case "FALSE":
                case "NULL":
                case "FROM":
                case "TRUE":
                case "NOT IN":
                    newHTML += "<span class='statement'>" + value + "&nbsp;</span>";
                    break;
                default:
                    newHTML += "<span class='other'>" + value + "&nbsp;</span>";
            }
        });

        $(this).html(newHTML);

        // Set cursor position to end of text
        var child = $(this).children();
        var range = document.createRange();
        var sel = window.getSelection();
        range.setStart(child[child.length - 1], 1);
        range.collapse(true);
        sel.removeAllRanges();
        sel.addRange(range);
        $(this)[0].focus();
    }
  });
 });

Lastly, in the Masterpage.master file, below are the links to StyleSheet.css and JScript.js:

<link rel="stylesheet" href="StyleSheet.css" type="text/css" />
<script type="text/javascript" src="JScript.js"></script> 

Despite all these configurations, the height, width, and background color of the div are not changing as expected.

Answer №1

To ensure consistency, consider setting the ClientIDMode property to static when running on the server-side. Another option is to assign a CssClass to the control for better organization.

Answer №2

By adding runat="server" to the div, the ID of the div will be changed according to the ContentPlaceHolder, causing #editor to not be found on the page.

To avoid this, you can either create a class in your css and assign it to the div, or set ClientIDMode="static" on the div to keep its id the same as you have set originally.

.editor {
width: 100px;
height: 500px;
background-color:#444;
color: white;
font-size: 14px;
font-family: monospace;
display: block;
}

<div id="editor" calss="editor" contenteditable="true" runat="server"></div>

Alternatively, you can add ClientIDMode="static" to the div.

<div id="editor" calss="editor" contenteditable="true" runat="server"  ClientIDMode="static"></div>

Answer №3

To implement the "clientidmode=Static" attribute in a div, you can use the following code:

#editor {
 width: 100px;
 height: 500px;
 background-color:#444;
 color: white;
 font-size: 14px;
 font-family: monospace;
 display: block;
}

<div id="editor" clientidmode="Static" runat="server"></div>

I have tried this code and it worked perfectly for me. Feel free to give it a go!

Answer №4

When not utilizing ClientIDMode and everything is in place, your div should function properly without any need for adjustments. Simply copy the CSS code provided below:

Your div:

<div id="editor" contenteditable="true" runat="server">Lorem Ipsum</div>

An additional rendered Div has been included at the end.

 div[id$='editor'] {
      width: 100px;
      height: 500px;
      background-color: #444;
      color: white;
      font-size: 14px;
      font-family: monospace;
      display: block;
}
<DIV id="ContentPlaceHolder1_editor" contentEditable="true">Lorem Ipsum</DIV>

We hope this information proves helpful to you.

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

Unable to designate the drop-down option as the default selection

Can anyone help me with setting a default drop-down value using JavaScript? I have been struggling to achieve this. You can find my code on jsFiddle <div ng-controller="csrClrt"> <div ng:repeat="(key, item) in items track by $index"> ...

The value of a Nullable object in Mongo's FindAsync method must not be null

In my C# and Mongo project, I am attempting the following: using (var cursor = await MongoCollection.FindAsync(query, findOptions)){ return await cursor.ToListAsync(); } Unexpectedly, it started throwing the error message "Nullable object must have a ...

Exploring the Use of Scrolling in jQuery

Hello everyone! I have a challenge I need help with. My goal is to display a div when a user scrolls past another div and hide it again when the user reaches the beginning of the latter div. Let's assume we have two divs identified by IDs #1 and #2. S ...

Is fs-extra the best tool for working with a .bundle file?

I am attempting to determine whether a specific folder in my project includes a .bundle file and, if it does, relocate it to another location. If the file is not found, I will use a default option instead. The problem I'm encountering is that I am una ...

Display a quote within an input field on a webpage using PHP

Here is a simplified version of the code I am trying to work with: <?php $var = 'Read "The Book"'; ?> <input type="text" value="<?php echo $var; ?>" /> The issue I am facing is that when displayed in the input field, it o ...

The WinForms application with a WebBrowser element functions properly only when running in debug mode

I've encountered a strange issue while developing a simple browser in Visual Studio C#. Everything works smoothly within the editor, but when I run the standalone build, my computer freezes and multiple "MyBrowser.exe" processes start running, unable ...

Generate new instance(s) of a C# class programmatically

I'm looking for a way to dynamically create instances of a class by using a dictionary to store objects and assigning names based on a counter. However, I'm having trouble accessing the properties of these objects once they are created. Here&apo ...

Tips for managing accordion events using a button press

I found a script online that I want to modify. Specifically, I am looking to create a button that will control the accordion event by closing the current div and opening the next one. <button> Click me to open next</button> inside each div in ...

What are some effective solutions for reducing large white gaps between images on a webpage using html/css?

Currently working on my portfolio and struggling with the spacing between logo images. The images are too spread out and I can't figure out how to reduce the space to 5px between each image. <html> <head> <title>Welcome to my portfol ...

What could be the reason for my div wrapper not showing up in the page preview?

I was expecting to see a centered div(wrapper) with a width of 940px and a black background-color when I previewed my page, but for some reason, there is no evidence of this wrapper even though there are no obvious errors in my HTML or CSS. My CSS is prope ...

Transmit intricate json data through a concealed variable and retrieve it in an mvc 3 controller

I am looking to achieve the following task. Encode a complex JSON object and store it in a hidden input variable. Retrieve the hidden variable using the form collection object. Transform the hidden text value into a dynamic object for seamless data retri ...

Sending React form data to Node Express using Axios

I'm encountering an issue that I can't quite figure out. I'm attempting to use axios to post data from React to Express. I followed a guide on connecting a React frontend to an Express server, which worked well for fetching data with a proxy ...

How can NHibernate listeners be utilized to streamline database modifications?

I am currently utilizing NHibernate 2.1 and have implemented listeners that handle IPreInsertEventListener, IPreUpdateEventListener, IPreDeleteEventListener. These listeners are designed to execute last-minute database operations for auditing purposes. Ho ...

Trouble with aligning vertically

The styles I have are working great, but for some reason Vertical-Align:middle and bottom aren't functioning properly. Everything inside the div is aligning to the top. .Progress { display: inline-block; z-index: 1000; width:auto; height: auto; b ...

Mastering VSCode IntelliSense: Unleashing the Power of Type Declarations

In my JavaScript projects, I aim to include TypeScript types sparingly to leverage IntelliSense for better code completions and receive warnings about type-related issues. To set up typechecking in JS, I created a jsconfig.json file and rely mostly on JSD ...

Regular expression for precise numerical values of a specific magnitude (any programming language)

I have been searching for a solution to what I thought was a common problem but haven't found one yet. What I need is a regular expression that will fail on a specific number of significant figures (a Max), but pass for fewer than the Max. It should ...

Styling with CSS to position a div tag on top of a webpage

I have a calendar code inside a div and I want to display the calendar when clicking an anchor tag without sliding the entire div. Currently, it is displayed as: However, I want it to be displayed as shown in the next image: <a class="aastext"&g ...

Tips for styling a button upon being clicked

Is there a CSS technique to make a button change color when clicked with the mouse? I am aware of using ':hover' for changing color, but I need something specific to a left mouse click. I want the same functionality as a standard button but with ...

Error: Unable to access 'amtSavingsInput' variable before it has been initialized

I encountered an issue with form3/chart3 while trying to replicate the success of form1/chart1 and form2/chart2. Despite following the same steps and structure, form3/chart3 seems to be malfunctioning. The error message I'm receiving is "Uncaught Refe ...

Sending parameters from one Node.js function to another in separate JavaScript files

I am currently working on passing function responses between two Node.js scripts. Here is my approach. Index.js var express = require('express'); require('./meter'); var app = express(); app.get('/',function(req,res){ ...