javascript 如何在c#代码隐藏中调用具有返回值的Javascript函数

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

How to call a Javascript function having return value in c# codebehind

c#javascript

提问by Vinod

I am having a page where i need to confirmwhether to show updated grid or not

我有一个页面需要确认是否显示更新的网格

I am calling a javascript function using

我正在调用一个 javascript 函数使用

ScriptManager.RegisterStartupScript(this, typeof(string), "Error", "confirm('Are u sure');", true);

What I want to know is how to get the return value of confirm in C# code behind.

我想知道的是如何在后面的C#代码中得到confirm的返回值。

回答by yogi

Instead of this

而不是这个

ScriptManager.RegisterStartupScript(this, typeof(string), "Error", "confirm('Are u sure');", true);

use this

用这个

ScriptManager.RegisterStartupScript(this, typeof(string), "Error", "ConfirmUser('Are u sure');", true);

<script type='javascript'>
 function ConfirmUser(msg)
 {
  if(confirm(msg))
    __doPostBack('','');
 }
<script>

see following links to know more about __doPostBack

请参阅以下链接以了解更多信息 __doPostBack

12

1 2

回答by adatapost

I am having a page where i need to confirm whether to show updated grid or not

我有一个页面需要确认是否显示更新的网格

You may prevent the postback when use choose Cancelbutton of confirm dialog.

您可以在使用Cancel确认对话框的选择按钮时防止回发。

<asp:Button ID="Button1" runat="server" 
            OnClientClick="return confirm('Are you sure to populate the list?');" 
            Text="Button" onclick="Button1_Click" />