Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

how can i show a message box in an asp.net page that would show once a button is clicked.
the message box should have "Yes" and "Cancel" as 2 buttons and the rest of the VB.net code should only be executed if "Yes" is clicked.

seeking help on this

thanks
Joe
Posted
Updated 10-Oct-11 1:31am
v2

you can take a help of Javascript.
use Confirm() function. here is example

JavaScript
function askConfirm()
{
  if(confirm("are you sure"))
    alert("you clicked Yes")
  else
    {
      alert("you clicked No") 
      return false;
    }
}


add this function on your submit button click.

"return false" will cancel your execution.

call following function in ASPX page like this

ASP.NET
<asp:button id="btnSubmit" runat="Server" text="Submit" onclientclick="return askConfirm()" >
 
Share this answer
 
v2
Comments
joe_j 10-Oct-11 7:46am    
but if the user clicks YES then i need to execute functions that are coded with VB.net in the code behind page.
so how do i get if yes, then execute the codebehind code?

thanks
koolprasad2003 10-Oct-11 7:48am    
Dear friend, code behind get automatically called after Javascript execute.
if you clicked YES your code behind will called automatically.
paste the code and see the output.
[no name] 27-Sep-23 5:54am    
Hello
joe_j 10-Oct-11 7:54am    
i got the onclientclick whihc i have added,
but im pretty confused with where to add function askConfirm() and the rest of the code.
if i put it into the Button1_Click codebehind, it gives me an error
joe_j 10-Oct-11 8:00am    
oooh k.. i think i got it..
the java script without the <script type="text/javascript"> kinda got me confused if it was c# code or javascript.. thanks for that koolprasad.
you can take a help of Javascript.
use Confirm() function. here is example

JavaScript
function askConfirm()
{
  if(confirm("are you sure"))
    alert("you clicked Yes")
  else
    {
      alert("you clicked No") 
      return false;
    }
}


add this function on your submit button click.

"return false" will cancel your execution.
 
Share this answer
 
v2
Comments
joe_j 10-Oct-11 7:37am    
thanks for that reply. but do i add the above after this?
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
koolprasad2003 10-Oct-11 7:40am    
no, not at all.
you have to add this in ASPX page. see my changed reply
Member 9424392 9-Jun-13 6:15am    
I want to pop up the message box after some activity has been done on click event and then want to ask the confirmation message box
please help me :)
C#
if(result>0)
{
string myscript = "alert ('message');";
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", myscript, true);
                    return false;
}





hope this will work....
 
Share this answer
 
v2
Comments
joe_j 10-Oct-11 7:44am    
could you please let me know this in vb.net code
Insted of msgbox use confirm from javascript.
it has two buttons "ok" and "cancel".

msgbox will execute on the server while confirm executes on client.
you can code for confirm like this:
C#
if(confirm("Are you sure to reload data?")){
    grid.reload();
}else{
    //to do nothing
}
 
Share this answer
 
Comments
joe_j 10-Oct-11 7:48am    
hi,
confirm in vb.net is giving an error
hope this helps

VB
Private Sub Test_Click()
Dim testmsg As Integer
testmsg = MsgBox("Click to test", 1, "Test message")
If testmsg = 1 Then
Display.Caption = "Testing Successful"
Else
Display.Caption = "Testing fail"
End If

End Sub
 
Share this answer
 
add this property to your button
OnClientClick="return confirm('Are you sure you want to Proceed ?');"


like this
<asp:button id="btnDelete" runat="server" onclick="btnDelete_Click" onclientclick="return confirm('Are you sure you want to Proceed ?');"  />
 
Share this answer
 
v2
Comments
Member 9424392 9-Jun-13 6:13am    
In button can I add multiple functions in OnclientClick()
Try this.
ASP.NET
<asp:button id="btn" runat="server" xmlns:asp="#unknown">OnClientClick="javascript:return confirm('Are you sure you want to delete this user?')" /></asp:button>
 
Share this answer
 
you can use on clientClick Property of Button
for example

<asp:button id="btnSubmit" runat="Server" text="Submit" onclientclick="return askConfirm()" xmlns:asp="#unknown">

and for back end you are use following code for it

if(result>0)
{
string myscript = "alert ('message');";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", myscript, true);
return false;
}
 
Share this answer
 
Comments
Member 9424392 9-Jun-13 6:12am    
I m getting an error "Name result unknown error" in code
what should I do
Try This:

1) Right Click on Solution Explorer & Add reference the "System.windows.Forms";

2) Now create a button in Default.aspx.

3) Double click on that button, Default.aspx.cs will open.

4) In that first of all add a namespace at the top "System.windows.Forms".

5) Now in button1 coding block write code,

Collapse | Copy Code
protected void Button1_Click(object sender, EventArgs e)
{
MessageBox.Show("yes.", "AIOOB", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

}
6) Now it is done.

Gaurav Bhardwaj
B.Tech (Computer Science Engineering).
Developed Many Projects in ASP.NET.
 
Share this answer
 
check following code snippet which helps you lot

JavaScript
<head>
  <script language="Javascript">
   function showPOPUP()
   {
      if(confirm("Do you want to continue")) //show 1st popup
      {
        window.open('Default2.aspx','_blank'); //show your aspx page as POPUP
      } 
      else
      {
         alert("You pressed cancel");
      }
   }
  </script>
</head>
 
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