Click here to Skip to main content
16,020,628 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
If i click yes its returns No

It return First click value on my second click ,

if i click yes first time it returns no,and i click second time no its return my first value yes..

What I have tried:

JavaScript
function Confirm() {
             var confirm_value = document.createElement("INPUT");
             confirm_value.type = "hidden";
             confirm_value.name = "confirm_value";
             if (confirm("Do you want to Delete data?")) {
                 confirm_value.value = "Yes";
             } else {
                 confirm_value.value = "No";
             }
             document.forms[0].appendChild(confirm_value);
         }

VB CODE
VB
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, GetType(Page), "Script", "Confirm();", True)

        Dim confirmValue As String = Request.Form("confirm_value")
        If confirmValue = "Yes" Then

            Response.Write("User Clicked yes")
        Else
            Response.Write("User Clicked No")
        End If
Posted
Updated 2-Mar-17 4:22am
v2

The approach here is wrong. If you want to confirm that a user is doing something then do something like this in JavaScript:
JavaScript
function ConfirmDelete(){
return confirm("Do you want to Delete data?"); // this means that the function will either return true or false depending on what user clicks.
}


Then in the ASPX side on your button you do this:
HTML
<aspButton id="btnConfirm" Text="Confirm" OnClientClick="return ConfirmDelete();" OnClick="btnClick" />


By adding OnClientClick to your button and then returning the value from ConfirmDelete(), if the user clicks Cancel in the Confirm dialog box, a false will be returned and OnClientClick will return false which will prevent the button from firing the postback and server side code. If user clicks OK in Confirm dialog then true is returned and the server side code will get executed.
 
Share this answer
 
Comments
Karthik_Mahalingam 2-Mar-17 11:03am    
5
It's hard to answer as we don't know the context the VB code is executed in. Is it in Page Load? Or somewhere else?

Your issue probably stems from a misunderstanding that RegisterClientScriptBlock is somehow running your javascript in code behind and that your .net code is running inside the browser and waits for your input and then does your "if" line after you click the confirmation in the browser. Well none of that is happening.

What RegisterClientScriptBlock is doing is adding that javascript to the output so that when all of your .net code has executed and all html has been built, that html is then sent to the browser and it is then that the javascript runs.

VB.NET
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, GetType(Page), "Script", "Confirm();", True)
' after executing the line above your .net code continues running

Dim confirmValue As String = Request.Form("confirm_value")

' there is no confirm_value in the request so confirmValue will be ""

If confirmValue = "Yes" Then

Response.Write("User Clicked yes")
Else
Response.Write("User Clicked No")
End If

' now that your .net code has finished building html to send to the client, that
' html is sent and the browser then runs your js and the confirm box appears
 
Share this answer
 
Comments
ManiLancer 2-Mar-17 9:41am    
i want to run it in button click event , OnClientClick="return Confirm()" Wont work for my scenario,i want to call the function from code behind only
ZurdoDev 2-Mar-17 11:29am    
OnClientClick is the right way to do it. Why won't it work for you?
ManiLancer 2-Mar-17 11:41am    
ya right but i want it to cal it using code behind..
F-ES Sitecore 2-Mar-17 11:43am    
Call what?
ManiLancer 2-Mar-17 11:45am    
Javascript Function "Confirm()"
This code Working Fine
JavaScript
function Confirm() {
    var confirm_value = document.createElement("INPUT");
    confirm_value.type = "hidden";
    confirm_value.name = "confirm_value";
    if (confirm("Do you want to Delete data?")) {
        confirm_value.value = "Yes";
    } else {
        confirm_value.value = "No";
    }
    document.forms[0].appendChild(confirm_value);
}

ASP.NET
<asp:Button ID="Button1" OnClientClick="Confirm()" runat="server"  Text="Button"/>

VB
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       
    Button1.OnClientClick = "return Confirm();"

    Dim confirmValue As String
    confirmValue = Request.Form("confirm_value")

    If confirmValue = "Yes" Then
        Response.Write("User Clicked yes")
    Else
        Response.Write("User Clicked No")
    End If

End Sub
 
Share this answer
 
v3

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