简体   繁体   English

如何从asp.net子函数调用javascript函数?

[英]How to call a javascript function from asp.net Sub functions?

So after extensive search, I found that in order to trigger a javasript function, we could do this: 因此,在广泛搜索之后,我发现为了触发javasript函数,我们可以这样做:

<script>
function foobar()
  {
   alert("foobar");
  }
</script>

Dim strScript As String = "<script language='javascript' id='myClientScript'>foobar();</script>"

Page.RegisterStartupScript(“callTest”,strScript)

However, the Page.RegisterStartupScript seem to be working only under the Page_load function.... 但是,Page.RegisterStartupScript似乎只能在Page_load函数下工作。

When I put it in a Sub, like this: 当我将其放在Sub中时,如下所示:

Sub Test

Dim strScript As String = "<script language='javascript' id='myClientScript'>foobar();</script>"

Page.RegisterStartupScript(“callTest”,strScript)

End Sub

This won't work. 这行不通。 As I link the above function to an asp button. 当我将上述功能链接到一个asp按钮时。 I triggered the button, but nothing happens. 我触发了按钮,但是什么也没发生。 So is there anyway to trigger javaScript function conditionally from asp.net? 因此,无论如何有条件地从asp.net触发javaScript函数? From a Sub function instead of on every page_load? 从子函数而不是每个page_load?

Thanks!! 谢谢!!

You should represent this line 您应该代表这一行

Dim strScript As String = "<script language='javascript' id='myClientScript'>foobar();</script>"

as

Dim strScript As String
strScript = "<script language='javascript' id='myClientScript'>foobar();</script>"

If you want to call javascript for asp.net button click, you can use OnClientClick attribute with a client side function OnClientClick="buttonClick()" :- 如果要为asp.net按钮单击调用javascript,则可以将OnClientClick属性与客户端函数OnClientClick="buttonClick()" :-

<asp:button id="Button1" 
  runat="server" 
  OnClientClick="buttonClick()" 
  Text="Click!" />

The javascript injected using the RegisterStartUpscript method is executed when the page is first loaded. 首次加载页面时,将执行使用RegisterStartUpscript方法注入的javascript。 Try using the RegisterClientScriptBlock method when you need to inject and execute javascript after the 当您需要在插入之后执行JavaScript时,请尝试使用RegisterClientScriptBlock方法。

Type t = this.GetType();
if (!ClientScript.IsClientScriptBlockRegistered(t, "myClientScript"))
{
   ClientScript.RegisterClientScriptBlock(t,"myClientScript", sb.ToString());
}

Not sure if it will work inside an updatepanel though 虽然不确定它是否可以在更新面板中使用

Below is the JavaScript function I am using and have used on numerous web applications where I work. 以下是我正在使用的JavaScript函数,该函数已在我工作的众多Web应用程序中使用。 On this specific example, I am comparing the clients IP address to a predetermined IP address on our network, and if the clients IP doesn't match, I display a message box that then informs the user that their specific computer terminal is not permitted to make these specific requests. 在此特定示例中,我正在将客户端IP地址与网络上的预定IP地址进行比较,如果客户端IP不匹配,则会显示一个消息框,然后告知用户不允许其特定的计算机终端提出这些具体要求。

Dim ipAdd as string = nothing
ipAdd = Request.ServerVariables.Item("REMOTE_ADDR")
If ipAdd <> "###.###.###.###" then
ClientScript.RegisterClientScriptBlock(Page.GetType, "Script", "<script language='javascript'>alert('This terminal is not permitted to submit requests.');</script>")
End If

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

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