最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - JavaScript Confirm on ASP.Net Button Control - Stack Overflow

programmeradmin0浏览0评论

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.

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.

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.

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.

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.

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.

Share Improve this question asked Oct 25, 2011 at 8:15 sams5817sams5817 1,03710 gold badges34 silver badges49 bronze badges 2
  • do you need to fire up a server event even if user say "no"? – balexandre Commented Oct 25, 2011 at 10:01
  • Yes that is the requirement, see the ment to my answer! – Sir Crispalot Commented Oct 25, 2011 at 10:11
Add a ment  | 

2 Answers 2

Reset to default 11

Change your CreatePopup() function to return a boolean:

function CreatePopup()
{
    return confirm("Do you confirm?");
}

And then ensure you return that from the OnClientClick() in the button:

<asp:Button ... OnClientClick="return CreatePopup();" />

Using that method, the OnClick() method will only fire if the OnClientClick() method returns true.

Why do you want to store the value of the JavaScript confirmation in the hidden field? Just simply return false in your JavaScript function when the confirm value is 'no' , to prevent the form from submitting. When cnfirm value is yes, return true to allow the form to be submitted. In your code behind in the button_click method you don't have to check what happened in your JavaScript confirm, since form will never be submitted if the user said no.

发布评论

评论列表(0)

  1. 暂无评论