简体   繁体   English

使用Visual Studio 2012的Asp.Net网站中未显示背景图像

[英]Background Image Not Appearing in Asp.Net Site Using Visual Studio 2012

I have two installations of Visual Studio, one is 2010 Ultimate, and the other is 2012 Ultimate. 我有两个Visual Studio安装,一个是2010 Ultimate,另一个是2012 Ultimate。 There are two pages in my site which display a background image associated with the page on PageLoad . 我的站点中有两个页面,它们显示与PageLoad上的页面关联的背景图像。 This is the code which pulls the image from the directory and displays it as the background image: 这是从目录中提取图像并将其显示为背景图像的代码:

public partial class TechCall : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    StringBuilder script = new StringBuilder();
    script.Append("<script type=\"text/javascript\">");
    script.Append("document.body.style.background = \"url('/Cyber7th/Images/CPUBack2nd.jpg')\";");
    script.Append("document.body.style.backgroundRepeat = 'no-repeat';");
    script.Append("</script>");

    this.ClientScript.RegisterClientScriptBlock(this.GetType(), "changeBackground", script.ToString());
}    
}

The other page has the same code, but just references a different image, and is this: 另一页具有相同的代码,但是只是引用了不同的图像,因此:

public partial class Build_It : System.Web.UI.Page

EDIT: Here is what the page should look like: 编辑:这是页面应显示的样子:

在此处输入图片说明

Now in Visual Studio 2010 this works just fine... when viewed in a browser (Firefox here), the background image shows up with no problem, and all the functionality is there. 现在,在Visual Studio 2010中,它可以正常工作...在浏览器(此处为Firefox)中查看时,背景图像显示没有问题,并且所有功能均已存在。

However, with the project in Visual Studio 2012, viewing the page using this same code and in the same browser for testing, I get this: 但是,对于Visual Studio 2012中的项目,使用相同的代码并在相同的浏览器中进行测试以查看页面,我得到以下信息:

在此处输入图片说明

Viewing the page in VS 2012 in the browser does not show the background image - there is a white space instead of the background image. 在浏览器中的VS 2012中查看页面不会显示背景图片-而不是背景图片有空白 Again, Firefox is being used, but the it happens no matter which one I use in 2012 (IE, Chrome, and Internal Browser). 同样,正在使用Firefox,但是无论我在2012年使用哪一种(IE,Chrome和内部浏览器),都会发生这种情况。 VS 2010 shows the background images in all browsers. VS 2010在所有浏览器中显示背景图像。 So that is the output that I should be seeing, which is the background images. 这就是我应该看到的输出,即背景图像。

Again, this has to be something with VS 2012, as I am not having this issue with 2010. Maybe, if I would have built this from ground up in VS 2012 instead of attempting to port the solution, perhaps I would not have this issue? 同样,这在VS 2012中一定有用,因为我在2010年没有这个问题。也许,如果我本来是在VS 2012中重新构建此问题,而不是尝试移植解决方案,也许我就不会遇到这个问题? Hopefully this helps to explain my issue a bit clearer. 希望这有助于更清楚地解释我的问题。

Try using this on Page_Load to dynamically change the image: 尝试在Page_Load上使用它来动态更改图像:

Image1.ImageUrl = "~/Images/SampleBackground.png";

Create a folder Images in your server and add your background image to it. 在服务器中创建一个文件夹Images并将背景图像添加到其中。 Then you can refer it with the above code. 然后,您可以使用上面的代码引用它。 This is not using javascript but plain c#. 这不是使用javascript,而是纯c#。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM