Results 1 to 5 of 5

Thread: RESOLVED!! VB.NET equivalent

  1. #1

    Thread Starter
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381

    Question 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:
    1. protected void myDataGrid_OnItemDataBound(object sender, DataGridItemEventArgs e)
    2. {
    3.  
    4.   if(e.Item.FindControl("DeleteLink") != null)
    5.   {
    6.     ((LinkButton) e.Item.FindControl("DeleteLink")).Attributes.Add("onClick", "return confirm('Are you sure you wish to delete this item?');");
    7.  
    8.   }
    9. }

    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:
    1. Sub dgCustomer_OnItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
    2.         If Not IsDBNull(e.Item.FindControl("DeleteMe")) Then
    3.             Dim deleteButton As LinkButton = e.Item.Cells(0).Controls(0)   '<-- ERROR HERE
    4.             deleteButton.Attributes("onclick") = "javascript:return Confirm('Are you sure you want to delete this record?');"
    5.         End If
    6.     End Sub
    Last edited by ARPRINCE; Apr 23rd, 2004 at 11:21 AM.

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    Test for Is Nothing...
    Try this and maybe try a 1 with the array index...
    VB Code:
    1. Sub dgCustomer_OnItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
    2.  
    3.     Dim deleteButton As LinkButton = e.Item.Cells(0).FindControl("DeleteMe")
    4.  
    5.     If Not deleteButton Is Nothing Then
    6.         deleteButton.Attributes("onclick") = "java script:return Confirm('Are you sure you want to delete this record?');"
    7.     End If
    8.  
    9. End Sub
    or leave out the array index
    VB Code:
    1. Sub dgCustomer_OnItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
    2.  
    3.     Dim deleteButton As LinkButton = e.Item.FindControl("DeleteMe")
    4.  
    5.     If Not deleteButton Is Nothing Then
    6.         deleteButton.Attributes("onclick") = "java script:return Confirm('Are you sure you want to delete this record?');"
    7.     End If
    8.  
    9. End Sub

  3. #3

    Thread Starter
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381
    Thanks for the syntax tip.

    This is what I have on my final code.

    VB Code:
    1. Sub dgCustomer_OnItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
    2.         If Not IsDBNull(e.Item.FindControl("DeleteMe")) Then
    3.             Dim deleteButton As LinkButton
    4.             Try
    5.                 deleteButton = e.Item.FindControl("DeleteMe")
    6.                 deleteButton.Attributes.Add("OnClick", "javascript:return confirm('Are you sure you want to delete this item?');")
    7.             Catch ex As Exception
    8.             End Try
    9.         End If
    10.     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.

  4. #4
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    You're using IsDBNull() wrong. It will always return false even if an object has or hasn't been instantiated...
    VB Code:
    1. Dim MyVar As Object
    2. Dim MyCheck As Boolean
    3. MyCheck = IsDBNull(MyVar)   ' Returns False.
    4. MyVar = ""
    5. MyCheck = IsDBNull(MyVar)   ' Returns False.
    6. MyVar = System.DBNull.Value
    7. MyCheck = IsDBNull(MyVar)   ' Returns True.
    You need to test for Nothing
    VB Code:
    1. Dim str As Object
    2. Dim ret As Boolean
    3.  
    4. ret = str Is Nothing          'Returns True
    5. str = "abc"
    6. ret = str Is Nothing          ' Returns False
    Did you try this code?
    VB Code:
    1. Sub dgCustomer_OnItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
    2.  
    3.     Dim deleteButton As LinkButton = e.Item.FindControl("DeleteMe")
    4.  
    5.     If Not deleteButton Is Nothing Then
    6.         deleteButton.Attributes("onclick") = "java script:return Confirm('Are you sure you want to delete this record?');"
    7.     Else
    8.         'Did not find the control
    9.         Response.Write("Control not found")
    10.     End If
    11.  
    12. End Sub

  5. #5

    Thread Starter
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381
    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
  •  



Click Here to Expand Forum to Full Width