join me
join me

Reputation: 33

How do I call a confirm popup box?

I want to show an confirm popup (yes, no) if an asp.net button was clicked.

If I click yes or no it should execute some c# code.

Lets say if I click yes it will insert a record and if I click no it should update the record

Can someone give me an example on how can I achieve this? I'm really having a hard time since I'm new in javascript and c#.

Upvotes: 2

Views: 2401

Answers (3)

Priyank Raunak
Priyank Raunak

Reputation: 11

OnClick="btnSubmit_Click"

onclientclick= "return confirm('Are You Sure To Submit Record ?.')";

Or You can use

onclientclick="return(window.confirm('Are You Sure To Submit Record?'))" ;

Upvotes: 0

Niranjan Singh
Niranjan Singh

Reputation: 18290

You can not get that which button is clicked Yes or No using the button's ClientClick event through JavaScript only. You can control the submission of the form by returning true or false.

 btn.OnClientClick = "return confirm('Ready to submit.')";

Javascript confirm without code in code behind

That functionality you are expecting to perform that need some client side JavaScript to server side code interaction. this may be possible check json.org. there are some libraries that perform such type of functionality.

As you asked that if click on Yes then it will do some thing and if No then Update. you can do this using the Ajax Controls. if you are allowed to use them in your solution.

If you are not allowed to use them then create a hidden field, and set the value of that field based on the result of the confirmation.

.aspx

<input type="hidden" id="confirmResult" name="confirmResult" value="" />
<asp:Button  OnClientClick="ConfirmDialog();" 
    OnClick="OnServerClick" runat="server" />

<script>
   function ConfirmDialog() {
      var result = Confirm("Confirm Insert?");
      // set a flag to be submitted - "true"/"false"
      // or whatever suits
      document.getElementById("confirmResult").value = result; 
   }

c#

protected void OnServerClick(object sender, EventArgs e) {
    bool dialogResult = Convert.ToBoolean(confirmResult.Value);
if(dialogResult)
{
//Insert
}
else
{
//Update
}
}

Check this stackoverflow thread for more details:
ASP.net - Button - Javascript Confirm dialog - execute SOME server-side code?

Check this link - AJAX Enabled MessageBox that i have used perform such functionality using the UserControl and Ajax, that get the confirmation result at the Server side Click event.

Have look on this, It is using ClientClient event of button etc but on this client side event it have used ModalPopupExtender ajax control.
Confirm Message Box

Upvotes: 1

Adam Rackis
Adam Rackis

Reputation: 83358

If you'd like to allow the user to cancel a button they just clicked on, you can use the OnClientClick attribute:

<asp:Button runat="server" OnClientClick="return confirm('Are you sure?')" 
    OnClick="CSharpMethod" />

Where CSharpMethod is a method in your code behind that executes the C# lines you want run (when the user accepts the confirmation).

The signature of this method should look like this:

protected void CSharpMethod(object sender, EventArgs E) {
    Response.Write("Hello From Server");
}

EDIT

If you want the true of false result of the confirmation to be sent to the server, I would recommend you first create a hidden input:

<input type="hidden" runat="server" id="hdnConfirm" />

Then in your .net button

<asp:Button runat="server" OnClientClick="confirmWithUser()" 
    OnClick="CSharpMethod" />

Then create the JavaScript function to go with the OnClientClick:

function confirmWithUser(){
    document.getElementById("hdnConfirm").value = confirm('Are you sure?');
}

In your C# method, you'll be able to get the response from the user like this:

protected void CSharpMethod(object sender, EventArgs E) {
    bool whatDidUserSay = Boolean.Parse(hdnConfirm.Value);

    //now do one thing or the other based on this value
}

Upvotes: 5

Related Questions