问题描述
我在一个ASP.Net C#应用程序的工作,我想创造一个按钮控制,当按钮用户点击,一个JavaScript确认弹出,然后从用户那里得到的布尔值(是/否)执行进一步行动在按钮onclick事件。
I working on an ASP.Net C# application, I wanted to create a Button Control, when user click on the button, a JavaScript confirm popup, then get the Boolean value from the user (Yes/No) to perform further actions in the button onClick event.
我目前的做法是在按钮,其中的OnClientClick触发JavaScript函数和(是/否)增值的OnClientClick和OnClick事件是存储到HiddenField控件OnClick事件过程中使用。
my current approach was added OnClientClick and OnClick event in the button, where OnClientClick trigger JavaScript function and the (Yes/No) value is store into HiddenField Control to make use during OnClick event.
有类似如下的code片段:
It is something like the following code fragments:
function CreatePopup(){
var value = confirm("Do you confirm?");
var hdn1 = document.getElementById('hdn1');
hdn1.Value = value;
}
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" OnClientClick="CreatePopup()"/>
<asp:HiddenField ID="hdn1" runat="server" />
有没有为了做到这一点任何更好的办法?谢谢你在先进。
Is there any better approach in order to do this? thank you in advanced.
推荐答案
更改CreatePopup()函数返回一个布尔值:
Change your CreatePopup() function to return a boolean:
function CreatePopup()
{
return confirm("Do you confirm?");
}
然后确保您返回从的OnClientClick()
的按钮:
&LT; ASP:按钮...的OnClientClick =返回CreatePopup(); /&GT;
使用该方法时,的OnClick()
法会,如果的OnClientClick()
方法返回true只火。
Using that method, the OnClick()
method will only fire if the OnClientClick()
method returns true.
这篇关于JavaScript的确认上的ASP.Net按钮控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!