国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

ASP.NET 2.0: 如何實(shí)現(xiàn)按一個(gè)按鍵在兩種語(yǔ)言之間切換

2024-07-10 13:04:39
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
我要實(shí)現(xiàn)這樣一個(gè)功能,就是按一個(gè)按鍵在兩種語(yǔ)言(比如中文和英文)之間切換。

注:我用的是asp.net beta 2。參考文章列在最后。

參考第一篇文章,"implicit localization expressions"一節(jié)。一個(gè)頁(yè)面(page)的本地化內(nèi)容是在frameworkinitlize()中創(chuàng)建所有的控件時(shí)決定的。比如

   button1.text = ((string)
      base.getlocalresourceobject("linkbuttonresource1.text"));

所以要在frameworkinitlize()之前設(shè)置好你所需要的頁(yè)面的文化(culture).

第二篇文章"page lifecycle"一節(jié)列出了頁(yè)面的生命周期(lifecycle)涉及的方法。下面列出了從頁(yè)面的構(gòu)造函數(shù)(constructor)開(kāi)始與我的問(wèn)題相關(guān)的一些方法。

    constructor
    construct
    testdevicefilter
    addparsedsubobject
    determinepostbackmode
    onpreinit
    loadpersonalizationdata
    initializethemes
    oninit

在第三篇文章"the page lifecycle"的開(kāi)頭寫道:

once the http page handler class is fully identified, the asp.net run time calls the handler's processrequest method to process the request. ... (it) begins by calling the method frameworkinitialize, which builds the controls tree for the page. the method is a protected and virtual member of the templatecontrol class - the class from which page itself derives. any dynamically generated handler for an .aspx resource overrides frameworkinitialize. in this method, the whole control tree for the page is built.

next, processrequest makes the page transit various phases: initialization, loading of view state information and postback data, loading of the page's user code and execution of postback server-side events. after that, the page enters in rendering mode: the updated view state is collected; the html code is generated and then sent to the output console. finally, the page is unloaded and the request is considered completely served.

從這里,我推斷frameworkinitlize()至少是在onpreinit()之前被調(diào)用的。但是仍然看不出來(lái)frameworkinitlize的準(zhǔn)確位置。

繞過(guò)上面所說(shuō)的這些。有兩種方法可以實(shí)現(xiàn)上述的要求。

方法一。在某頁(yè)面的initializeculture方法中設(shè)置你所需要的文化。比如

public partial class _default : system.web.ui.page
{
    protected override void initializeculture()
    {
        string cul = session["uiculture"];
        if (!string.isnullorempty(cul))
        {
            thread.currentthread.currentuiculture =
                new cultureinfo(cul);
        }
    }
}

但這是針對(duì)某一個(gè)頁(yè)面的。要應(yīng)用到整個(gè)網(wǎng)站,就要多一點(diǎn)兒麻煩。要把這個(gè)initializeculture方法寫在一個(gè)system.web.ui.page的子類中,然后讓所有頁(yè)面的類繼承于這個(gè)子類而不是直接繼承system.web.ui.page。

方法二。在prerequesthandlerexecute中設(shè)置你所需要的文化。參見(jiàn)httpapplication類的在線幫助:
an application executes events that are handled by modules or user code that is defined in the global.asax file in the following sequence:
   1. beginrequest
   2. authenticaterequest
   3. postauthenticaterequest
   4. authorizerequest
   5. postauthorizerequest
   6. resolverequestcache
      an event handler (a page corresponding to the request url) is created at this point.
   7. postresolverequestcache
   8. postmaprequesthandler
   9. acquirerequeststate
  10. postacquirerequeststate
  11. prerequesthandlerexecute
      the event handler is executed.
  12. postrequesthandlerexecute
  13. ...

我試過(guò) beginrequest, 但是不行。我想原因是因?yàn)閜age.request在此之后才被創(chuàng)建或賦值,所以在beginrequest中設(shè)置的文化會(huì)被覆蓋了。直到 10. postacquirerequeststate,才被準(zhǔn)備好,只有在此之后我們才可以設(shè)置所需要的文化。
11和12之間的 "the event handler is executed."就應(yīng)該是整個(gè)頁(yè)面的生命周期的處理了。方法一使用initializeculture就是在這里。

這樣就能夠達(dá)到最初的目的了。但是還有一個(gè)小問(wèn)題。按鍵的事件處理函數(shù)只會(huì)在設(shè)置了線程的文化之后才會(huì)被執(zhí)行(不論是用prerequesthandlerexecute,抑或initializeculture)。結(jié)果就是在頁(yè)面的html傳到瀏覽器時(shí),所要設(shè)的文化與線程的文化不一樣。而在其他的事件發(fā)生時(shí),由于所要設(shè)的文化沒(méi)有改變,就會(huì)和線程的文化一樣。這樣程序的行為就不一致了,容易使用戶產(chǎn)生混淆。有一個(gè)不太完善的結(jié)局方法,就是在按鍵的事件處理函數(shù)最后加上下面這句,這樣用戶就能立刻看到改變了的語(yǔ)言了。

response.redirect(request.path);

缺憾是由于當(dāng)前頁(yè)面要重新生成,原先的狀態(tài)(比如用戶已經(jīng)輸入的東西)就不會(huì)保留了。

另外還有一個(gè)與此相關(guān)的問(wèn)題,就是如何本地化sitemap(網(wǎng)頁(yè)導(dǎo)航?)。請(qǐng)參考下面這篇文章。
asp.net 2.0: 在使用web.sitemap時(shí),如何本地化網(wǎng)頁(yè)導(dǎo)航(sitemap)



收集最實(shí)用的網(wǎng)頁(yè)特效代碼!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 洛扎县| 积石山| 陈巴尔虎旗| 广饶县| 永顺县| 丰顺县| 右玉县| 尼勒克县| 镶黄旗| 昭通市| 三门县| 治多县| 天峻县| 衡山县| 文水县| 合山市| 绥中县| 南郑县| 东乡县| 甘洛县| 阿克| 钟祥市| 太原市| 兴安县| 金阳县| 郧西县| 布拖县| 乌拉特前旗| 惠安县| 贵州省| 稻城县| 肥东县| 高台县| 镇安县| 郓城县| 高阳县| 平山县| 班玛县| 平武县| 平和县| 康保县|