help running javascript in my webpart
-
jeudi 16 août 2012 16:18
I am making a visual webpart in SP 2010, and I need help on how to run some javascript.
As you can see I made a js file in the root of the webpart
As well I changed its type to embedded resource
In the AssemblyInfo.cs I added
[assembly: System.Web.UI.WebResource("JScript1.js", "text/javascript")]In the javascript file I added
function button_click() { alert('button clicked'); } button_click();And on the page load function of the webpart I added:
Page.ClientScript.RegisterClientScriptResource(GetType(), "JScript1.js");
But when I test the webpart, nothing happens.
Does anyone know why?
I expect the alert code to work but it isn't...
Also how can I call a function from the javascript file?
I don't want to attach a onclick event or anything, I want something like
call_function("JScript1.js", "button_click();");
Can anyone help please?
Toutes les réponses
-
jeudi 16 août 2012 16:34
hi
1-create mapped folder for layouts in your project.
2-add a java script file(myScript.js) to that folder
3-write your java script Code
5- in custom page's Code Behind, in page_Load function , add bellow code.
ScriptLink.RegisterScriptAfterUI(this.Page, "/_layouts/myScript.js", false, true);
yaşamak bir eylemdir
- Modifié Ilker Alizade jeudi 16 août 2012 16:35
- Marqué comme réponse Jinchun ChenMicrosoft Employee, Moderator mercredi 22 août 2012 12:04
-
jeudi 16 août 2012 16:44
Ok I changed it to this:
I kept it as embedded resource.
I put this in the page load function, but it's saying ScriptLink doesn't exist in the current context.
ScriptLink.RegisterScriptAfterUI(this.Page, "/_layouts/myScript.js", false, true);
What am I doing wrong?
Also do I stil need this in the AssembyInfo.cs?
[assembly: System.Web.UI.WebResource("myScript.js", "text/javascript")]
-
jeudi 16 août 2012 16:53
you do not need changing assemblyInfo. remove lines that you added manualy
build action most be Content
yaşamak bir eylemdir
- Modifié Ilker Alizade jeudi 16 août 2012 16:54
-
jeudi 16 août 2012 17:04
What do you mean by this:
"remove lines that you added manualy"
Which lines?
And do you know how to fix the ScriptLink not being found error?
Thanks
-
jeudi 16 août 2012 17:09
You said that,
In the AssemblyInfo.cs I added
[assembly: System.Web.UI.WebResource("JScript1.js", "text/javascript")]
if you added that line manually remove it
yaşamak bir eylemdir
-
jeudi 16 août 2012 17:18ok thanks, so for ScriptLink do I need a "using x" ?
-
jeudi 16 août 2012 17:32
oh I figured it out, I need
using Microsoft.SharePoint.WebControls;
to run ScriptLink.
Ok but now how can I call a function in javascript? I don't want to use a button clicked event.
- Marqué comme réponse Jinchun ChenMicrosoft Employee, Moderator mercredi 22 août 2012 12:04
-
lundi 20 août 2012 09:56Modérateur
Hi rvands,
To call JavaScript function in .net, you can use RegisterStartupScript, you can get more information about this in the following links:
ClientScriptManager.RegisterStartupScript method:
http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx
how to call JavaScript code from c#:
http://stackoverflow.com/questions/9861321/how-to-call-a-javascript-function-from-c-sharp-code-behind-on-asp-net-button-cli
using JavaScript Along with ASP.NET:
http://msdn.microsoft.com/en-us/library/aa479011.aspxThanks,
Qiao Wei
TechNet Community Support
- Proposé comme réponse Sundar NarasimanMVP lundi 20 août 2012 10:46
- Marqué comme réponse Jinchun ChenMicrosoft Employee, Moderator mercredi 22 août 2012 12:04

