|
-
Apr 22nd, 2004, 10:30 AM
#1
Thread Starter
Hyperactive Member
RESOLVED!! VB.NET equivalent
I have this code in a tutorial but was coded in c#. What is the equivalent code in VB.NET?
VB Code:
protected void myDataGrid_OnItemDataBound(object sender, DataGridItemEventArgs e)
{
if(e.Item.FindControl("DeleteLink") != null)
{
((LinkButton) e.Item.FindControl("DeleteLink")).Attributes.Add("onClick", "return confirm('Are you sure you wish to delete this item?');");
}
}
Basically I have a DataGrid with a DELETE linkbutton on the first column (id=DeleteMe) as a template. When user clicks on the DELETE link, it is suppose to prompt a confirmation.
I tried converting to VB but I am getting an error Specified argument was out of the range of valid values. Parameter name: index
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.
VB Code:
Sub dgCustomer_OnItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
If Not IsDBNull(e.Item.FindControl("DeleteMe")) Then
Dim deleteButton As LinkButton = e.Item.Cells(0).Controls(0) '<-- ERROR HERE
deleteButton.Attributes("onclick") = "javascript:return Confirm('Are you sure you want to delete this record?');"
End If
End Sub
Last edited by ARPRINCE; Apr 23rd, 2004 at 11:21 AM.
-
Apr 22nd, 2004, 06:26 PM
#2
Frenzied Member
Test for Is Nothing...
Try this and maybe try a 1 with the array index...
VB Code:
Sub dgCustomer_OnItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
Dim deleteButton As LinkButton = e.Item.Cells(0).FindControl("DeleteMe")
If Not deleteButton Is Nothing Then
deleteButton.Attributes("onclick") = "java script:return Confirm('Are you sure you want to delete this record?');"
End If
End Sub
or leave out the array index
VB Code:
Sub dgCustomer_OnItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
Dim deleteButton As LinkButton = e.Item.FindControl("DeleteMe")
If Not deleteButton Is Nothing Then
deleteButton.Attributes("onclick") = "java script:return Confirm('Are you sure you want to delete this record?');"
End If
End Sub
-
Apr 23rd, 2004, 07:51 AM
#3
Thread Starter
Hyperactive Member
Thanks for the syntax tip.
This is what I have on my final code.
VB Code:
Sub dgCustomer_OnItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
If Not IsDBNull(e.Item.FindControl("DeleteMe")) Then
Dim deleteButton As LinkButton
Try
deleteButton = e.Item.FindControl("DeleteMe")
deleteButton.Attributes.Add("OnClick", "javascript:return confirm('Are you sure you want to delete this item?');")
Catch ex As Exception
End Try
End If
End Sub
If I remove the TRY..CATCH..END TRY it craps out though which I can't figure out why. It gives an error Object reference not set to an instance of an object. . SO I have in there and it works just fine.
-
Apr 23rd, 2004, 11:00 AM
#4
Frenzied Member
You're using IsDBNull() wrong. It will always return false even if an object has or hasn't been instantiated...
VB Code:
Dim MyVar As Object
Dim MyCheck As Boolean
MyCheck = IsDBNull(MyVar) ' Returns False.
MyVar = ""
MyCheck = IsDBNull(MyVar) ' Returns False.
MyVar = System.DBNull.Value
MyCheck = IsDBNull(MyVar) ' Returns True.
You need to test for Nothing
VB Code:
Dim str As Object
Dim ret As Boolean
ret = str Is Nothing 'Returns True
str = "abc"
ret = str Is Nothing ' Returns False
Did you try this code?
VB Code:
Sub dgCustomer_OnItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
Dim deleteButton As LinkButton = e.Item.FindControl("DeleteMe")
If Not deleteButton Is Nothing Then
deleteButton.Attributes("onclick") = "java script:return Confirm('Are you sure you want to delete this record?');"
Else
'Did not find the control
Response.Write("Control not found")
End If
End Sub
-
Apr 23rd, 2004, 11:11 AM
#5
Thread Starter
Hyperactive Member
You have been a great help for pointing that out to me. Basically that is the reason why my code was crapping out if I remove the TRY..END TRY.
Thanks a lot!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|