|
-
Jan 1st, 2010, 05:20 PM
#1
Thread Starter
Addicted Member
Run Java in code behind using ClientScript.RegisterStartupScript
Hello every one,
I know how to register a client script that is on my web page, but I would like to know how to register my script when the script is in another file. Does anyone know how to register a file containing java script file. This is my code for calling the function when it's in the page.
HTML Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script language="javascript" type="text/javascript" >
function openNewWin(url)
{
var x = new Object;
x = window.open(url, 'mynewwin', 'width=600,height=600,toolbar=1');
}
</script>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnOpenPop" runat="server" Text="Open Pop" />
</div>
</form>
</body>
</html>
This is the code behind for my button.
Code:
Protected Sub btnOpenPop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpenPop.Click
Dim url As String = "http://www.yahoo.com"
ClientScript.RegisterStartupScript(Me.GetType(), "OpenWin", "<script>openNewWin('" & url & "')</script>")
'How do I register script in an external file called JScript.js
End Sub
I have the exact same function in a javascript file (Jscript.js) in the root directory. I know I need the src property to idenify the file, but how do I register the whole script file and call the function?
This is the code in my javascript file
Code:
//// Jscript.js /////
function openNewWin(url) {
var x = new Object;
x = window.open(url, 'mynewwin', 'width=600,height=600,toolbar=1');
}
If anyone can help me I would appreciate it.
Thanks
-
Jan 2nd, 2010, 02:12 AM
#2
Re: Run Java in code behind using ClientScript.RegisterStartupScript
hi you can do it this way:
Code:
Page.ClientScript.RegisterClientScriptInclude("uniqueID",
ResolveClientUrl("Jscript.js"));
you must give each file unique id, if you don't only the last file will be register.
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Jan 2nd, 2010, 09:41 AM
#3
Thread Starter
Addicted Member
Re: Run Java in code behind using ClientScript.RegisterStartupScript
Thanks!
I got everything to work using RegisterClientScriptInclude but the only way I could get it to work was calling RegisterStartupScript again when I wanted to call the function. It works great but I wanted to make sure I was doing it correctly or if there was a better way to call my function without using RegisterStartupScript. Is there a better way to do this than the way I'm dooing it?
Thanks
GEM
Javascript code:
Code:
//// Jscript.js /////
function openNewWin2(url) {
var x = new Object;
x = window.open(url, 'mynewwin', 'width=600,height=600,toolbar=1');
}
HTML Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script language="javascript" type="text/javascript" src="JScript.js" ></script>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnOpenPop" runat="server" Text="Open Pop" />
</div>
</form>
</body>
</html>
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Page.ClientScript.RegisterClientScriptInclude("Java1", ResolveClientUrl("Jscript.js"))
End If
End Sub
Protected Sub btnOpenPop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpenPop.Click
Dim url As String = "http://www.yahoo.com"
ClientScript.RegisterStartupScript(Me.GetType(), "OpenWin", "<script>openNewWin2('" & url & "')</script>")
'\\is there a better way to call my function than using RegisterStartupScript - this seem rendundant
End Sub
-
Jan 3rd, 2010, 07:08 AM
#4
Re: Run Java in code behind using ClientScript.RegisterStartupScript
Hey,
Is there a reason why you are "including" the JavaScript code in your Server Side code? Why not attach it directly in a link tag in the head of your page? Once you have done that, you can simply call RegisterStartupScript in the knowledge that is js file is already available on the client.
Gary
-
Jan 3rd, 2010, 07:49 AM
#5
Thread Starter
Addicted Member
Re: Run Java in code behind using ClientScript.RegisterStartupScript
I guess that was my other question. The way I did it worked but it seemed redunant. I had the java script in the page, then 2 register scripts in the code behind.
I had no reason to put the javascript in the page other than to idenity the javascript file. I experimented trying several different ways and this is the first way I found that worked. If you know another way without the redundancy I would love to hear it. I assume you were talking about an include statement something like the one below.
<!--#INCLUDE virtual="jsscript.js"-->
Could you give me an example? All of my code is posted
-
Jan 3rd, 2010, 07:53 AM
#6
Re: Run Java in code behind using ClientScript.RegisterStartupScript
Hey,
Not quite, what I was referring to was this:
Code:
<script type="text/javascript" src="simplemethods.js"></script>
Have a look here:
http://www.yourhtmlsource.com/javasc...avascript.html
and here:
http://www.w3schools.com/js/js_examples.asp
For more information.
Gary
-
Jan 3rd, 2010, 08:19 AM
#7
Thread Starter
Addicted Member
Re: Run Java in code behind using ClientScript.RegisterStartupScript
I think I understand your question now. I think you are referring to something like this:
<A HREF="javascript:newwindow()" ><IMG SRC="scare.jpg" border="0"></A>
What I used in my post was just a sample snippet. The code I eventually use will be used in a gridview control. When the user clicks on the part number in the grid I will create a query string using the part number then open my page with the query string and show the sales summary by customer for that part.
I currently have everything working using response.redirect but that opned a whole page; my boss wanted more of a 'pop-up' screen.
-
Jan 3rd, 2010, 08:34 AM
#8
Re: Run Java in code behind using ClientScript.RegisterStartupScript
Hey,
Ok, now I am lost.
Did you try including the javascript file in the head of your markup?
Gary
-
Jan 3rd, 2010, 08:47 AM
#9
Thread Starter
Addicted Member
Re: Run Java in code behind using ClientScript.RegisterStartupScript
I included the java script in the markup. This is from my initial post
HTML Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script language="javascript" type="text/javascript" src="JScript.js" ></script>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnOpenPop" runat="server" Text="Open Pop" />
</div>
</form>
</body>
</html>
-
Jan 3rd, 2010, 09:24 AM
#10
Re: Run Java in code behind using ClientScript.RegisterStartupScript
Registering the file and calling the function are two different things. Use RegisterClientScriptInclude to include the JS on your page - that simply produces the <script> tag and points to the JS file. Then, use RegisterClientScript or RegisterStartupScript to call the JS function that you want, the JS function can be on the page itself or in the JS file that you just included.
-
Jan 3rd, 2010, 09:54 AM
#11
Thread Starter
Addicted Member
Re: Run Java in code behind using ClientScript.RegisterStartupScript
Hello mendhak,
I'm confused, This is what I think you meant
If you use
Page.ClientScript.RegisterClientScriptInclude("Java1", ResolveClientUrl("Jscript.js"))
then you don't need to use
<script language="javascript" type="text/javascript" src="JScript.js" ></script> because it redundant. That was my question about redundancy.
I just tried that and it didn't work. I removed the <script language="javascript" type="text/javascript" src="JScript.js" ></script> and in the page load I used
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Page.ClientScript.RegisterClientScriptInclude("Java1", ResolveClientUrl("Jscript.js"))
'This does the same thing as
' <script language="javascript" type="text/javascript" src="JScript.js" ></script> Right?
End If
End Sub
in the button to call the function I used
Code:
Dim url As String = "http://www.yahoo.com"
ClientScript.RegisterStartupScript(Me.GetType(), "OpenWin", "<script>openNewWin2('" & url & "')</script>")
It didn't work. what am I doing wrong? Is the vb code in the wrong place?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|