Click here to Skip to main content
15,915,019 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need get Attributes of all controls (ASP Controls and HTml controls) my code is wrong.


VB
Sub checkControls(controls As ControlCollection)
      
        For Each conrol As Control In controls
            Dim ctr As WebControl = DirectCast(conrol, WebControl)
            If Not IsNothing(ctr.Attributes("data-action")) Then
                    Dim dataAction As String = ctr.Attributes("data-ction").Trim.ToLower
                    If dataAction <> "btn-select" Then
                        conrol.Visible = False
                    End If
                End If

            If conrol.HasControls Then
                checkControls(conrol.Controls)
            End If
        Next
    End Sub
Posted
Comments
Kornfeld Eliyahu Peter 11-Jun-14 10:03am    
Except the error in attribute name (data-action and data-ction), what wrong with your code?
S@53K^S 11-Jun-14 10:10am    
I think there is a slight difference between ctr.Attributes("data-action") and ctr.Attributes("data-"a"ction") an "a" is missing

1 solution

As System.Web.UI.AttributeCollection does not have a public definition for GetEnumerator so the only way that you have is just using Keys property of this class in order to get all attributes' keys
public void CheckControls(ControlCollection controls)
   {
       foreach (Control control in controls)
       {
           WebControl ctr = control as WebControl;
           if (ctr != null)
           {
               foreach (string key in ctr.Attributes.Keys)
               {
                   //Do whatever you want
                   if (key == "sth")
                   {
                       string dataAction = ctr.Attributes[key].Trim().ToLower();
                       control.Visible = false;
                   }
               }
               if (control.HasControls() == true)
                   CheckControls(control.Controls);
           }
       }
   }
 
Share this answer
 

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



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