2 Quick Questions on forms and printing

kkonkle

Newcomer
Joined
Feb 9, 2004
Messages
20
Location
Ann Arbor, MI
I'm fairly new to ASP.Net 2.0 and I have a couple (probably simple) questions.

I did some searching on this forum, and the tutorials and code library sections too, but didn't find what I needed.

Searching google just turned up a lot of sites about how to make a printer friendly version of your page, but not how to actually print.

1) How do I add a button to my page to print my aspx page? I don't need to pretty up the page, or move any controls or anything, I just want to pop up the print dialog and let the user print the page.

Looks like the most common thing to do is "window.print()", but Visual Studio doesn't like that. I have an "ASP:Button" control and in it's "OnClick" property I tried adding window.print(), but it says "window" is not a member. I also tried adding some javascript to my aspx page, then calling the function I defined in the "OnClick" property, but VS still didn't like it.

Is there some simple way to just print the form from a button click?

2) I have, in my aspx page, the tag: <body style="background-image:url('renew_bkgrd2.jpg')">

Is there some way for me to change this in the code behind page before the page is loaded so I can slip a different jpg in there?
 
OnClientClick property

Looks like the most common thing to do is "window.print()", but Visual Studio doesn't like that. I have an "ASP:Button" control and in it's "OnClick" property I tried adding window.print(), but it says "window" is not a member. I also tried adding some javascript to my aspx page, then calling the function I defined in the "OnClick" property, but VS still didn't like it.

You need to use the OnClientClick property instead of OnClick - OnClick specifies a server-side ASP.Net method, and OnClientClick specifies a client-side function such as Javascript.

Good luck :cool:
 
2) I have, in my aspx page, the tag: <body style="background-image:url('renew_bkgrd2.jpg')">

Is there some way for me to change this in the code behind page before the page is loaded so I can slip a different jpg in there?

A couple of ways spring to mind.

1. Change the line to <body style="background-image:url('<%=myImage%>')">

Then in your code behind

Public myImage as String

Private Sub Page_Load etc
myImage = "renew_bkgrd2.jpg"
End Sub

A better way it to use a literal control. Put an ASP.NET Literal control in place of that line, then in your code behind you just need

Literal1.Text = "<body style=""background-image:url('renew_bkgrd2.jpg')"">"

Hope that helps
 
LiteralControl not suitable for <body> tag

A better way it to use a literal control. Put an ASP.NET Literal control in place of that line

Usually this would be better, but I would strongly advise against using a LiteralControl to insert the opening <body> tag, if only for the reason that VS is going to complain an awful lot if you don't have one.

I would stick with the first option, or some other method of dynamically generating a style attribute.
 
Back
Top