How to Execute a client side script from code behind??

burak

Centurion
Joined
Jun 17, 2003
Messages
127
Hello,

Here is the client script I want to execute. It
basically serves to make a page "printer friendly".

function PrintThisPage()
{
var
sOption="toolbar=yes,location=no,directories=yes,menubar=yes,";

sOption+="scrollbars=yes,width=750,height=600,left=100,top=25";

var sWinHTML =
document.getElementById('contentstart').innerHTML;

var winprint=window.open("","_top",sOption);
winprint.document.open();
winprint.document.write('<html><LINK
href=/eggheadcafe.css rel=Stylesheet><body>');
winprint.document.write(sWinHTML);
winprint.document.write('</body></html>');
winprint.document.close();
winprint.focus();
}

I created an image button as follows

<asp:imagebutton id="imgPrint" runat="server"
ImageUrl="images/b_print_friend.gif"></asp:imagebutton>

and my server side code is

Private Sub imgPrint_Click(ByVal sender As Object,
ByVal e As System.Web.UI.ImageClickEventArgs) Handles
imgPrint.Click
imgBack4.Visible = False
imgSubmit.Visible = False
imgSave5.Visible = False
imgPrint.Visible = False

Dim script As String
script += " script += "function PrintThisPage() <br> "
script += "{ <br>"
script += "var
sOption='toolbar=yes,location=no,directories=yes,menubar=yes,';
<br>"
script +=
"sOption+='scrollbars=yes,width=750,height=600,left=100,top=25';
<br>"
script += "var sWinHTML =
document.getElementById('contentstart').innerHTML;
<br>"
script += "var
winprint=window.open('','_top',sOption); <br>"
script += "winprint.document.open(); <br>"
script +=
"winprint.document.write('<html><LINK
href=/eggheadcafe.css rel=Stylesheet><body>'); <br>"
script += "winprint.document.write(sWinHTML);
<br>"
script +=
"winprint.document.write('</body></html>'); <br> "
script += "winprint.document.close(); <br> "
script += "winprint.focus(); <br>"
script += " } <br> "
script += "</script>"

If Not
IsClientScriptBlockRegistered("PrintThisPage") Then
RegisterClientScriptBlock("PrintThisPage",
script)
End If

End Sub


but nothing is happening on the client side. Do I have
to do something else to execute this script?

Would appreciate your help.

Thank you,

Burak
 
in the pageload event

Dim script As String
script += " script += "function PrintThisPage() <br> "
script += "{ <br>"
script += "var
sOption='toolbar=yes,location=no,directories=yes,menubar=yes,';
<br>"
script +=
"sOption+='scrollbars=yes,width=750,height=600,left=100,top=25';
<br>"
script += "var sWinHTML =
document.getElementById('contentstart').innerHTML;
<br>"
script += "var
winprint=window.open('','_top',sOption); <br>"
script += "winprint.document.open(); <br>"
script +=
"winprint.document.write('<html><LINK
href=/eggheadcafe.css rel=Stylesheet><body>'); <br>"
script += "winprint.document.write(sWinHTML);
<br>"
script +=
"winprint.document.write('</body></html>'); <br> "
script += "winprint.document.close(); <br> "
script += "winprint.focus(); <br>"
script += " } <br> "
script += "</script>"



imgPrint.Attributes.Add("onclick",script)
 
I tried it out but it only executed the code behind, not the client side function. :(


wessamzeidan said:
in the pageload event

Dim script As String
script += " script += "function PrintThisPage() <br> "
script += "{ <br>"
script += "var
sOption='toolbar=yes,location=no,directories=yes,menubar=yes,';
<br>"
script +=
"sOption+='scrollbars=yes,width=750,height=600,left=100,top=25';
<br>"
script += "var sWinHTML =
document.getElementById('contentstart').innerHTML;
<br>"
script += "var
winprint=window.open('','_top',sOption); <br>"
script += "winprint.document.open(); <br>"
script +=
"winprint.document.write('<html><LINK
href=/eggheadcafe.css rel=Stylesheet><body>'); <br>"
script += "winprint.document.write(sWinHTML);
<br>"
script +=
"winprint.document.write('</body></html>'); <br> "
script += "winprint.document.close(); <br> "
script += "winprint.focus(); <br>"
script += " } <br> "
script += "</script>"



imgPrint.Attributes.Add("onclick",script)
 
In the original post all your button click code is doing is registering a client-side javascript function - it never does anything to cause that javascript function to be executed. And, I think that javascript function would blow up due to all the html '<br>' tags impropertly included in it (more on this far below).

It doesn't look like you're using any values from the code behind inside the script block you're generating (your PrintThisPage() javascript function) - so I don't see why you bother to generate it from code behind. You could make your life a lot easier and just stick the page printing script block in your aspx page. Then your only task is to associate your asp:button control with that script - and there's absolutely no reason to use an asp:button unless you need to execute some code behind for it (I guess you could do a postback to change the visibility of all those images but you could also just do it within the PrintThisPage() javascript function and avoid an extra postback).

So, one approach would be to handle all aspects of the print button in the aspx using javascript rather than doing a postback.

If you want to keep the postback (for dealing with image visibility) one way to make the print happen would still be to put the PrintThisPage() javascript function in the aspx page (since there's still no reason to generate it from a postback (button click). Instead, in your print button click code behind you'd generate a tiny script block like this:

Code:
Dim sScript As String

sScript = "<script language=javascript>"
sScript += "PrintThisPage();"
sScript += "</script>"

And use Page.RegisterStartupScript (or whatever it's called) rather than the general register script method. A startup script block is placed at the bottom of your page - so it executes *after* all page elements are rendered - which can be important because sometimes javascript will have issues with trying to manipulate stuff that doesn't exist yet.

What'll happen is the print button is clicked, the page will postback and insert that tiny script block, and when the page is rendered that little script block will fire the PrintThisPage() javascript function.

Also, you don't want to concatenate in '<br>' tags with script you generate in code behind. '<br>' is an html tag - it won't function as a carriage return/newline for javascript - you'd need to concatenate in a vbCrLf constant instead if you were wanting to generate nicely formatted script from code behind. For example:

Code:
Dim sScript As String

sScript = "<script language=javascript>" + vbCrLf
sScript += "PrintThisPage();" + vbCrLf
sScript += "</script>" + vbCrLf

...but...your browser doesn't care how nice your html or your script looks - it ignores extra whitespace. Unless you want to view source yourself for debugging you don't really need to worry about it. I've always been very particular about how my html source looks - but ASP.Net creates absolute garbage for HTML, particularly if you use fun controls like repeaters or register script from code beind, so I wouldn't bother overexerting yourself when ASP.Net is going to destroy your efforts anyways.

Paul
 
Back
Top