本文介绍了来自usercontrol的图像不会显示在网络表单上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将此代码添加到了我的ascx文件 http://jsfiddle.net/xkRcN/8/ ..当是aspx文件时..image(现在在我的项目中现在是"Images"文件夹..之前是flickr)将显示正常..但是当我将该页面放入用户控件并将其添加到我的Webform中时,图片消失了..我尝试过多次更改路径,但无法正常工作..用户控件位于我的页面中名为"usercontrols"的文件夹中..image图像中的"images"名为文件夹中,而我的aspx页面位于根目录下仅级别..so我更改了usercontrol的css中的图像路径,如下所示:-

I added this code in my ascx file http://jsfiddle.net/xkRcN/8/ ..When it was aspx file..the image(location is "Images" folder in my project now..was flickr earlier) would display alright..but when I made this page into a user control and added it in my webform, the image's vanished..I have tried changing the path many times but its not working..The user control is inside of "usercontrols" named folder..image inside of "images" named folder in my page and my aspx page's at root level only..so I changed the image path in the css of usercontrol as follows:-

background-image: url(../images/myImage.jpg);

甚至尝试过

background-image: url(../../images/myImage.jpg);

还有这个

background-image: url(~/images/myImage.jpg);

但Sprite图片不会显示

but the Sprite image just won't display

用户控件中的其他所有内容都显示良好..只是该图像丢失了..怎么了?

Everything else in the usercontrol is getting displayed well ..just that image's missing..whats wrong?

推荐答案

正如@Michael建议的那样,首先测试渲染的html(在浏览器中查看源代码)是否更改了您要设置样式的任何id.当您使用用户控件和母版页时,asp.net控件的ID将会更改

As @Michael suggested first test if the rendered html (view source code in your browser) has changed any of the ids that you are trying to style. The asp.net controls will have their ids changed when you are using usercontrols and masterpages

还可以通过放置该图像的完整路径来检查(在您的浏览器中)该图像是否存在.因此,如果您知道图片位于 http://example.com/images/myImage.jpg 和您的页面(是否使用用户控件无关) http://example.com/pages/page1.aspx ,那么您知道应该这样做:

Also check (in your browser) if he image does exists by putting the full path of that image. So if you know the image is in http://example.com/images/myImage.jpg and your page (doesn't matter if you use usercontrols) http://example.com/pages/page1.aspx then you know you should do this:

background-image: url(../images/myImage.jpg);

如果没有任何效果,则只需执行以下操作:

If nothing works, then just do this:

background-image: url(http://example.com/images/myImage.jpg);

祝你好运!

修改

由于您发现ID更改是导致问题的原因,所以唯一的解决方案是使用类来设置html标记的样式:

Since you found out the id change is what caused the problem, the only solution is to use classes to style your html markups:

html

//some people don't know this but you can also put the style directly into the
//asp.net control event if visual studio doesn't support it in the intellisense
<asp:Label id="label1" CssClass="test" style="color:blue;" runat="server" />

css

.test { color:red; }

如果您仍然想使用id来设置代码样式,则可以将生成的长id放入CSS代码中,也可以升级到可控制asp.net id的asp.net 4,例如:

If you still want to use the id to style your code, then you can either put the long generated id into your css code or upgrade to asp.net 4 which gives control over your asp.net ids, for example:

<asp:Label id="label1" ClientIDMode="Static" runat="server" />

有第三方解决方案可让您控制asp.net 3.5及更低版本的ID,但它们并不是开箱即用的,需要一些摆弄.

There are 3rd party solutions to give your control over the ids for asp.net 3.5 and below but they are not out of the box and needs some fiddling.

这篇关于来自usercontrol的图像不会显示在网络表单上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 04:24
查看更多