如何嵌入在Web服务器控件的静态图像资源? [英] How to embed a static image resource in a web server control?
问题描述
我要创建一个Web服务器控件重新presenting一个TreeView。所以,我想用2图像,+和 - 的展开/折叠。我怎么能建设成当页面上呈现,可以用作图像源的方式控制这一点?
I am going to create a web server control representing a treeview. So I want to use 2 images for + and - for expand/collapse. How can I build this into the control in a way that can be used as image source when rendered on the page?
由于这将是在编译的Web控件库,我不想依赖于Web应用程序外部图像。
Since this will be in a compiled web controls library, I don't want to rely on external images in the web application.
编辑:结果
根据<一个href=\"http://stackoverflow.com/questions/2265004/how-to-embed-a-static-image-resource-in-a-web-server-control/2265071#2265071\">this通过回答 安德烈·克雷默我做了以下内容:
Based on this answer by Andre Kraemer I did the following:
在AssemblyInfo.vb的:
In AssemblyInfo.vb:
<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.Resources.plus.gif", "image/gif")>
<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.Resources.minus.gif", "image/gif")>
在我的 RenderContents
覆盖:
Dim lPlusImage As New WebControls.Image()
Dim lMinusImage As New WebControls.Image()
lPlusImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyCompany.MyWebControls.Resources.plus.gif")
lMinusImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyCompany.MyWebControls.Resources.minus.gif")
lPlusImage.RenderControl(output)
lMinusImage.RenderControl(output)
我的大会名称为 MyWebControls
。
我的根命名空间是 MyCompany.MyWebControls
。
图像 plus.gif
和 minus.gif
位于一个文件夹名为资源
,图像有生成操作设置为嵌入的资源
The images plus.gif
and minus.gif
are located in a folder named Resources
, and the images have Build Action set to Embedded Resource.
它仍然无法正常工作。我没有得到任何错误。我已经直接在浏览器尝试生成的图像的URL,BOT什么也没有发生,只是一个空白页。
It still does not work. I get no errors. I have tried the generated image url directly in the browser, bot nothing happens, just a blank page.
的 注意:的结果
我试图在资源名称中使用无效的路径,结果是完全一样的,这让我想知道如果我需要做一些特别的东西来映射实际资源的资源名称。我有一个404 Not Found错误只有当我在code比在程序集信息指定使用不同的名称,它有无关路径指向一个实际的资源!
Note:
I tried to use an invalid path in the resource name, and the result was exactly the same, which made me wonder if I need to do something special to map the actual resource to the resource name. I got a 404 Not Found error only if I used different name in the code than what was specified in AssemblyInfo, it had nothing to do with path was pointing to an actual resource!
编辑:
我发现,它是C#和VB之间的差。看到了自己的<一个href=\"http://stackoverflow.com/questions/2265004/how-to-embed-a-static-image-resource-in-a-web-server-control/2424317#2424317\">answer到了这个问题。
I found out that it is a difference between C# and VB. See my own answer to this question.
推荐答案
两个图像添加到您的TreeView控件项目的子文件夹名为图像。然后在属性网格从内容改变他们的生成操作嵌入的资源。
add the two images to a subfolder called images of your treeview control project. Then change their build action in the property grid from content to embedded resource.
除此之外,你必须注册这两个图像作为嵌入资源程序集AssemblyInfo.cs文件中是这样的:
In addition to that, you have to register those two images as embedded resources for your assembly in the assemblyinfo.cs file like this:
[assembly: System.Web.UI.WebResource("YourProjectsNameSpace.Images.plus.gif", "img/gif")]
[assembly: System.Web.UI.WebResource("YourProjectsNameSpace.Images.minus.gif", "img/gif")]
由于这些图像将被嵌入到你的控件大会现在,你可以使用ClientScriptManager这样的访问它们:
As those images will be embedded into your controls assembly now, you can access them using the ClientScriptManager like this:
string plusImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "YourProjectsNameSpace.Images.plus.gif");
string minusImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "YourProjectsNameSpace.Images.minus.gif");
这篇关于如何嵌入在Web服务器控件的静态图像资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!