Click here to Skip to main content
15,886,868 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using a confirm function is js and calling the function on the click of a button, i am getting the Confirm box on the click of the button in the button, but the message that i am passing in the Confirm function is not appearing.Here is the code below

<script type="text/javascript">
C#
function confirm() {
            var x;
        var r=confirm('Are You Sure!');
        if (r==true)
        {
            x="You pressed OK!";
        }
        else
        {
            x="You pressed Cancel!";
        }


I am passing a message here Are you sure but i m not getting it, rather it is blank and what i am getting is only the two buttons that is OK & Cancel.

XML
<asp:GridView ID="gdvData"  runat="server" Height="159px" Width="511px"
                   AutoGenerateColumns="False">

                   <Columns>

<asp:TemplateField HeaderText="Delete" HeaderStyle-BackColor="#4EA600" HeaderStyle-ForeColor="White">
<itemtemplate>
<asp:LinkButton ID ="ButtonDelete" Text="Delete" OnClientClick="confirm()" value="Are You Sure!" runat="server" />



And also please let me know how can i use the onclick event of the Buttons i.e. Ok and Cancel of the Confirm function
Posted

hi,

try to change like below:

<asp:linkbutton id="ButtonDelete" text="Delete" onclientclick="return confirmmessage();" value="Are You Sure!" runat="server" />

JavaScript
<script>
  function confirmmessage()
  {
     if(confirm('Are you sure!'))
     { //yes  
       // to call your sp on coding side
        $.ajax({
                type: "POST",
                url: "Demo.aspx/DeleteSupplierDetails",
                contentType: "application/json; charset=utf-8",
                data: "{R1:'" + value+ "', R2:'" + Value+ "', R3:'" + Value+ "', R4:'" + Value + "'}",
                dataType: "json",
                success: function (data) {
                    if(data.d == true)
                        alert('successfully call sp');

                },
                failure: function (data) {
                    alert('sp not called');
                }
            });
     }
     else
     { //No  
        return false;
     }
  }
</script>

// on server side
XML
[System.Web.Services.WebMethod(true)]
        public static bool DeleteSupplierDetails(string R1,string R2,string R3,string R4)
        { 
             //call your sp
             // if success
             return true;
         }


hope it helps.
 
Share this answer
 
v4
Comments
Rock_Dead 5-Aug-13 6:52am    
Thanks the above solution worked fine but one issue i have here is on the aert box heading it is showing like "Windows Internet Explorer" Can i get this changed and how to use the onclick event of the OK and Cancel button of the message
Harshil_Raval 5-Aug-13 6:58am    
I am not sure how to change heading of confirm message. But for button event see my answer, i have comment //yes, means when ok button clicked flow comes in that section, and if cancel button clicked then see //No, flow will come in that section. For changing header of confirm, think you can not change it. If you really need to change it, then create your own popup. There are many js available for it.
Rock_Dead 5-Aug-13 7:17am    
I want to call a stored procedure on the click of OK button how can that be done
Harshil_Raval 5-Aug-13 7:23am    
To achieve it use jquery ajax method. You can find out many tutorials and post about this.Check my updated code.
Rock_Dead 5-Aug-13 7:25am    
Can i have an example on this please? and where can i find your updated code?
see this...:)

HTML
<html>
<head>
<script>
function disp_confirm()
{
var r=confirm("Press a button!")
if (r==true)
  {
  alert("You pressed OK!")
  }
else
  {
  alert("You pressed Cancel!")
  }
}
</script>
</head>
<body>

<input type="button" onclick="disp_confirm()" value="Display a confirm box">

</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900