Background Image ?

W

WJ

Is there a way to specify background image for an Aspx page at runtime in c#
? I clicked on the Web form in the IDE (Vs.Net 2003) and it shows
"DOCUMENT", which is not accessible inside code behind c#. On the IDE, I can
set the "background" property to
http://localhost/myWeb/image/myBackGroundImage.gif, however, it would be
nice to do it at runtime also.

Thanks

John
 
P

Pete Beech

One simple way to do this is to declare a string property in the codebehind
Page class, and call it, say, BackgroundImage. Then, put this in your body
tag in the .aspx page:

<body background=<%=BackgroundImage%>>

HTH,
Pete Beech
 
W

WJ

Thanks Pete,

That works very well!

John

Pete Beech said:
One simple way to do this is to declare a string property in the codebehind
Page class, and call it, say, BackgroundImage. Then, put this in your body
tag in the .aspx page:

<body background=<%=BackgroundImage%>>

HTH,
Pete Beech

in
 
W

WJ

One problem with this technique is that now I cannot click on a button to
redirect to another page, it simply blink and remains on the same page,
whicj happens to be the homepage.

John
 
P

Pete Beech

Hi John,
I've used this technique loads of times, and never had an error like that.
And I can't reproduce the error this time. How are you doing the redirect?
Can you post your .aspx code, and maybe the button handler code? You could
also, as a test, try putting a label and a button on the form, and see if
you can set the labels text in the button handler.

BTW, the code I posted before should be:
<body background="<%=BackgroundImage%>">

Without the quotes round the attribute, the VS.NET design view doesn't work.

Cheers,
Pete Beech

(also, just in case you're using VS.NET 2002, make sure the automatic code
which fixes up the event handler hasn't been removed. That sometimes happens
with that version)
 
W

WJ

Pete,

Pete,

Thanks for your reply. Your 1st code works perfectly. I put it in the
background property as "<%=myBackGroundImg%>" at design time, cleared all my
own hardcoded images (background)

Here below is my code in "page1.aspx" and it is this simple, no more no
less.

//****************************************
public class myClass: System.Web.UI.Page
{
private string myPage2="";
public string myBackGroundImg="";

private void Page_Load(object sender,System.EventArgs e)
{
myPage2="http://localhost/myWeb/page2.aspx";
myBackGroundImg="http://localhost/myWeb/image/background.gif";
}

private void buttonPage2_Click(object sender,System.EventArgs e)
{
Response.Redirect(myPage2);
}
}

John
 
P

Pete Beech

Well, that should work OK, as long as the button Click event is firing OK -
try setting a breakpoint at the Response.Redirect line, or add a label
control to your page1, and set the text to something in the button handler,
to prove that its firing.

If it isn't, open the generated code region and check that the handler is
being set up.

I can't think of anything else, apart from the form tag not being set to
"runat='server' ", or the asp:button tag not being within the form tag.

Pete

PS One unrelated point: its probably better to use relative references, so
rather than having the full URL for the background image and the redirect
URL, you can just use "image/background.gif" and "page2.aspx". This makes
everything more portable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top