Invalid background style in ASP.Net

Using ASP.Net, I have a server control for which I would like to add the "background-image: none" inline CSS style. However, when I call:

writer.AddStyleAttribute("background-image", "none"); 

The following inline style is created (and tries to resolve the URL to "none"):

 background-image:url(none) 

Is there any special syntax that I can use to set the background image to non-line?

+4
source share
2 answers

Looking at the code for the HTMLTextWriter and CssTextWriter in .NET Reflector, the only thing I can think of is subclassing HTMLTextWriter own.

"Binary is not the first element in an enumeration of styles," ~HtmlTextWriterStyle.BackgroundColor , is what it uses for any style whose name it does not recognize, and therefore does not bother checking if the value needs "url ()" when he is actually discharged.

HtmlTextWriterEx not the biggest name, but anything. Depending on what you are doing, you may (?) Do something similar in your subclass of System.Web.UI.Page :

 protected override HtmlTextWriter CreateHtmlTextWriter(TextWriter writer) { return new HtmlTextWriterEx(writer); } 

And here is the class:

 class HtmlTextWriterEx : HtmlTextWriter { public HtmlTextWriterEx(TextWriter writer) : this(writer, "\t") { } public HtmlTextWriterEx(TextWriter writer, string tabString) : base(writer, tabString) { } public override void AddStyleAttribute(string name, string value) { if (name.ToLower() == "background-image" && value.ToLower() == "none") base.AddStyleAttribute(name, value, ~HtmlTextWriterStyle.BackgroundColor); else base.AddStyleAttribute(name, value); } public override void AddStyleAttribute(HtmlTextWriterStyle key, string value) { if(key == HtmlTextWriterStyle.BackgroundImage && value.ToLower() == "none") base.AddStyleAttribute("background-image", value, ~HtmlTextWriterStyle.BackgroundColor); else base.AddStyleAttribute(key, value); } } 
+1
source

You can try adding a CSS class to your page, for example

 .noimage { background-image: none; } 

Then, instead of adding the style attribute to your code behind, you can add a CssClass.

+1
source

Source: https://habr.com/ru/post/1305319/


All Articles