How to get a response from the Confirm field in the code

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 when a button is clicked. Before saving a record, I will check if this record exists in the database or not (in the code behind). If it exists, then I need to show the warning to the user as "The record already exists." Do you want to continue? "When the user clicks" Yes ", I need to continue saving the record in the code, otherwise 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 code above shows that I confirm the box with the "OK" and "Cancel" buttons. My questions

  • How can I do this “Yes” or “No” in the confirmation dialog?
  • After the user clicks “Yes” / “No”, how can I catch the answer (yes / no) and continue the rest of my program?

I searched this a lot. But could not get the right answer. Please help me with this.

+5
source share
5 answers

You can do this in many ways:

One of them places a button on the page, makes it display: none, and when true is confirmed, start its click with js.

Like in aspx

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

on the client side, create a js function to call up a confirmation dialog, e.g.

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

}

in the code below

Code verification in some event handler

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

bthSaveData click handler to save data.

protected void btnSaveData_Click(object sender, EventArgs e)
{
 // Code for your saving.
}
+3
source

You can use a confirmation window in JS like this

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

-, , Yes/No , .

document.getElementById('<%= hiddenField.ClientID %>').value = ans;
+2

TRUE/FALSE , runat = "server", , (/). .

0

AJAX modalpopupextender.

   <cc1:modalpopupextender id="ModalPopupExtender1" runat="server"  cancelcontrolid="ButtonNo" okcontrolid="ButtonYes" popupcontrolid="PNL" targetcontrolid="UrSaveButton">
   </cc1:modalpopupextender>  
   <asp:Panel ID="PNL" runat="server" Style="display: none; width: 200px; background-color: #000099; border-width: 2px; border-color: Black; border-style: solid; padding: 20px;">
     <span style="color: White; font-weight: bold">Record already exist.Do you want to proceed?</span>
       <br />
        <br />
            <div style="text-align: right;">
                <asp:Button CssClass="tbl_blue" ID="ButtonYes" runat="server" CausesValidation="false" EnableTheming="false" Text="YES" />
                <asp:Button CssClass="tbl_blue" ID="ButtonNo" runat="server" CausesValidation="false" EnableTheming="false" Text="NO" />
             </div>
  </asp:Panel>

.

0

All Articles