Shaitan00 Posted March 26, 2005 Posted March 26, 2005 --Using [VS ASP.NET]-- This is a really simple question... How do you make a message box that has a YES/NO and returns a boolean depending on the users response. Specifically I want to prompt the user to confirm if he wants to delete the seletected item. So like if (messagebox.response) then XXX else YYY type of Pseudo-code. I am sure this is rather trivial just couldn't find any clear examples. Thanks, Quote
kahlua001 Posted March 26, 2005 Posted March 26, 2005 Use javascript in your page <script language="javascript"> function confirm_delete() { if (confirm("Are you sure you want to delete this item?")) return true; else return false; } </script> On a control, add a javascript call, like... onClick="return confirm_delete();" Quote
Leaders snarfblam Posted March 26, 2005 Leaders Posted March 26, 2005 <script language="javascript"> function confirm_delete() { if (confirm("Are you sure you want to delete this item?")) return true; else return false; } </script> [/Quote] I'm not sure about this, but would the confirm() function itself not return a boolean? You are using it as the condition for an if statement. If for some reason you really want to throw it into a function, couldn't you just do function confirm_delete() { return (confirm("Are you sure you want to delete this item?")); } Quote [sIGPIC]e[/sIGPIC]
Mister E Posted March 27, 2005 Posted March 27, 2005 Here's a static function I use:public static void AddConfirmDialog(System.Web.UI.WebControls.WebControl ctrl, string msg) { ctrl.Attributes.Remove("onClick"); ctrl.Attributes.Add("onClick","return confirm('" + msg + "');"); }Just feed in a button or other WebControl and then specify your message. This will fire a JavaScript confirm dialog in a similar manner what was stated in the other posts. Quote
Rattlesnake Posted March 27, 2005 Posted March 27, 2005 Hi, How could I add this messagebox for buttons in a datagrid. I have a datagrid that displays attachments. I want to have a confim message when someone clicks the delete button next to the attachment. Thanks Quote When you gotta go,you gotta go !!!!!!!
kahlua001 Posted March 27, 2005 Posted March 27, 2005 Eitherin your itemcreated or itemdatabound event. If e.item.itemtype = item or .... then Dim btnDelete As Button = e.Item.FindControl("btnDelete") btnDelete.Attributes.Add("onClick","return your_confirm_function();") end if Quote
Rattlesnake Posted March 27, 2005 Posted March 27, 2005 Thanks. I have 2 more questions: 1. How can find what the user selected, Yes or NO 2. How will I find the row from which the button was clicked. In the datagrid I have the first colmn that uniquely identifies the record number. Excuse my ignorance, but I am new to ASP.net. Thanks Eitherin your itemcreated or itemdatabound event. If e.item.itemtype = item or .... then Dim btnDelete As Button = e.Item.FindControl("btnDelete") btnDelete.Attributes.Add("onClick","return your_confirm_function();") end if Quote When you gotta go,you gotta go !!!!!!!
kahlua001 Posted March 28, 2005 Posted March 28, 2005 If the user clicked Yes, then the javascript will continue with the form post, if No, then the js will return back to the browser, in effect, nothing will happen after that. If your item delete event of your datagrid is fired, then you know the user has clicked Yes. To get the datakey of the datagrid item, use the index such as.. intID = Datagrid1.datakeys(e.item.itemindex) Quote
Rattlesnake Posted March 31, 2005 Posted March 31, 2005 Hi, Thanks Kahlua001. It seems to work, but with a small problem. It prompts me for the confirmation on alternate rows only (i.e I get the confirmation questions on the 1st ,3rd ,5th etc rows only), on other rows it deletes without confirmation. It seems in the ItemCreated event it runs only on alternate rows. The Itemcreated event is as below Private Sub dgAttachments_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgAttachments.ItemCreated If e.Item.ItemType = ListItemType.Item Then Dim btnDelete As ImageButton = e.Item.FindControl("btnDelete") btnDelete.Attributes.Add("onClick", "return confirm_delete();") End If End Sub Any reason why?? Quote When you gotta go,you gotta go !!!!!!!
mike55 Posted March 31, 2005 Posted March 31, 2005 Check out the following site: http://www.metabuilders.com/ Has a message box similar to what you are looking for in the free downloads section Mike55 Quote A Client refers to the person who incurs the development cost. A Customer refers to the person that pays to use the product. ------ My software never has bugs. It just develops random features. (Mosabama vbforums.com)
kahlua001 Posted March 31, 2005 Posted March 31, 2005 Also check for alternatingitem If e.Item.ItemType = ListItemType.Item or e.Item.ItemType = ListItemType.AlternatingItem then End If Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.