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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With