Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the response from Confirm box in the code behind

Tags:

c#

asp.net

I am new to asp.net/C# .I am trying to create a web application.

Below is my requirement.

I am trying to save a record on button click. Before saving the record,I will be checking if that record exist in the database or not(in the code behind).If it exist,then I need to show an alert to the user as "Record already exist.Do you want to proceed?"When the user press 'Yes',I need to continue my save for the record in the code ,else I just need to exit the save process.

//......code for checking the existence of the record    
if (check == true)
{            
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script", " confirm('Record already exist.Do you want to proceed?');", true);
}
//

The above code shows me confirm box with 'OK' and 'Cancel' buttons. My questions are

  1. how can I make it 'Yes' or 'No' in the confirm dialog?
  2. After the user press 'Yes'/'No',how can I catch the response(yes/no),and proceed with rest of my program?

I have searched for this a lot.But couldn't get a proper answer.Please help me on this.

like image 583
Soumya Avatar asked Jul 25 '12 05:07

Soumya


2 Answers

You can do this in many ways:

One is place a button on the page, make its display:none and when confirm is true, trigger its click with js.

Like in aspx

<asp:Button runat="server" ID="btnSaveData" 
     onClick="btnSaveData_Click" style="display:none;" />

in client side make a js function for calling confirmation dialog box, like

function ConfirmSave()
{
   if(confirm('Record already exist.Do you want to proceed?')
   {
       jQuery("[ID$=btnSaveData]").click();
   }

}

in Code Behind

Your code check in some event handler

if (check == true)
{            
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script", 
      "ConfirmSave();", true);
}

bthSaveData click handler for saving data.

protected void btnSaveData_Click(object sender, EventArgs e)
{
 // Code for your saving.
}
like image 50
Yograj Gupta Avatar answered Oct 14 '22 22:10

Yograj Gupta


You can use confirm box in JS like this

var ans = confirm ('Record already exist.Do you want to proceed?');
if(ans==true)
{
}
else
{
}

Secondly, to get the response in code behind, you can store the Yes/No value into a hidden field e.g.

document.getElementById('<%= hiddenField.ClientID %>').value = ans;
like image 44
Umesh Avatar answered Oct 14 '22 20:10

Umesh