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 use MessageBox.show in Web Application 3

Status
Not open for further replies.

LadyDragon

Programmer
Jan 14, 2004
24
US
I'm trying to create a popup message to prompt a user Y/N whether they really want to delete a user before proceeding. The MessageBox class has the functionality I need, but my application is a web app. The MessageBox class resides within the System.windows.form.dll assembly however. Is there similar functionality within the System.web.* assemblies, or is there a way to use the classes located in the System.windows.form.dll assembly in a web app? When I tried referencing the System.windows assembly in my project, I got an error when I tried to build. (It couldn't find the definitions).

Any help you can provide will be greatly appreciated!

Thanks!
Juls
 
Juls,
The best way to accomplish what you need is to use the javascript function confirm(). Remember that ASP.NET code runs on the server and it would be unwise to go to the server just to display confirmation message. Javascript code, on the other hand, runs on the client which makes it very responsive as no trip to the server is required. Thus, in the HTML code of your page, add the following script:
Code:
<script language="javascript">
  [COLOR=blue]function[/color] ConfirmDel(msg)
  {
    [COLOR=blue]if[/color] (confirm(msg))
      [COLOR=blue]return true[/color];
    [COLOR=blue]else
      return false[/color];
  }
</script>
Then, assuming that btn is the button control that your users click on to delete a user, add this in your your Page_Load event:
Code:
[COLOR=blue]string[/color] msg = "Are you sure you want to...";
btn.Attributes.Add("onclick", 
    "ConfirmDel('" + msg + "');");
How it works
When btn is clicked, the javascript function ConfirmDel is called on the client (with the message you specify in the msg parameter). ConfirmDel will return either true or false, depending on what the user responds to the question. When true is returned, the form is submitted to the server where the actual deletion code runs (this is the server code that you write in the click event of btn). If the user responds no to the question, false is returned and the form is not submitted to the server, thus canceling the whole deletion process.

Hope this helps!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
Thanks for the help. I tried it and I'm getting the popup, but when I hit 'Cancel', the delete is executed anyway. Any ideas?

Thanks,
Juls
 
That's strange! Try this:
Code:
<script language="javascript">
  function ConfirmDel(msg)
  {
    [b]if (confirm(msg) == true)[/b]
      return true;
    else    
      [b]return false[/b];
  }
</script>
Look at the lines in bold. Make sure you (1) compare the confirm function against the true value and (2) return false in the else part of the function. It should work!

JC


Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
I tried that and it still deletes whether true or false. Here is the code for this item, which may shed more light on the problem. I'm afraid it could be the way the CommandEvent Handler is calling the Delete function. Even if ConfirmDel is false, the Event Handler is still firing. How can I check the true/false return of the DataGrid attribute, before firing the Delete? I apologize if this seems straight-forward to you, I'm learning a lot of this as I go...

//On Pageload - sets the javascript ConfirmDel attribute
Code:
String msg = "Are you sure you want to delete this user?";
this.DataGrid1.Attributes.Add("onclick","ConfirmDel('" + msg + "');");

initialize component - this is a datagrid, not button btw
This was originally set up to run without confirmation prior to firing
Code:
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.OnDelete);

//OnDelete function - calls the DeleteUser func from Services
Code:
private void OnDelete(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
	DataSet ds = new DataSet();
        string cs = GetConnectString();
        VESServices.Authentication  auth = new  VESServices.Authentication(cs);

        if (auth != null)
	{
	   string sc = CreateSortClause();
           ds = auth.DeleteUser (e.Item.Cells[2].Text, sc);
	   DataGrid1.DataSource = ds;
	   DataGrid1.DataBind();
	 }
 }

Thank You!
Juls
 
Juls,
Don't worry about whether somethings seems straight forward or not; we're here to help each other so everything is kosher!

Alright, so where were we? Oh yes, about your user being deleted anyways...Well, what we're trying to do is prevent the form from being submitted to the server so that the code in the server that deletes the user won't have a chance to run. This is accomplished by returning false from a javascript function that runs on, say, the click event of a control. You're saying, though, that the postback succeeds regardless of what value is returned from the javascript function, so that puzzles me a bit and leaves me thingking of two possible causes for the problem:

1 - The javascript function is not really returning false so the postback succeeds all the time. To check if this is true, I would update the javascript function so that it (temporarily) only returns false regardless of what the user responds to the "Are you sure..." question. If this doesn't delete the user, it means we have to look more closely at the javascript function itself and make sure it returns the correct value (true/false).

OR

2 - The datagrid is firing its DataGridCommand event regardless and it's ignoring the value returned by the javascript function. I haven't tried stoping a postback triggered by the "onclick" of a datagrid but I don't see why it would be any different from any of the other controls. In any case, try the following: Add a dummy button to your page; bind its "onclick" property to the ConfirmDel javascript function, and try the delete operation from the button, not from the grid. On the server code-behind, add the deleting logic to the button as well, and then try deleting the user. If the delete is attempted from the button and answering "Cancel" to the "Are you sure..." question stops the postback, it means we need another way to cancel the postback because returning false from the javascript is not doing it for the datagrid. I'm unaware of any other way to stop the postback, but we'll see what se come up with.

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
I tried your suggestions and it appears that the event is firing regardless of the return value of the javascript function. I set the javascript function to return false no matter what, then created a button with the delete event:
Code:
this.MyTestDeleteButton.Click += new System.EventHandler(this.OnDelete);

Even with the javascript function returning false, the delete event still fired.

Thanks for your help so far. Any other suggestions you have are greatly appreciated!

Thanks,
Juls
 
Juls
I got it!!!

Put the return keyword right on the onclick attribute. Like this:

btn.Attributes.Add("onclick", "return ConfirmDel(...);");

And also, if you want, you could modify the javascript function ConfirmDel and make it be simply this:
Code:
function ConfirmDel(msg)
{
   return confirm(msg);
}
Just make sure you add that "return" on the onclick attribute. It works very nicely!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
Thank You! Thank You! Thank You!

That was it! I appreciate the time and effort you spent to help me out. You're a genius! [sunshine]

Thanks!
Juls
 
I'm trying to adapt this solution to be used with the checking of a checkbox, rather than the pushing of a button:

cbLock.Attributes.Add("CheckedChanged", "return ConfirmDel('" + msg + "');");

I'm trying the above, but for some reason, this event (CheckedChanged) doesn't seem to fire/be recognized.
I have autopostback enabled - still no dice.
 
silly question... but is your checkbox autopostback enabled??
 
There isn't a client side checkchanged event you would need to use
Code:
cbLock.Attributes.Add("onchange", "return ConfirmDel('" + msg + "');");

Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
This doesn't seem to register either... nothing fires when I check the checkbox.

Is it easier/better to do this in the HTML?
 
You could always use onclick. That definitely works for checkboxes and you can then test the checked property to see if you needed to run the Confirm.

Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top