如何将 JavaScript 直接嵌入到我的 ASP.net 自定义控件中?

发布于 2024-08-07 09:21:01 字数 1396 浏览 4 评论 0原文

我正在构建我的第一个用户控件,我想将控件所需的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮了一弯 2024-08-14 09:21:01

找到它,

问题是我在资源名称中包含“脚本”,这样我就有了“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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文