Pergunta

I have two installations of Visual Studio, one is 2010 Ultimate, and the other is 2012 Ultimate. There are two pages in my site which display a background image associated with the page on 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:

enter image description here

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.

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:

enter image description here

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. Again, Firefox is being used, but the it happens no matter which one I use in 2012 (IE, Chrome, and Internal Browser). VS 2010 shows the background images in all browsers. 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? Hopefully this helps to explain my issue a bit clearer.

Foi útil?

Solução

Try using this on Page_Load to dynamically change the image:

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

Create a folder Images in your server and add your background image to it. Then you can refer it with the above code. This is not using javascript but plain c#.

Hope this helps.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top