【问题标题】:Getting path to image获取图片路径
【发布时间】:2012-06-05 10:26:42
【问题描述】:

大家!

我知道,这个问题可能被问了很多次,但无论如何..

问题是:

我有一个包含 2 个项目(C# 类库和 Web 项目)的解决方案。 在类库项目中,我重写了一个文本框控件,因此它具有内置验证。在本地服务器上一切正常,但是当我上传已发布的项目时,问题就开始了。

代码如下:

    [Browsable(true)]
    [Category("Appearance")]
    [DefaultValue("~/images/alert.png")]
    public string AlertImageUrl
    {
        get
        {
            string path = HttpContext.Current.Server.MapPath("~/images/alert.png");
            return GetPropertyValue<string>("AlertImageUrl", path);
        }
        set
        {
            SetPropertyValue("AlertImageUrl", value);
        }
    }

    protected override void Render(HtmlTextWriter writer)
    {
        this.Attributes["onfocus"] = "var objs = this.parentNode.childNodes; for (var i = 0; i < objs.length; i++) {if (objs[i].id == 'imgAlert') { objs[i].style.display = 'none'; } }";
        writer.WriteFullBeginTag("span");
        base.Render(writer);
        if (!_validator.IsValid)
        {
            writer.WriteBeginTag("img");
            writer.WriteAttribute("id", "imgAlert");
            writer.WriteAttribute("runat", "server");
            writer.WriteAttribute("style", "left: -20px; top: 3px;");
            writer.WriteAttribute("src", AlertImageUrl);
            writer.WriteEndTag("img");
        }
        writer.WriteEndTag("span");
    }

图片位于 App_Themes/Theme_name/images/alert.png 如何正确获取 AlertImageUrl?奇怪,如果我手动设置它会返回正确的路径,但是 WriteAttribute - no..

任何帮助表示赞赏, 问候,马里斯

【问题讨论】:

    标签: c# asp.net url path image


    【解决方案1】:

    使用ResolveClientUrl 方法代替Server.MapPath

    【讨论】:

    • 仔细阅读文档:msdn.microsoft.com/en-us/library/…A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code.
    猜你喜欢
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    • 2017-03-10
    相关资源
    最近更新 更多