简体   繁体   English

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

[英]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.

相关问题 如何将jQuery库嵌入到asp.net自定义服务器控件中? - How to embed jquery library in asp.net custom server control? 如何使ASP.NET识别我的自定义服务器控件? - How do I get ASP.NET to recognize my custom server control? ASP.NET如何将javascript函数添加到控件? - ASP.NET How do I add javascript function to a control? 如何在托管控件的页面上调用ASP.Net Custom控件的嵌入式资源中的JavaScript函数? - How do you call a JavaScript function that is in an embedded resource for an ASP.Net Custom control on the page that the control is hosted? 如何给 ASP.Net 自定义控件一个新的自定义事件 - How do I give an ASP.Net custom control a new custom event ASP.NET自定义控件和JavaScript函数 - Asp.net custom control and javascript function 如何有条件地控制ASP.NET中控件的可见性? - How do I conditionally control the visibility of a control in ASP.NET? 如何在.NET 3.5 Web应用程序中安装和使用ASP.NET AJAX Control Toolkit? - How do I install and use the ASP.NET AJAX Control Toolkit in my .NET 3.5 web applications? 如何在自定义ASP.NET控件中使用ScriptManagerProxy? - How do you use ScriptManagerProxy in a custom ASP.NET control? 异步回发后如何在我的ASP.NET控件中保留私有级别变量? - How do I retain my private level variable in my ASP.NET control after an async postback?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM