javascript 通过 C# 中的代码隐藏获取确认框值

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

Get confirm-box value via code-behind at C#

c#javascriptasp.netcode-behindconfirm

提问by Engin Kartal

I want to get the value from side to confirm aspx

我想从侧面获取值以确认 aspx

bool type=false;

 type=   ClientScript.RegisterStartupScript(typeof(Page), "exampleScript", "confirm('are you confirm?')", true);

if(type){
...
}

How do I get the value of?

我如何获得价值?

回答by Prash

Doesn't sound like best of the approach (I mean you could show pop-up client side)... However, if you want to accomplish this...

听起来不是最好的方法(我的意思是你可以显示弹出客户端)......但是,如果你想完成这个......

You have have a hidden asp:Button on your aspx and attach a event handler to it and write the code that you want to have executed upon clicking Yes on the confirm button.

您的 aspx 上有一个隐藏的 asp:Button 并为其附加一个事件处理程序,然后编写您希望在确认按钮上单击 Yes 后执行的代码。

And modify your RegisterStartupScript as below

并修改您的 RegisterStartupScript 如下

type=   ClientScript.RegisterStartupScript(typeof(Page), "exampleScript", "if(confirm('are you confirm?')) { document.getElementById('btn').click(); } ", true);

回答by Naveen

I was facing the same issue. The below code worked for me.

我面临同样的问题。下面的代码对我有用。

ClientScript.RegisterStartupScript(typeof(Page), "exampleScript", "if(confirm(\"Are you sure?\")){ document.getElementById('Button1').click(); }", true);
<asp:Button ID="Button1" Visible="true" SkinID="button"  OnClick="Button1_Click"  runat="server" />

Use a hidden buttonand write the code on click event of the button. Please note that don't use visible="false"property to make the button hidden. Instead use style="display:none"

使用 ahidden button并在按钮的单击事件上编写代码。请注意,不要使用visible="false"属性来隐藏按钮。而是使用style="display:none"

回答by bgs

bool type=false;

  "return confirm('are you confirm?')"

if(type){
...
}