惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
量子位
大猫的无限游戏
大猫的无限游戏
Hugging Face - Blog
Hugging Face - Blog
S
SegmentFault 最新的问题
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
Google DeepMind News
Google DeepMind News
小众软件
小众软件
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
MongoDB | Blog
MongoDB | Blog
B
Blog RSS Feed
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
雷峰网
雷峰网
Jina AI
Jina AI
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - Sunny

开发人员一定要加入收藏夹的网站(转载) 我写的一个小程序 tabs研究 dotnetnuke( modules /tabs )权限研究 数据库知识2 数据库学习1 asp.net缓存、企业程序库缓存及格式化日期 - Sunny - 博客园 可爱的外甥女 dotnetnuke 里profile 的使用 怎样写自己的ConfigurationHandler(DOTNETNUKE 研究)? 编写upload程序 多国技术总结 远程教育系统总结 项目(多国技术) 总结 循环遍历一个控件方法 页面验证码程序(C#) string与stringbuilder flyweigth享元设计模式与委托总结 怎样获取获取GridView里面的label
dotnetnuke 本地化技术杂谈
Sunny · 2006-03-27 · via 博客园 - Sunny

        <asp:Label ID="Label2" runat="server"   Text="Input the Company Name" resourcekey="Label2Resource1"></asp:Label>

label控件并没有resourcekey属性,而在dotnetnuke里面却可以根据这个属性获取资源文件里面相应的数据,这是为什么呢?
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控件,设
label.Text=value,处理方法为:

Friend Sub ProcessControl(ByVal c As Control, ByVal affectedControls As ArrayList, ByVal includeChildren As BooleanByVal ResourceFileRoot As String)

获取resourcekey的value的方法是:

                value = Services.Localization.Localization.GetString(key, ResourceFileRoot)

而怎样获取方法中的key呢?比如就是获取顶端的resoucekey的值Label2Resource1,方法如下:

          Dim key As String = GetControlAttribute(c, affectedControls)

方法完整代码如下:

 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 = NothingIf 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”。

就这么多了,
然后简要介绍dotnetnuke本地化方法:
如何使用aspx页面本地化资源文件?
直接在控件中加resourcekey属性,如:

        <asp:Label ID="Label2" runat="server"   Text="Input the Company Name" resourcekey="Label2Resource1"></asp:Label><br />

resourcekey可以为例中的Lable2Resource1,也可以为Lable2Resource1.Text。

如何使用用户控件ascx页面的资源文件?
使用方法:

        Label2.Text = Services.Localization.Localization.GetString("label2", Services.Localization.Localization.GetResourceFile(Me, MyFileName))

即可。