如何将 JavaScript 直接嵌入到我的 ASP.net 自定义控件中?
我正在构建我的第一个用户控件,我想将控件所需的 javascript 与程序集打包在一起,以便最终用户不必担心包含依赖项。我遵循了 Scott Mitchell 的教程(https://web.archive.org/web/20210510023046/http://aspnet.4guysfromrolla.com/articles/080906-1.aspx)但我似乎无法得到它正确工作。
这是我到目前为止所做的:
我创建了一个 CollapsiblePanel.js 文件,其中包含以下函数:
function TogglePanel(panelId) {
// $(panelId + ' .PanelContent').toggle();
alert(panelId);
}
在属性面板下,我将“构建操作”设置为“嵌入资源”。该文件位于我的类库项目内的 script/ 目录中。我的项目的根命名空间是 webstation.WebControls 所以如果我的理解是正确的我应该通过“webstation.WebControls.scripts.CollapsiblePanel.js”引用js文件
我在自定义的类声明之前添加了以下行控件:
<Assembly: WebResource("webstation.WebControls.scripts.CollapsiblePanel.js", "text/javascript")>
我已经重写了自定义控件中的 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
当我渲染控件时,我有一个带有“TogglePanel(this.id);”功能的按钮在 onclick 事件中,但是当我单击按钮时,出现错误,提示该函数未定义。如果有人知道我如何开始使用嵌入式 javascript,我将非常感谢您的帮助,
Mike
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. I've followed a tutorial from Scott Mitchell (https://web.archive.org/web/20210510023046/http://aspnet.4guysfromrolla.com/articles/080906-1.aspx) but I can't seem to get it to work right.
Here is what I've done so far:
I've created a CollapsiblePanel.js file that contains the following function:
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. 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"
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:
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);" in the onclick event, but when I click the button I get an error saying that the function is not defined. If anyone knows how I might begin using my embedded javascript I would really appreciate the help,
Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到它,
问题是我在资源名称中包含“脚本”,这样我就有了“webstation.WebControls.scripts.CollapsiblePanel.js”
我决定尝试一下,所以我有:
“webstation.WebControls.CollapsiblePanel.js”
就成功了。
这有点令人困惑,因为所有文档都明确指出将路径作为命名约定的一部分,但也许我在项目中包含文件夹时做错了什么。
麦克风
Found it,
The problem was I included 'scripts' in the resource name so that I had "webstation.WebControls.scripts.CollapsiblePanel.js"
I decided to try it so I had:
"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