简体   繁体   English

从CodeBehind文件中调用JS函数

[英]Call JS function from CodeBehind file

I am having trouble accessing a Javascript function from my code behind file. 我无法从文件后面的代码访问Javascript函数。 I need to do this as I am using the GoogleMaps JS API to add markers to a map based on addresses retrieved from my database. 我需要执行此操作,因为我正在使用GoogleMaps JS API根据从数据库中检索到的地址向地图添加标记。 I have a function called AddMarker that takes in the address as a parameter, so I need to be able to call that from my code behind file in the page_load function. 我有一个名为AddMarker的函数,该函数将地址作为参数,因此我需要能够从page_load函数中文件后面的代码中调用该函数。

To simplify the question, how I can I call this javascript function to display an alert with a string passed from my code behind file?: 为了简化问题,我如何调用此javascript函数以显示警报,其中包含从文件后面的代码传递的字符串?

function hello(message)
{
alert(message)
}

Thanks in advance! 提前致谢!

PS Either vb or c# will do :) PS VB或C#都可以:)

That should do: 那应该做:

 protected void Page_Load(object sender, EventArgs e) 
 { 
     string bing = "link";
     Response.Write(@"<script language='javascript'>alert(bing);</script>");
 }

I'm not certain if this is best practice, but you could just render out a call to the JS function somewhere in your page, after it has been defined. 我不确定这是否是最佳做法,但是在定义好之后,您可以在页面中的某个位置呈现对JS函数的调用。

Or you could use jQuery to delay the call until everything in the page is rendered. 或者,您可以使用jQuery将呼叫延迟,直到页面中的所有内容都呈现为止。

So 所以

<script type="text/javascript">

$(document).ready(function(){

hello("myAspString");

});

</script>

Replace myAspString with your content, making sure to preserve the quotes as needed by JS. 用您的内容替换myAspString,确保保留JS所需的引号。

This code will cause the function hello to be called with value from the code behind: 此代码将导致使用后面的代码中的值调用函数hello

string value = "world";
Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "my_script", string.Format("hello('{0}');", value.Replace("'", "\\'")), true);

The RegisterClientScriptBlock will append proper <script> tags to the HTML output sent to browser and inject your code in there. RegisterClientScriptBlock会将适当的<script>标记附加到发送到浏览器的HTML输出中,并将代码注入其中。

The second argument is the "key" of the script, it enable you to have several statements and check if you already registered specific statement based on the key. 第二个参数是脚本的“键”,它使您可以拥有多个语句,并检查是否已基于键注册了特定的语句。 The last argument tells the framework to add <script> tags for you. 最后一个参数告诉框架为您添加<script>标记。

You need to replace any single quotes with the proper escape sequence to avoid breaking the string when it contains single quotes as this is the "delimeter" used to pass the value to the function. 您需要用适当的转义序列替换任何单引号,以避免在包含单引号的字符串时破坏该字符串,因为这是用于将值传递给函数的“定界符”。

You may consider this a hack but you could always put your message in an html element like: 您可能会认为这是一种黑客行为,但始终可以将消息放在html元素中,例如:

<p id='message' style='display: none;'>Your Message</p>

Then in your javascript: 然后在您的JavaScript中:

function hello()
{
    var m = document.getElementById('message').innerHTML;
    alert(m);
}

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

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