MessageBox with answer returns

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
--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,
 
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();"
 
kahlua001 said:
<script language="javascript">
function confirm_delete()
{
if (confirm("Are you sure you want to delete this item?"))
return true;
else
return false;
}
</script>
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?"));
}
 
Here's a static function I use:
Code:
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.
 
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
 
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
 
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



kahlua001 said:
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
 
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)
 
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

Code:
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??
 
Back
Top