lundi 8 août 2016

How to get TagName of Control if exists?

I have written this code:

    If (AlohaEnabled) Then
        Dim head As Control = Nothing
        For Each control In Master.Controls
            Dim field = control.GetType.GetField("TagName")
            If ((field IsNot Nothing) AndAlso (field.GetValue(control).Equals("head"))) Then
                'Add aloha scripts
            End If
        Next
    End If

If AlohaEnabled is True, then I intend to add some links and scripts to the head tag. I do not know in advance what kind of Master will be used, therefore I iterate its Controls and look for a field called TagName through reflection. If field has a value, then I compare it to "head" and if it is a match, then I intend to add aloha scripts (the question is more general though, I could need this for different scripts as well or somewhere else). TagName is a field of System.Web.UI.HtmlControls.HtmlControl. The 0'th control in my test case returns

{Name = "HtmlHead" FullName = "System.Web.UI.HtmlControls.HtmlHead"}

on control.GetType. If we look at System.Web.UI.HtmlControls.HtmlHead, we see that it inherits System.Web.UI.HtmlControls.HtmlGenericControl, which on its turn inherits System.Web.UI.HtmlControls.HtmlContainerControl, which inherits from HtmlControl. Since TagName is Public, I would expect control.GetType.GetField("TagName") to Return "head". Instead of that, it returns Nothing. I wonder what is the cause of this behavior?





Aucun commentaire:

Enregistrer un commentaire