Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, November 5, 2008 5:46 PM
This should be easy. I am trying to reference a .js file in my c# website. The code below is on the control...
< script type="text/jscript" runat="server" src="JScript.js" >
< /script >
The code below is in the JScript.js file...
function DisplayProblem(DivID) {
alert("test");
return false;
}
When I attempt to run the site it gives me an error of "Identifier expected" on line 1 of JScript.js.
Any help is appreciated.
All replies (6)
Thursday, November 6, 2008 3:41 AM âś…Answered
Hi
If you want to write a javascript file dynamicly. you must write following code in cs file:
Page.ClientScript.RegisterClientScriptInclude("a", "JScript.js");
and this code in HTML:
<button onclick="DisplayProblem(2);">sdgfsdf</button>
but if you use from runat server attribute in script tag. ASP.NET think this file is C# [:D]
you write following code in JScript.js and write your code. see:
JScript.js File:
public class Class1
{
public Class1()
{
}
}
HTML file:
<script type="text/javascript" runat="server" src="JScript.js">
</script>
these codes is ok. you must write C# code in JScript.js File
Wednesday, November 5, 2008 6:28 PM
Try opening your page in Firefox and view the error in the Error Console. IE is terrible at reporting javascript errors.
Wednesday, November 5, 2008 8:37 PM
Thank you for the response.
It's not a javascript error. It's a .net error.
Compiler Error Message: CS1001: Identifier expected
Wednesday, November 5, 2008 8:43 PM
Oh your using server side jscript... [:S] can you use the "src" attribute like that with a server side script tag..? Sorry I've never seen anyone do what ur trying to do
Thursday, November 6, 2008 12:40 AM
Hai,
The javascript file code should be like C# code once you Make it as **RUNATSERVER, The property such as alert is not recognized by the complier as it does not exist i, when javascript is "RunAtServer".
**
This is your modified code
protected void test(object sender, EventArgs e)
{
Response.Write( "hello world" );
}
<script type="text/javascript" runat="server" src="JScript1.js"></script>
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick ="test"></asp:Button>
</div>
hope it helped you
bye , (if this resolves your issue please mark this post as answered)
Thursday, November 6, 2008 7:57 AM
That is exactly what I needed. Thank you.