< ASP: Label ID = "Label2" Runat = "Server" Text = "Input the company name" Resourcekey = "Label2resource1" > </ ASP: Label >
The label control does not have the resourcekey attribute, but in dotnetnuke, the corresponding data in the resource file can be obtained based on this attribute. Why?
Every ASPX page in dotnetnuke inherits dotnetnuke. Framework. cdefault, and cdefault inherits the dotnetnuke. Framework. pagebase class.
The cdfault class only contains some simple attributes. I think it should be done in pagebase. Article . After studying for half a day, I finally confirmed that it was here to obtain the content of the resource file based on resourcekey. Private Overloads Sub Iteratecontrols ( Byval Controls As Controlcollection, Byval Affectedcontrols As Arraylist, Byval Resourcefileroot As String )
For Each C As Control In Controls
Processcontrol (C, affectedcontrols, True , Resourcefileroot)
Next
End sub ' Iteratecontrols
The preceding method cyclically processes the control on the page, obtains the value of resoucekey from the resource file, and assigns it to the control, such as the label control.
Label. Text = value. The processing method is: Friend Sub Processcontrol ( Byval C As Control, Byval Affectedcontrols As Arraylist, Byval Includechildren As Boolean , Byval Resourcefileroot As String )
The method to obtain the value of resourcekey is:Value=Services. Localization. Localization. getstring (Key, resourcefileroot)
How can we get the key in the method? For example, to obtain the value label2resource1 of the top resoucekey, the method is as follows:Dim key as string = getcontrolattribute (C, affectedcontrols)
Complete Method Code As follows: Friend Shared Function Getcontrolattribute ( Byval C As Control, Byval Affectedcontrols As Arraylist) As String
Dim AC As System. Web. UI. attributecollection = Nothing
Dim Key As String = Nothing
If Typeof C Is Literalcontrol Then ' Literalcontrols don't have an attribute collection
Key = Nothing
AC = Nothing
Else
If Typeof C Is Webcontrol Then
Dim W As Webcontrol = Directcast (C, webcontrol)
AC = W. Attributes
Key = AC (services. Localization. Localization. keyname)
Else
If Typeof C Is Htmlcontrol Then
Dim H As Htmlcontrol = Directcast (C, htmlcontrol)
AC = H. Attributes
Key = AC (services. Localization. Localization. keyname)
Else
If Typeof C Is Usercontrol Then
Dim U As Usercontrol = Directcast (C, usercontrol)
AC = U. Attributes
Key = AC (services. Localization. Localization. keyname)
' Use reflection to check for Attribute key. This is a last ditch Option
Else
Dim Controltype As Type = C. GetType ()
Dim Attributeproperty As Propertyinfo = Controltype. getproperty ( " Attributes " , GetType (System. Web. UI. attributecollection ))
If Not Attributeproperty Is Nothing Then
AC = Directcast (Attributeproperty. getvalue (C, Nothing ), System. Web. UI. attributecollection)
Key = AC (services. Localization. Localization. keyname)
End If
End If
End If
End If ' If the key was found add this attributecollection to the list that shocould have the key removed during render
End If
If Not Key Is Nothing And Not Affectedcontrols Is Nothing Then
Affectedcontrols. Add (AC)
End If
Return Key
End Function ' Getcontrolattribute
Verify the control type, obtain the control property set, and obtain the value of the keyname. The keyname is "resourcekey ".
that's all.
next, we will briefly introduce the dotnetnuke Localization Method:
How to Use ASPX page to localize resource files?
directly add the resourcekey attribute to the control, for example, Asp: Label id =" label2 " runat =" server " text =" input the company name " resourcekey =" label2resource1 " > Asp: Label > Br />
Resourcekey can be lable2resource1 or lable2resource1. Text in the example.
How to use resource files on the ascx page of the user control?
Usage:
Label2.text = Services. Localization. Localization. getstring ( " Label2 " , Services. Localization. Localization. getresourcefile ( Me , Myfilename ))
You can.