[英]How do I embed javascript directly into my ASP.net Custom Control?
I am building my first user control and I would like to package the javascript required for the control with the assembly so the end user does not have to worry about including dependencies. 我正在构建我的第一个用户控件,并且希望将控件所需的javascript与程序集打包在一起,因此最终用户不必担心会包含依赖项。 I've followed a tutorial from Scott Mitchell ( http://aspnet.4guysfromrolla.com/articles/080906-1.aspx ) but I can't seem to get it to work right. 我遵循了Scott Mitchell的教程( http://aspnet.4guysfromrolla.com/articles/080906-1.aspx ),但我似乎无法使其正常运行。
Here is what I've done so far: 到目前为止,这是我所做的:
I've created a CollapsiblePanel.js file that contains the following function: 我创建了一个CollapsiblePanel.js文件,其中包含以下功能:
function TogglePanel(panelId) {
// $(panelId + ' .PanelContent').toggle();
alert(panelId);
}
Under the properties panel I set the Build Action to "Embedded Resource". 在属性面板下,将“构建操作”设置为“嵌入式资源”。 This file resides in a scripts/ directory inside my class library project. 该文件位于我的类库项目中的scripts /目录中。 The root namespace of my project is webstation.WebControls so if my understanding is correct I should be referencing the js file via "webstation.WebControls.scripts.CollapsiblePanel.js" 我项目的根名称空间是webstation.WebControls,因此,如果我的理解正确,我应该通过“ webstation.WebControls.scripts.CollapsiblePanel.js”引用js文件。
I've added the following line just before my class declaration for the custom control: 我在自定义控件的类声明之前添加了以下行:
<Assembly: WebResource("webstation.WebControls.scripts.CollapsiblePanel.js", "text/javascript")>
I've overridden the OnPreRender event in my Custom Control and added the following: 我在自定义控件中覆盖了OnPreRender事件,并添加了以下内容:
Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
Page.ClientScript.RegisterClientScriptInclude("wsWebControlsCollapsiblePanel", _
Page.ClientScript.GetWebResourceUrl(Me.GetType(), "webstation.WebControls.scripts.CollapsiblePanel.js"))
MyBase.OnPreRender(e)
End Sub
When I render my control I have a button with the function "TogglePanel(this.id);" 呈现控件时,我有一个带有功能“ TogglePanel(this.id);”的按钮。 in the onclick event, but when I click the button I get an error saying that the function is not defined. 在onclick事件中,但是当我单击按钮时,我收到一条错误消息,提示未定义函数。 If anyone knows how I might begin using my embedded javascript I would really appreciate the help, 如果有人知道我将如何开始使用嵌入式JavaScript,我将非常感谢您的帮助,
Mike 麦克风
Found it, 找到了,
The problem was I included 'scripts' in the resource name so that I had "webstation.WebControls.scripts.CollapsiblePanel.js" 问题是我在资源名称中包含了“脚本”,所以我有了“ webstation.WebControls.scripts.CollapsiblePanel.js”
I decided to try it so I had: "webstation.WebControls.CollapsiblePanel.js" 我决定尝试一下,所以有了:“ webstation.WebControls.CollapsiblePanel.js”
and that did the trick. 然后就成功了。
It's a little confusing because all the documentation specifically states including the path as part of the naming convention, but perhaps I did something wrong when including a folder in the project. 这有点令人困惑,因为所有文档都特别说明了将路径作为命名约定的一部分,但是在项目中包含文件夹时,也许我做错了。
Mike 麦克风
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.