-
confirmation box
In my dataGrid I have a delete button which deletes the record that is clicked. I want a confirmation box to pop-up to confirm the record before it is deleted. The delete button in the datagrid does not have an onclick event, so I'm using the OnDeleteCommand to call my procedure. What I don't understand is, the delete button in the datagrid does not have an ID or name, so how do I call it if I use
btnName.Attributes.Add("onclick", "javaScript:window.
does anyone have an example of this?
thanks,
eye
-
Assuming your DataGrid was named Descriptions, add an onItemDataBound event handler and include the following. This assumes the submit button for the DELETE command is the sixth control within a row. If not, just change the 5 below to the appropriate number:
Descriptions.Controls.Add(New LiteralControl("<SCRIPT FOR=""" & e.Item.Controls(5).ClientID.ToString() & """ EVENT=""onclick"" Language=""JavaScript"">if(!confirm('Are you sure?')){return false; window.event.cancelBubble=true;}</SCRIPT>" & vbCrLf))
-
I have 5 fields in my row, so I made the index 4, assuming it began from zero. I'm not getting any errors, but I'm also not getting a message box. I debugged and it is going through the code. I'm not using the itemDataBound to fire off my procedure. I'm using onDeleteCommand. Could this be an issue?
Public Sub DeleteBid(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand
DataGrid1.Controls.Add(New LiteralControl("<SCRIPT FOR=""" & e.Item.Controls(4).ClientID.ToString() & """ EVENT=""onclick"" Language=""JavaScript"">if(!confirm('Are you sure?')){return false; window.event.cancelBubble=true;}</SCRIPT>" & vbCrLf))
<asp:datagrid id=DataGrid1 DataKeyField="BID_ID" EnableViewState="False" onDeleteCommand="DeleteBId" style="Z-INDEX: 107; LEFT: 25px; POSITION: absolute; TOP: 203px" runat="server" Height="244px" Width="724px" Font-Names="Courier New" DataSource="<%# DataSet11 %>" DataMember="PBSS_BID" AutoGenerateColumns="False">
-
Without the above code in your project. Check the source code of the delete link from the browser source. I suspect that asp.net creates a javascript function to run when you click delete and that when you add an onclick attribute you may be causing problems with the link now having two onclick events.
-
To add the JavaScript I posted, you have to do it in the onItemDataBound.
You will want to check the current row index against the row index if the edit row and if they are the same, then use the code. You don't want a confirmation box on the 5th item of EVERY row, just that one with the button :D