从 vb.net 代码后面调用 javascript

发布于 2024-12-08 14:43:27 字数 318 浏览 0 评论 0原文

如何从代码隐藏中调用 javascript 函数?
最流行的响应是“ScriptManager.RegisterStartupScript”,但是,这在我的情况下不起作用。

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

所以我正在做类似

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

如何从 vb.net 类中调用此函数?

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.

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")

So I am doing something like

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

茶底世界 2024-12-15 14:43:27
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

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

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

沉睡月亮 2024-12-15 14:43:27

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

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

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

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

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

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

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.

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

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

Then, your script will execute at the point the Literal is rendered.

A neater way of doing it is to add your script to the page via a ScriptManager as 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?

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.

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