Javascript 如何从后面的asp.net代码调用javascript函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6829186/
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-23 23:21:06  来源:igfitidea点击:

How to Call javascript function from asp.net code behind

javascriptasp.net

提问by emre

How to call JavaScript function from ASP.NET code behind?

如何从后面的 ASP.NET 代码调用 JavaScript 函数?

I have JavaScript function and I am using it in a listviewwith a parameter

我有 JavaScript 函数,我在listview带参数的a 中使用它

I want to use it with another parameter in code behind ?

我想在后面的代码中与另一个参数一起使用它?

 <a href="ChatBox.aspx?id=<%# Eval("USR_UserID")%> " 
     onclick="openWindow(this.href,'chat<%# Eval("USR_UserID")%>',600,600);
     this.blur();
     return false;"><%# Eval("USR_UserName") %></a>

that is the listviewside. How can I use openwindowfunction in code behind fore specific ID?

那是listview一边。如何openwindow在特定 ID 后面的代码中使用函数?

回答by Evan

I would think using RegisterClientScriptBlockwould be the best solution.

我认为使用RegisterClientScriptBlock将是最好的解决方案。

Lets say you have a javascript function, MyJSFunction():

假设您有一个 javascript 函数MyJSFunction()

function MyJSFunction() {
        ...
}

In your event, such as a Button Click, you would inject the following code:

在您的事件中,例如按钮单击,您将注入以下代码:

C#:

C#:

ScriptManager.RegisterClientScriptBlock(this, typeof(System.Web.UI.Page), "MyJSFunction", "MyJSFunction();", true);

VB.NET:

VB.NET:

ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "MyJSFunction", "MyJSFunction();", True)

This effectively inserts a call to your JavaScript from the code-behind.

这有效地从代码隐藏插入了对 JavaScript 的调用。

回答by Madhur Ahuja

Use RegisterStartupScriptor RegisterClientScriptBlock

使用RegisterStartupScriptRegisterClientScriptBlock

Injecting Client-Side Script from an ASP.NET Server Control http://msdn.microsoft.com/en-us/library/aa478975.aspx

从 ASP.NET 服务器控件注入客户端脚本 http://msdn.microsoft.com/en-us/library/aa478975.aspx