By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,749 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,749 IT Pros & Developers. It's quick & easy.

Background Image ?

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

"WJ" <JW***@Msn2.Com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

Nov 18 '05 #2

P: n/a
WJ
Thanks Pete,

That works very well!

John

"Pete Beech" <pe*********@hotmail.nojunk.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
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

"WJ" <JW***@Msn2.Com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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


Nov 18 '05 #3

P: n/a
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
Nov 18 '05 #4

P: n/a
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)

"WJ" <JW***@Msn2.Com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

Nov 18 '05 #5

P: n/a
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

"Pete Beech" <pe*********@hotmail.nojunk.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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)

"WJ" <JW***@Msn2.Com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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


Nov 18 '05 #6

P: n/a
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.

"WJ" <JW***@Msn2.Com> wrote in message
news:Op****************@TK2MSFTNGP09.phx.gbl...
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

"Pete Beech" <pe*********@hotmail.nojunk.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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)

"WJ" <JW***@Msn2.Com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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



Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.