Q: combining Javascript and ASP-function?

H

haakern

Hi,

In an aspx page, I've got a TreeView and a detail form and a print
button. When the user clicks on the print button, I'd like to collapse
the TreeView and send the page to the printer.
Using Javascript, the print is easy:
<asp:ImageButton ID="btnPrint" runat="server" AlternateText="Print"
ImageUrl="~/images/print.gif"
OnClientClick="javascript:window.print();return false;" ToolTip="Print
page" />

However, I'm not able to combine the window.print() function with the
necessary method to collapse the TreeView (MyTreeView.CollapseAll()).

Anyone who's got any pointers on how to combine the Javascript print-
function with the ASP.NET TreeView.Collapse method on a single button?

Thanks!

Regards,
-Haakon-
 
H

haakern

Thanks for the reply. I think I was a bit unclear on what I wanted to
achieve.
I need to collapse my TreeView before I print the page. So going to
the server and collapsing the tree after firing off the browser print
function will not solve my problem.

I tried:

<script runat="server">
void CollapseTree(Object sender, EventArgs e)
{
TreeView1.CollapseAll();
}
</script>

and then:

<asp:ImageButton ID="ImageButton1" runat="server"
AlternateText="Print" ImageUrl="~/images/print.gif"
OnClientClick="CollapseTree;javascript:window.print();return true;"
ToolTip="Print page" />

but that results in the error: 'CollapseTree' is undefined - which is
strange, given that I have other ASP-functions that DO work on e.g.
OnSelectedIndexChanged

Regards,
-Haakon-
 
E

Eliyahu Goldin

What you put in OnClientClick runs on client side whereas CollapseTree is a
server side method.

You don't need to use OnClientClick. Instead let CollapseTree do its job and
communicate a request to print to the client side.

This article may help you for the last part:
How to Pass Messages and Actions between Server and Client
http://usableasp.net/DeveloperPage....o/HowToPassMessagesBetweenServerAndClient.htm

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Thanks for the reply. I think I was a bit unclear on what I wanted to
achieve.
I need to collapse my TreeView before I print the page. So going to
the server and collapsing the tree after firing off the browser print
function will not solve my problem.

I tried:

<script runat="server">
void CollapseTree(Object sender, EventArgs e)
{
TreeView1.CollapseAll();
}
</script>

and then:

<asp:ImageButton ID="ImageButton1" runat="server"
AlternateText="Print" ImageUrl="~/images/print.gif"
OnClientClick="CollapseTree;javascript:window.print();return true;"
ToolTip="Print page" />

but that results in the error: 'CollapseTree' is undefined - which is
strange, given that I have other ASP-functions that DO work on e.g.
OnSelectedIndexChanged

Regards,
-Haakon-

The "return false;" part of the OnClientClick tells the event not to
progress to the server where the collapsing part is handled. Change it to
"return true"

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP
[ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top