如何正确地将脚本添加到控件
How to add correctly a script to a control?
我有这样的代码:
Dim script = New HtmlGenericControl("script")
script.Attributes.Add("type", "text/javascript")
script.InnerText = "var alohaInterval = setInterval(function() {if (typeof Aloha === ""undefined"") {return;} clearInterval(alohaInterval); Aloha.ready(function() {Aloha.jQuery("".editable-content"").aloha();})}, 100);"
htmlControl.Controls.Add(script)
生成如下内容:
<script type="text/javascript">var alohaInterval = setInterval(function() {if (typeof Aloha === "undefined") {return;} clearInterval(alohaInterval); Aloha.ready(function() {Aloha.jQuery(".editable-content").aloha();})}, 100);</script>
问题是它是html编码的。我的问题是:如何确保脚本不会是html编码?
根据您的需要,您可以使用几种方法来完成此操作。
对于您总是希望控件可用的少量脚本,您最常作为字符串嵌入到代码中,您将需要使用RegisterClientScriptBlock或RegisterStartupScript。你选择哪一个主要取决于你想在哪里渲染脚本。你可以在这里找到一个很好的解释,什么时候选择一个或另一个。
您可能想要选择的另一个选项是RegisterClientScriptResource。当我编写一个包含大量JavaScript的服务器控件或者我想将其作为类库的一部分时,我发现这个选项最有用。使用此选项,您可以将脚本作为嵌入式资源编译到库中,然后将库包含在其他项目中。当我拥有的不仅仅是几行JavaScript代码时,编辑体验会好得多。
对于这种特殊情况:
ClientScript.RegisterClientScriptBlock(Me.GetType, "alohainit", "var alohaInterval = setInterval(function() {if (typeof Aloha === ""undefined"") {return;} clearInterval(alohaInterval); Aloha.ready(function() {Aloha.jQuery("".editable-content"").aloha();})}, 100);", True)
相关文章:
- 如何在ASP中为用户控件添加Javascript对象网
- 在HTML5画布上添加按钮和控件
- 向ASP控件添加自定义事件
- 在带有 bootbox 的自定义对话框中添加选项以选择表单控件.js在 ASP.NET MVC 中
- 如何动态添加 HTML5 视频控件
- 使用JavaScript将日期/时间添加到我的asp.net用户控件中
- 添加控件以操纵Javascript Canvas中的动画
- 如何向JavaScript计时器添加控件
- 如何将标记添加到Mapbox图层控件
- Asp.Net 将多个窗体添加到一个窗体时,用户控件无法正常工作
- 将页面控件添加到数据表,但当代码运行时,页面中不显示任何内容
- 如何使用 JavaScript 在分层结构上动态添加控件
- Cakephp 2.5:使用Cake控制器在js,css,images上添加控件
- Gridster没有在我想要的地方添加控件
- 向ng-repeat添加控件以过滤数据
- 设置“;“在javascript中添加控件
- 添加控件以显示莫里斯图表
- TypeError ';null '没有属性';添加控件时
- 如何在asp.net页面上动态添加控件,并根据之前添加的控件进行填充
- 动态添加控件javascript Asp.net c#