Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript and ASP.Net button 1

Status
Not open for further replies.
Mar 14, 2002
711
US
This may be a very dumb question as I am surely overseeing something simple. I am trying to run a Javascript when someone pushes a button on my webform (asp.net), but it comes back with an error when the page loads, button is not even yet pushed.

This is the code:

<script language=javascript>
function CallPrint(strid)
{
var prtContent = document.getElementById(strid);
var WinPrint = window.open('','','letf=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
prtContent.innerHTML=strOldOne;
}
</script>




<div id="divPrint">
<asp:datagrid>
...
...
...
</asp>

</div>

<P align="center"><asp:button id="btnPrint" runat="server" OnClick="CallPrint(strid);"></asp:Button></P>

What am I missing here???

Thanks,
 
It is telling you there is in error in your code. If you are using IE, doubleclick the icon on the bottom left of the browser, it will display a pop up with error detail.

Jim
 
you have left mispelled...also double click the yellow triangle shaped error message to see the actual error...

-DNG
 
Sorry this is what is should be, but the error's the same:

<P align="center"><asp:button id="btnPrint" runat="server" OnClick="Javascript:CallPrint(strid);"></asp:Button></P>


Error:

BC30456: 'Javascript' is not a member of 'ASP.Bottom_aspx'
 
Hmm, same error message even though I changed it to lower case...could it have something to do with the Javascript itself or where I put it in the code on the HTML page?
 
the javascript needs to go between the <HEAD> tags of the HTML portion of your page
 
can you do this for debugging purposes...

function CallPrint(strid)
{
alert("inside CallPrint");
}
</script>

if you get the alert page...go ahead and add your code line by line and put the alert after each line and see where the code is breaking...

-DNG
 
In your code:

Code:
<asp:button id="btnPrint" runat="server" OnClick="Javascript:CallPrint(strid);"/>

You reference the OnClick event. This is actually a reference to a server-side event and is probably the source of your problems.

Instead, on the server do something like this:

Code:
btnPrint.Attributes.Add( "onclick", "CallPrint('some id');" );

If there's an error in your javascript, you can track it down by unchecking the "Disable Scrip Debugging" option in IE and launching the Script Explorer (if using VS 2005). Using it, you'll be able to set breakpoints and step through debugging as for other code.



 
Alright! That worked (sort of, hehehehe), it opened my grid data in a new window and printed, but without any formatting, so I got 3 pages of lines of data unformatted. I guess now I have to search the other forums for more info on how to format it correctly...

Thanks everyone!!
 
Got it, had to set the strid = divPrint (function which called the formatted datagrid right before the datagrid on the HTML page.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top