Display the Yes / No warning window in C # code

I am trying to display a yes / no message from code in C #. I want to call the AddRecord procedure if the user clicks Yes and does nothing if the user clicks No.

Ideally, I want to use the code below, but from codebehind:

OnClientClick = "return confirm('Are you sure you want to delete?');" 

I searched for SO and google but couldn't find anything useful.

+4
source share
6 answers

on the "Add Entry" button, simply follow these steps:

  <asp:button ID="AddRecordbutton" runat="server" Text="Add Record" onclick="AddRecordButton_Click" onclientclick="return confirm('add record?');" /> 

In your code behind, just add the write code to the AddRecordButton_Click event handler. It will only be called if they clicked β€œYes” in the popup window.


Alternatively, you can assign your code to the onclientclick code when the button was originally displayed.

For instance:

 protected void Page_Load(object sender, EventArgs e) { AddRecordButton.OnClientClick = @"return confirm('Add Record?');"; } 
+11
source

No, you do not.

You seem to misunderstand the basic concept of a web page.

An ASPX page is a short program that starts, generates HTML, and then exits. HTML is then sent over the Internet to the users browser. Everything you do in the code must be complete before the user ever sees it.

You really need a javascript dialog. (In fact, from what you described, you could simply create a view for the HTML code as an HTML code with a standard HTML form.)

+2
source

To display the actual message, you will need javascript, as it is done on the client side. For whatever reason, if you cannot use javascript, you can do what AEMLoviji suggested and "fake" it with some dexterity.

Note that you do not need jQuery to display the message, simple javascript is enough.

+1
source

If you use the Ajax Control Toolkit group popup window modifier in a panel with two of your buttons, this will trigger an event on the server that can be processed and executed no matter what method / function you want

See here for an example.

0
source

Use RegisterStartupScript

 ScriptManager.RegisterStartupScript(this, GetType(), "unique_key", "element.onclick = function(){ return confirm('Are you sure you want to delete?'); };", true); 
0
source

To show yes / no

 <script> function AlertFunction() { if (confirm('Are you sure you want to save this thing into the database?')) { $('#ConfirmMessageResponse').val('Yes'); } else { $('#ConfirmMessageResponse').val('No'); } } </script> 

to handle it from the .net side:

 string confirmValue = ConfirmMessageResponse.Value; if (confirmValue == "Yes") {...} 
0
source

All Articles