ASP.Net不正确的背景图片呈现的风格 [英] ASP.Net incorrect background image style rendered

查看:130
本文介绍了ASP.Net不正确的背景图片呈现的风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ASP.Net,我有,我想补充的内联CSS样式背景图片:无的服务器控件。然而,当我打电话:

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

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

下面内嵌样式生成(并试图解析URL无):

The following inline style is generated (and tries to resolve the url "none"):

background-image:url(none)

有一个特殊的语法我可以使用背景图片设置为none内联?

Is there a special syntax I can use to set the background image to none inline?

推荐答案

纵观code为的HtmlTextWriter CssTextWriter 的HtmlTextWriter 自己。

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

二进制不是在风格枚举的第一个元素〜HtmlTextWriterStyle.BackgroundColor ,就是它使用任何风格的名字不承认,因此,不会刻意去检查,如果该值需要包裹在URL()时,它实际上写出来。

"Binary not the first element in the style enum", ~HtmlTextWriterStyle.BackgroundColor, is what it uses for any style whose name it doesn't recognize, and therefore doesn't bother to check if the value needs wrapped in "url()" when it's actually written out.

HtmlTextWriterEx 不是最伟大的名字,但不管。 (?)根据你在做什么,你可能需要做这样的事情在你的code-背后 System.Web.UI.Page 子类:

HtmlTextWriterEx isn't the greatest name, but whatever. Depending on what you're doing, you might(?) need to do something like this in your code-behind System.Web.UI.Page subclass:

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

和这里的类:

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);
    }
}

这篇关于ASP.Net不正确的背景图片呈现的风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆