Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Confirm on ASP.Net Button Control

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.

like image 696
sams5817 Avatar asked Oct 25 '11 08:10

sams5817


1 Answers

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.

like image 140
Sir Crispalot Avatar answered Nov 15 '22 06:11

Sir Crispalot