Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

--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,

Posted

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();"

  • Leaders
Posted
<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?"));

}

[sIGPIC]e[/sIGPIC]
Posted
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.

Posted

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

When you gotta go,you gotta go !!!!!!!
Posted

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

Posted

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

When you gotta go,you gotta go !!!!!!!
Posted

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)

Posted

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??

When you gotta go,you gotta go !!!!!!!
Posted

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

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)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...