Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted

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:

Never trouble another for what you can do for yourself.
Posted
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

Posted

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.

Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...