Click here to Skip to main content
15,884,041 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All,
Good day to all of you. I having problem on how to execute my command once I have click on Confirm message that appear. If I would press Yes button, it will execute a certain command while if press No Button, it will prompt an alert message. Appreciate if you guys could help me on this matter. Please find below my code.

Here is my code for the command to execute if Yes been press

VB
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
       Dim strSQL As New StringBuilder
       If txtboxvalue.Text = "" Then
           txtboxvalue.Focus()
           cf.messagebox("There is no value to delete. Please select value !", Me)
           Exit Sub
       Else
           cf.msgboxyesno("Are you sure to delete this data !", Me)
           ' If msgboxyesno=Yes is clicked Then
           '    execute certain command
           ' else
           '    Exit Sub
           ' End If

       End If



Here is my code to display the confirm messagebox

VB
Public Sub msgboxyesno(ByVal sMsg As String, ByVal m As Page)

    Dim sb As New StringBuilder()
    Dim oFormObject As New System.Web.UI.Control

    sMsg = sMsg.Replace("'", "\'")
    sMsg = sMsg.Replace(Chr(34), "\" & Chr(34))
    sMsg = sMsg.Replace(vbCrLf, "\n")
    sMsg = "<script language=javascript>confirm(""" & sMsg & """)</script>"

    sb = New StringBuilder()
    sb.Append(sMsg)

    For Each oFormObject In m.Controls
        If TypeOf oFormObject Is HtmlForm Then
            Exit For
        End If
    Next

    oFormObject.Controls.AddAt(oFormObject.Controls.Count, New LiteralControl(sb.ToString()))

End Sub
Posted
Updated 18-Dec-14 13:41pm
v3
Comments
Sergey Alexandrovich Kryukov 19-Dec-14 1:24am    
Javascript? Where? :-)
—SA

1 solution

You're doing it wrong.

Write javascript function on the page. For btnDelete raise the question and return the result. If the result is yes, server side code triggers, if the answer is no (false) it will never get to server side.

Your code should look like this:
aspx file
ASP.NET
<asp:button id="btnDelete" runat="server" onclick="btnDelete_Click" onclientclick="javascript: return confirmDelete();" xmlns:asp="#unknown" />

<script type="text/javascript">
function confirmDelete() {
if (your-text-box.value.trim == "")
return confirm("There is no value to delete. Please select value !");
}
else
{
return confirm("Are you sure to delete this data ?");
}
</script>



in your aspx.vb file
VB
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
           '    execute your delete


You don't need server side msgboxyesno method.

If this helps please take time to accept the solution. Thank you.
 
Share this answer
 
v2
Comments
Nikolai.Kimi 19-Dec-14 2:59am    
Hi Sinisa, thanks it works
Sinisa Hajnal 19-Dec-14 5:37am    
Glad to be of help.

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