从后面的 vb.net 代码调用 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7673795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:09:10  来源:igfitidea点击:

Call javascript from vb.net code behind

javascriptasp.netvb.netcode-behind

提问by Troy

How can I call a javascript function from code behind?
The most popular response is "ScriptManager.RegisterStartupScript" however, that does not work in my situation.

如何从后面的代码调用 javascript 函数?
最受欢迎的回答是“ ScriptManager.RegisterStartupScript”,但是,这在我的情况下不起作用。

I have a vb class that is doing a database check to see if a record exists. If exists, then call a javascript function to display an alert("Record exists")

我有一个 vb 类正在执行数据库检查以查看记录是否存在。如果存在,则调用 javascript 函数以显示警报(“记录存在”)

So I am doing something like

所以我正在做类似的事情

Dim strMessage as string = "javascript:RecordExists('Param');"  

How do I call this function from my vb.net class?

我如何从我的 vb.net 类调用这个函数?

回答by Deeptechtons

If DataStore.Record.Exists(theRecord) Then

    Dim script As String = "alert('Record exists')"
    If Not Page.ClientScript.IsStartUpScriptRegistered(Me.GetType(), "alertscript") Then
        Page.ClientScript.RegisterStartUpScript(Me.GetType(), "alertscript", script, True)

    End If
End If

you would do it like above, where you should replaceDataStore.Record.Exists(theRecord) with condition that checks database record exists

你会像上面那样做,你应该用检查数据库记录存在的条件替换DataStore.Record.Exists(theRecord)

回答by Widor

You need to think about your script in a slightly different way - remember, JavaScript runs client-side, and VB.NET runs server-side. So you can't "call" JavaScript from the server side.

您需要以稍微不同的方式考虑您的脚本 - 请记住,JavaScript 在客户端运行,而 VB.NET 在服务器端运行。所以你不能从服务器端“调用”JavaScript。

However, you can generateJavaScript on the server side, but it will need to be output to the page before it can run.

但是,您可以在服务器端生成JavaScript,但它需要输出到页面才能运行。

If you were doing a full page postback, a crude way of achieving it would be to assign the script or function to a Literalcontrol, which renders its Textproperty on the HTML page exactly as written.

如果您正在执行整页回发,实现它的一种粗略方法是将脚本或函数分配给一个Literal控件,该控件Text在 HTML 页面上完全按照所写的方式呈现其属性。

Then, your script will execute at the point the Literalis rendered.

然后,您的脚本将在Literal渲染时执行。

A neater way of doing it is to add your script to the page via a ScriptManageras you noted. Rather than a StartupScript, you could try using .RegisterClientScriptBlock()instead? You don't mention what it is about your situation that doesn't work?

一种更简洁的方法是通过ScriptManager您指出的a 将您的脚本添加到页面中。而不是 a StartupScript,您可以尝试使用.RegisterClientScriptBlock()?你没有提到你的情况是什么不起作用?

The most comprehensive way of doing it would be to use AJAX - either .NET's built-in framework, or jQuery. jQuery's AJAX (and AJAX in general) is a separate topic, which you can read about here.

最全面的方法是使用 AJAX - .NET 的内置框架或 jQuery。jQuery 的 AJAX(以及一般的 AJAX)是一个单独的主题,您可以在此处阅读。