简体   繁体   English

在VB.NET代码后面动态添加JS块

[英]Dynamically adding JS block in VB.NET code behind

I am working on a system that administers a number of pages to end users for data entry. 我正在开发一个系统,该系统管理着许多页面供最终用户输入数据。 There is a need to prevent the back button (I know, not ideal, but that is the requirement), but to disable this 'block' when viewing the pages in test mode. 有必要防止返回按钮(我知道,不理想,但这是必需的),但是在测试模式下查看页面时,请禁用此“阻止”。

I have the necessary JS and it works for what we need, but I'm having issues adding the code dynamically to the page so that it fires. 我有必要的JS,它可以满足我们的需求,但是在将代码动态添加到页面上以使其触发时遇到问题。

What do I mean by this? 我是什么意思 Well, I can get the code block added dynamically so it looks exactly as it should, but the code doesn't work. 好吧,我可以动态添加代码块,以使其看起来完全正确,但是代码不起作用。 If I copy and paste the text and manually add it to the aspx page (so not using code behind to do it), it works fine. 如果我复制并粘贴文本,然后将其手动添加到aspx页面(这样就不用后面的代码了),它就可以正常工作。

Here's the JS to work-around the back button click: 这是解决后退按钮单击问题的JS:

> <script type="text/javascript">
>       function preventBack(){window.history.forward();} setTimeout("preventBack()", 0); window.onunload=function(){null};
>     </script>

In my aspx.vb code behind file I have the following: 在文件后面的aspx.vb代码中,我具有以下内容:

If Not TestMode Then
  ' add JS
End If

I have tried adding the JS in the following ways - none of which seem to have worked: 我尝试通过以下方式添加JS-似乎都没有用:

  1. Page.RegisterClientScriptBlock("preventBack", "<script type=""text/javascript"">function preventBack(){window.history.forward();} setTimeout(""preventBack()"", 0); window.onunload=function(){null};</script>")

  2. Dim scriptText As New StringBuilder 昏暗的scriptText作为新的StringBuilder

     scriptText.Append("<script type='text/javascript'>") scriptText.Append("function preventBack(){") scriptText.Append(" window.history.forward();} ") scriptText.Append("setTimeout('preventBack()', 0); ") scriptText.Append("window.onunload=function(){null};") scriptText.Append("</script>") ClientScript.RegisterClientScriptBlock(Page.GetType, "noback", scriptText.ToString, False) 
  3. Dim lit As New Literal 昏暗的新文字

     lit.Text = "<script type='text/javascript' src='JS/noBack.js'>" '"<script type='text/javascript'>function preventBack(){window.history.forward();} setTimeout('preventBack()', 0); window.onunload=function(){null};</script>" Head1.Controls.Add(lit) 
  4. Dim gc As New HtmlGenericControl Dim gc作为新的HtmlGenericControl

     gc.TagName = "script" gc.Attributes.Add("type", "javascript") gc.Attributes.Add("src", "JS/noBack.js") Head1.Controls.Add(gc) 
  5. ClientScript.RegisterClientScriptBlock(Me.GetType(), "MyScript", "JS/noBack.js", True)

Where the noBackJS file contains the JS code mentioned above. 其中noBackJS文件包含上述JS代码。

As I said, the actually code is written to the HTML on the page when rendered, but doesn't work/fire at all. 正如我所说,实际的代码在呈现时会写到页面的HTML上,但根本无法正常工作/无法启动。 Clearly I'm missing something here, but can't for the life of me work out what it is, can anyone help please? 显然我在这里缺少什么,但是我一生无法解决它的问题,有人可以帮忙吗?

Thx. 谢谢。

try this: 尝试这个:

Dim jScript As String = "<script>alert('')</script>"
Page.ClientScript.RegisterStartupScript(Me.GetType(), "Registered Script", jScript, False)

您可以使用eval() ,例如:

eval($("#mytextbox").val())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM