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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating a daragrid's delete

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
How do I add an 'Are you sure you want to delete this record' to the datagrid's delete event?

I have no clue how to embed javascript into the asp.net code.

I read the FAQ, and it was not clear to me how to do this.

Thanks
 
What exactly about the FAQ did you not understand? What current code do you have, and why is not working like Alcar showed?

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
I am using Web Matrix while waiting for Microsoft to release Visual Studio 2003 next week.

Where do I put this?

private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType lit = e.Item.ItemType;
if (lit == ListItemType.Item)
{
Button btnDelete = (Button) e.Item.FindControl("btnDelete");
btnDelete.Attributes.Add("OnClick", "javascript: return confirm('Are you sure you want to delete this record?');");
}
}

It looks like javascript code. When I create a new sub called DataGrid1_ItemCreated, and put it in there, it gives me an error.
 
I just took a second look. This is C# code. I am working with VB. Ok, now I need help big time.
 
So it converted it to this:

'VB
Private Sub DataGrid1_ItemCreated(sender As Object, e As System.Web.UI.WebControls.DataGridItemEventArgs)
Dim lit As ListItemType = e.Item.ItemType
If lit = ListItemType.Item Then
Dim btnDelete As Button = CType(e.Item.FindControl("btnDelete"), Button)
btnDelete.Attributes.Add("OnClick", "javascript: return confirm('Are you sure you want to delete this record?');")
End If
End Sub 'DataGrid1_ItemCreated


Now I get this error:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 219: If lit = ListItemType.Item Then
Line 220: Dim btnDelete As Button = CType(e.Item.FindControl("btnDelete"), Button)
Line 221: btnDelete.Attributes.Add("OnClick", "javascript: return confirm('Are you sure you want to delete this record?');")
Line 222: End If

 
Do you have the control named btnDelete on your datagrid?

line 220 is looking for that control, casting it to a button, and then line 221 is assigning the onclick event for that control.

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
OMG!!You are right. No, there is no control of that type. It just says delete. I am using the default code for the grid.

<asp:datagrid id=&quot;DataGrid1&quot; runat=&quot;server&quot; DataKeyField=&quot;OrderID&quot; OnItemCommand=&quot;DataGrid_ItemCommand&quot; OnEditCommand=&quot;DataGrid_Edit&quot; OnUpdateCommand=&quot;DataGrid_Update&quot; OnCancelCommand=&quot;DataGrid_Cancel&quot; OnDeleteCommand=&quot;DataGrid_Delete&quot; AllowPaging=&quot;true&quot; PageSize=&quot;6&quot; OnPageIndexChanged=&quot;DataGrid_Page&quot; ForeColor=&quot;Black&quot; BackColor=&quot;White&quot; CellPadding=&quot;3&quot; GridLines=&quot;None&quot; CellSpacing=&quot;1&quot; width=&quot;80%&quot;>
<FooterStyle backcolor=&quot;#C6C3C6&quot;></FooterStyle>
<HeaderStyle font-bold=&quot;True&quot; forecolor=&quot;White&quot; backcolor=&quot;#4A3C8C&quot;></HeaderStyle>
<PagerStyle font-size=&quot;Smaller&quot; horizontalalign=&quot;Right&quot; backcolor=&quot;#C6C3C6&quot; mode=&quot;NumericPages&quot;></PagerStyle>
<ItemStyle backcolor=&quot;#DEDFDE&quot;></ItemStyle>
<Columns>
<asp:EditCommandColumn ButtonType=&quot;LinkButton&quot; UpdateText=&quot;Update&quot; CancelText=&quot;Cancel&quot; EditText=&quot;Edit&quot;>
<ItemStyle font-size=&quot;Smaller&quot; width=&quot;10%&quot;></ItemStyle>
</asp:EditCommandColumn>
<asp:ButtonColumn Text=&quot;Delete&quot; CommandName=&quot;Delete&quot;>
<ItemStyle font-size=&quot;Smaller&quot; width=&quot;10%&quot;></ItemStyle>
</asp:ButtonColumn>
</Columns>
</asp:datagrid>
 
I think I have found some code at another site that will make it working.... I'll post what I have in a bit.
 
Still nothing! Error again.
I know what's wrong: It's looking at column 0, but it won't let me change it to column 1. What's the deal?

I must be stupid! Thia can't be that hard.


Private Sub DataGrid1_ItemCreated(sender As Object, e As System.Web.UI.WebControls.DataGridItemEventArgs)

Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.EditItem
Dim myTableCell As TableCell
myTableCell = e.Item.Cells(0)
Dim myDeleteButton As LinkButton
myDeleteButton = myTableCell.Controls(0)
myDeleteButton.Attributes.Add(&quot;onclick&quot;,&quot;return confirm('Are you Sure you want to delete this company?');&quot;)

End Select

End Sub 'DataGrid1_ItemCreated


<html>
<head>
</head>
<body style=&quot;FONT-FAMILY: arial&quot;>
<h2>Editable Data Grid
</h2>
<hr size=&quot;1&quot; />
<form runat=&quot;server&quot;>
<asp:datagrid id=&quot;DataGrid1&quot; runat=&quot;server&quot; DataKeyField=&quot;OrderID&quot; OnItemCommand=&quot;DataGrid_ItemCommand&quot; OnEditCommand=&quot;DataGrid_Edit&quot; OnUpdateCommand=&quot;DataGrid_Update&quot; OnCancelCommand=&quot;DataGrid_Cancel&quot; OnDeleteCommand=&quot;DataGrid_Delete&quot; AllowPaging=&quot;True&quot; OnPageIndexChanged=&quot;DataGrid_Page&quot; BackColor=&quot;White&quot; CellPadding=&quot;3&quot; GridLines=&quot;Vertical&quot; width=&quot;80%&quot; OnItemCreated=&quot;DataGrid1_ItemCreated&quot; BorderWidth=&quot;1px&quot; BorderColor=&quot;#999999&quot; BorderStyle=&quot;None&quot;>
<FooterStyle forecolor=&quot;Black&quot; backcolor=&quot;#CCCCCC&quot;></FooterStyle>
<HeaderStyle font-bold=&quot;True&quot; forecolor=&quot;White&quot; backcolor=&quot;#000084&quot;></HeaderStyle>
<PagerStyle font-size=&quot;Smaller&quot; horizontalalign=&quot;Center&quot; forecolor=&quot;Black&quot; backcolor=&quot;#999999&quot; mode=&quot;NumericPages&quot;></PagerStyle>
<SelectedItemStyle font-bold=&quot;True&quot; forecolor=&quot;White&quot; backcolor=&quot;#008A8C&quot;></SelectedItemStyle>
<AlternatingItemStyle backcolor=&quot;Gainsboro&quot;></AlternatingItemStyle>
<ItemStyle forecolor=&quot;Black&quot; backcolor=&quot;#EEEEEE&quot;></ItemStyle>
<Columns>
<asp:EditCommandColumn ButtonType=&quot;LinkButton&quot; UpdateText=&quot;Update&quot; CancelText=&quot;Cancel&quot; EditText=&quot;Edit&quot;>
<ItemStyle font-size=&quot;Smaller&quot; width=&quot;10%&quot;></ItemStyle>
</asp:EditCommandColumn>
<asp:ButtonColumn Text=&quot;Delete&quot; CommandName=&quot;Delete&quot;>
<ItemStyle font-size=&quot;Smaller&quot; width=&quot;10%&quot;></ItemStyle>
</asp:ButtonColumn>
</Columns>
</asp:datagrid>
<br />
<asp:LinkButton id=&quot;LinkButton1&quot; onclick=&quot;AddNew_Click&quot; runat=&quot;server&quot; Text=&quot;Add new item&quot; Font-Size=&quot;smaller&quot;></asp:LinkButton>
<br />
<br />
<asp:Label id=&quot;Message&quot; runat=&quot;server&quot; ForeColor=&quot;red&quot; width=&quot;80%&quot; EnableViewState=&quot;false&quot;></asp:Label>
</form>
</body>
</html>
 
I GOT IT! I GOT IT! I GOT IT! I GOT IT! I GOT IT! I GOT IT!

Here's the reason it did not work: I needed to change the lines in the DataGrid1_ItemCreated Event..

From

Dim myDeleteButton As LinkButton
myDeleteButton = myTableCell.Controls(0)
myDeleteButton.Attributes.Add(&quot;onclick&quot;,&quot;return confirm('Are you Sure you want to delete this company?');&quot;)

To
Dim mydeleteButton As LinkButton = e.Item.Cells(1).Controls(0)
myDeleteButton.Attributes.Add(&quot;onclick&quot;,&quot;return confirm('Are you Sure you want to delete this company?');&quot;)

I HAD to reference Cells(1), and not Cells(0).

Ahhhhhhhhhhhhhhhhh, no hair left, but project is 1 more step to finish.


 
And there's only a slight change when using real buttons too.

Thank you all for the help. [2thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top