【问题标题】:embedding images in asp.net Custom Control在 asp.net 自定义控件中嵌入图像
【发布时间】:2011-12-07 11:33:54
【问题描述】:

我正在创建一个自定义 Gridview 控件,根据要求我必须在 Top Pager 中放置两个图像按钮。我可以在首页显示图像按钮,但只有一个图像图像按钮显示图像,另一个图像按钮不显示。

代码如下:

 protected override void InitializePager(GridViewRow row,int columnSpan,PagedDataSource pagedDataSource)
        {
//adding  refresh button in TopPager
            ImageButton Refresh = new ImageButton();
            Refresh.ToolTip = "Refresh GridView";
            Refresh.ImageAlign = ImageAlign.Right;
            Refresh.AlternateText = "Refresh GridView";
            ClientScriptManager cs1 = this.Page.ClientScript;
            Refresh.ImageUrl = cs1.GetWebResourceUrl(typeof(myCustomGrid.CustomGrid), "myCustomGrid.Refresh.gif");

// adding button  in TopPager to add new record..
            ImageButton ExportToPDF = new ImageButton();
            ExportToPDF.ToolTip = "Export To PDF";
            ExportToPDF.AlternateText = "PDF Export";
            ClientScriptManager cs2 = this.Page.ClientScript;
            ExportToPDF.ImageUrl = cs2.GetWebResourceUrl(typeof(myCustomGrid.CustomGrid), "myCustomGrid.PDF.gif");
}

我已将两个图像属性构建操作设置为嵌入资源

在汇编信息中

[assembly: WebResource("myCustomGrid.Refresh.gif", "image/gif")]
[assembly: WebResource("myCustomGird.PDF.gif", "image/gif")]

我哪里出错了!非常感谢任何帮助。

【问题讨论】:

    标签: asp.net gridview dll custom-controls


    【解决方案1】:

    这些图像是否放置在项目的根目录中?因为,如果不是,您必须添加相对路径。而且我看不到组装的路径。 你必须这样做:

    [assembly: WebResource("{Your assembly full name}.{Path to image inside assembly and with replacement '/' to '.'}.{Image name}", "image/gif")]
    

    例如:

    [assembly: WebResource("MyBestProject.Controls.resources.images.myCustomGird.PDF.gif", "image/gif")]
    

    【讨论】:

    • 根文件夹中有两个图像“Refresh.gif”和“PDF.gif”,refresh.gif 图像显示正常,但 PDF.gif 不是..
    • 再次检查图像的路径和属性构建操作
    • 我使用了 '[assembly: WebResource("{dllname/namespace}.{image}", "image/gif")]',它适用于 refresh.gif 图像案例。检查图像的属性构建操作,它设置为嵌入资源,使用 GetWebResourceUrl 方法有什么问题。
    • 尝试使用 '[assembly: WebResource("{dllname}.{image}", "image/gif")]' 在我自己的项目中我使用: [assembly: WebResource("Workflow. Web.Controls.resources.images.clear.png", "image/png")] clear.png 放在 dllRoot/resources/images
    【解决方案2】:

    可能是笔误。

    你的意思是:-

    [assembly: WebResource("myCustomGrid.PDF.gif", "image/gif")]
    

    而不是……

    [assembly: WebResource("myCustomGird.PDF.gif", "image/gif")]
    

    【讨论】:

    • 是的,您指向的图像没有显示在 imagebutton 上。
    【解决方案3】:

    更新: 当 imagebuttons 分别呈现由 Refresh.gif 和 PDF.gif imagebuttons 生成的 HTML 时。PDF.gif 不加载而是我得到指定的替代文本,它在此代码行 ExportToPDF.AlternateText = "PDF Export";

     <input align="right" type="image" style="height:20px;width:20px;" alt="Refresh GridView" src="/WebResource.axd?d=CIOcfTt92OMP_aqepr-cGa2E4E8-uaiywsae5-4NcYocrJ-NE-cIL4Qb2JfX3tY0cIa8EwDzWnyeBqNiST34SNpBNuGjVC113XqMTprBN-XU9J7ilPsI6bsaXwHa-Qt30&amp;t=634589418951556073" title="Refresh GridView" name="ctl00$MainContent$customGrid0$ctl01$ctl02">
    
    
    
     <input type="image" style="height:20px;width:20px;margin-left:480px;" alt="PDF Export" src="/WebResource.axd?d=su3rsVMzKa7BNi4A-JZVUHPhHh3sS-oWW9bmAeK4I0h1A11NHqBD2setFyA3yundDC34YO0gbBoz5G0PHS0uxeQYI66QON_syQhIdlIKs4JzJW6H_oLErMjOZE6Q_slB0&amp;t=634589418951556073" title="Export To PDF" name="ctl00$MainContent$customGrid0$ctl01$ctl01">
    

    【讨论】:

      【解决方案4】:

      “仅”2 年后,我遇到了类似的问题。我只是通过查看这篇文章和我的问题之间的相似性得出的一个简单想法解决了我的问题。也许其他人将来会发现这个解决方案很有用。

      问题似乎出在名称的 ALLCAPS 部分。出于某种原因,UpperLower 名称和所有 lower 名称都可以正常工作。但我猜测 ALLCAPS 段会在内部转换为其他内容并且不起作用。就我而言,将我的图标从“ARROWLEFT.GIF”重命名为“ArrowLeft.gif”(文件名、注册和对资源的任何引用)解决了这个问题。

      附加说明:我在同一个项目中有另一个图像,名为“icon_Calendar.GIF”,在我将其重命名为“icon_Calendar.gif”之前它也无法正常工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-26
        • 1970-01-01
        • 2023-03-14
        相关资源
        最近更新 更多