![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
dotnetnuke里的每个aspx页面都是继承DotNetNuke.Framework.CDefault,CDefault同时又继承DotNetNuke.Framework.pagebase类。
cdfault类只包含一些简单的属性,我想应该在pagebase里面做了文章。研究了半天,终于确定的确是在这里实现根据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
通过上面方法循环对页面中的控件进行处理,从资源文件获取resoucekey的value并把他赋给控件,例如label控件,设
For Each c As Control In controls
ProcessControl(c, affectedControls, True , ResourceFileRoot)
Next
End Sub ' IterateControls
label.Text=value,处理方法为:
Friend
Sub
ProcessControl(
ByVal
c
As
Control,
ByVal
affectedControls
As
ArrayList,
ByVal
includeChildren
As
Boolean
,
ByVal
ResourceFileRoot
As
String
)
获取resourcekey的value的方法是:
value
=
Services.Localization.Localization.GetString(key, ResourceFileRoot)
而怎样获取方法中的key呢?比如就是获取顶端的resoucekey的值
Label2Resource1,方法如下:![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
方法完整代码如下:
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 should 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
先验证控件类型,然后获取控件属性集合,从中获取keyname的value,keyname就是“resourcekey”。
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 should 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
就这么多了,
然后简要介绍dotnetnuke本地化方法:
如何使用aspx页面本地化资源文件?
直接在控件中加resourcekey属性,如:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
如何使用用户控件ascx页面的资源文件?
使用方法:
Label2.Text
=
Services.Localization.Localization.GetString(
"
label2
"
,
Services.Localization.Localization.GetResourceFile( Me , MyFileName))
即可。
Services.Localization.Localization.GetResourceFile( Me , MyFileName))