Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im get the following Error -;OfType; is not a member of System.Web.UI.ControlCollection'

This is the code thats gives the error

VB
Protected Sub dgdWebsite_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles dgdWebsite.RowDataBound
        Try
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim item As String = e.Row.Cells(0).Text
                For Each button As Button In e.Row.Cells(7).Controls.OfType(Of Button)()
                    If button.CommandName = "Delete" Then
                        button.Attributes("onclick") = "if(!confirm('Do you want to delete " + item + "?')){ return false; };"
                    End If
                Next
            End If
        Catch ex As Exception
            ShowErrorMsg(ex.Message)
        End Try

    End Sub
Posted
Updated 12-May-17 18:30pm
v3
Comments
Kornfeld Eliyahu Peter 21-Jul-14 10:04am    
What version of .NET do you use?
Sergey Alexandrovich Kryukov 21-Jul-14 11:09am    
Do you really think it will make any difference? Please see my answer.
—SA
Kornfeld Eliyahu Peter 21-Jul-14 14:41pm    
It makes all the difference!
OfType is an extension method that ControlCollection has via IEnumerable but only since version 3.5!!!
Sergey Alexandrovich Kryukov 21-Jul-14 15:16pm    
I got it, thank you for answering.
It could be a reason; prior to v.3.5 (or 3.0?) the extension methods themselves are not available. I credited your comments and adjusted my answer accordingly so the problem could be solved even without having the extension method.
—SA

There is nothing wonderful if some gibberish written in code cause some compilation error. The method OfType applied to a collection of control is a fruit of your fantasy. There is no such thing, as clearly reported by your compiler.

If you wanted to dynamically sort out some objects based on their runtime types, there are two VB.NET operators, Is and TryCast; their use should be obvious from the MSDN help pages:
http://msdn.microsoft.com/en-us/library/kb136x1y.aspx[^],
http://msdn.microsoft.com/en-us/library/zyy863x8.aspx[^].

As to your OfType, there is a generic method System.Linq.Enumerable.OfType<>:
http://msdn.microsoft.com/en-us/library/vstudio/bb360913%28v=vs.100%29.aspx[^].

If you have the version of .NET where it is implemented (please see the comment to the question, thanks to Kornfeld Eliyahu Peter), you can use this method with your collection to System.IEnumerable.

These CodeProject articles could be useful:
Extension Methods in .NET[^],
Extension Methods in VB.NET[^].

But ultimately, in the implementation of the filtering in such a method you would still need to use the Is operator. :-)

—SA
 
Share this answer
 
v5
VB
If e.Row.RowState <> DataControlRowState.Edit Then
               If e.Row.RowType = DataControlRowType.DataRow Then
                   Dim id As String = e.Row.Cells(0).Text
                   Dim lb As ImageButton = DirectCast(e.Row.Cells(6).Controls(0), ImageButton)

                   If lb IsNot Nothing Then
                       lb.Attributes("onclick") = "if(!confirm('Do you want to delete " + id + "?')){ return false; };"
                   End If
                   Dim Sel As ImageButton = DirectCast(e.Row.Cells(7).Controls(0), ImageButton)
                   If Sel IsNot Nothing Then
                       Dim path As String = e.Row.Cells(4).Text
                       Sel.Attributes("onclick") = "window.open('" + path + "','_blank');"
                   End If
               End If
           End If
 
Share this answer
 
Try to import library

Imports System.Web.UI.ControlCollection
Imports System.Linq

I try doint it ,It is work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900