JavaScript confirmation on ASP.Net control button

I am working on an ASP.Net C # application, I wanted to create a Button control when a user clicks a button, a JavaScript confirmation popup window, then receives a logical value from the user (yes / no) to perform further actions in the onClick event on the button.

In my current approach, the OnClientClick and OnClick events were added in the button where the OnClientClick launch function is launched and the value (Yes / No) is stored in the HiddenField Control for use during the OnClick event.

This is something like the following code snippets:

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 a better approach for this? thank you in advanced condition.

+4
source share
2 answers

Modify the CreatePopup () function to return a boolean:

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

And then make sure you return this from OnClientClick() in the button:

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

Using this method, the OnClick() method is launched only if the OnClientClick() method returns true.

+7
source

Why do you want to save the JavaScript confirmation value in a hidden field? Just return false in your JavaScript function when the confirmation value is “no” to prevent the form from submitting. When cnfirm is yes, return true to allow form submission. In the code specified in the button_click method, you do not need to check what happened in your JavaScript confirmation, as the form will never be submitted if the user says no.

0
source

All Articles