Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to add a pop up warning on server control button 1

Status
Not open for further replies.

skitty123

Technical User
May 4, 2004
56
US
I am trying to create a logout button on my application. which ends the users session, I currently have an HTML button with a runat=server since I have it doing the following actions 'onclick':

myownclass.unsetUserInfo();//my own function
Session.Abandon();
Server.Transfer("~/Home/LoggedOut.aspx");

Is it possible to also add a pop-up message box when the user clicks this button? I dont know how to add a box before it calls the serverside function.
 
Please try to use the search this question was just answered yesterday.

thread855-860041


Hope everyone is having a great day!

Thanks - Jennifer
 
Thanks but I was looking for an example in C# , the thread you pointed to was in VB, and i tried to modify it but not being very familier with java script i keep getting errros:
I tried this:
javascript: return confirm("Are you sure you want to delete this record?'");

gives me an errro saying cannot recognize 'confirm'
Also what do I use for just a OK type of message box ( no yes/no)
 
Substitute the html <input type=button> with a Button server control and handle the above login in its Click event. In page code behind in the OnInit event, attach javascript to this button:
Code:
html:
<asp:Button ID="myButton" Runat=Server />

code behind:
private void myButton_Click(object sender, System.EventArgs e)
{
  myownclass.unsetUserInfo();//my own function 
  Session.Abandon();
  Server.Transfer("~/Home/LoggedOut.aspx");
}

override protected void OnInit(EventArgs e)
{
  //
  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  //
  InitializeComponent();
  base.OnInit(e);

  // attach javascript to the onclick event of rendered button
  if(myButton.Attributes["onclick"] == null)
  {
    myButton.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete this record?')");
  }
}
The client side onclick will execute prior to the server side Click event. If false returned from the confirm, the form won't post back and the server side Click event will not fire.
 
thanks LV! it works great. but how do i capture the return value from the question in the message box
ie
1) if Yes --- do something
2) if no -- something else
 
Where do you need to capture it? On the client or server? And what is the purpose for capturing the return value?
 
I need to capture it on the server i guess. the purpose is , say the user pressed a button called "Delete". the pop up box gives the user a warning asking "are you sure you want to delete this?"
if the user says yes: call delete function
if the user says no: call some other fucntion.
 
Well, the above functionality will do the following (correct me if I'm wrong):
1. If the user says yes: the post back will occur and the Click server side event of the Button will fire - you perform the delete in there.
2. If the user says no: do you really need to call another function? The user just changed his mind and nothing should occur - no post back. It's a "cancel" ot "do nothing" logic.
 
Yes, in this case I probably dont care , but was just curious if it was possible to capture the return value
 
Sure possible on the client, but getting it to the server would be more complicated:
Code:
<head>
 <script language=javascript>
   function confirmDelete(){
     var doDelete = false;
     if(confirm("Are you sure...?")){
       // "Yes": return true to post back
       doDelete = true;
     }
     else{
       // "No": for example, redirect to another page
       window.location = "/somePage.aspx";
     }

     return doDelete;
   }
 </script>
</head>
.......................
if(myButton.Attributes["onclick"] == null)
{
  myButton.Attributes.Add("onclick", "javascript:confirmDelete()");
}
 
Thanks so much LV, exactly what I needed to know!!!
 
one more question: is it possible to show the message box AFTER the server code has run ? as in
"Thank you. your changes have been saved." ....after a user presses "Submit".

I tried to add this code inside the click event of a submit button. but nothing happens.

btnSubmit.Attributes.Add("onclick", "javascript:alert('Your change has been saved.');");
 
Possible. Just do it inside of the server side Click event of your Button control:
Code:
using System.Text;
.........................
private void myButton_Click(object sender, System.EventArgs e)
{
  // some logic here...
  StringBuilder sb = new StringBuilder();
  sb.Apppend("<script language=javascript>");
  sb.Append("function window.onload(){");
  sb.Append("alert('Your change has been saved.');}");
  sb.Append("</script>");

  if(!Page.IsClientScriptBlockRegistered("SaveConfirm"))
  {
    Page.RegisterClientScriptBlock("SaveConfirm", sb.ToString());
  }  
}
This should display the alert box after your server side script is processed on top of rendered page HTML.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top