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

首頁 > 編程 > .NET > 正文

ASP.NET 2.0中動態修改頁面標題

2024-07-10 13:10:46
字體:
來源:轉載
供稿:網友

在老外的站上看到解決的好方法,故簡單編譯之:
在一個asp.net 的應用中,經常要動態修改頁面的標題,一個典型的例子就是,在一個頁面導航的控件中,希望用戶點選哪一個連接,在頁面的title里就顯示相關的內容,舉個例子,比如一個網站,有如下的網站架構:
有圖書分類,下面再有中國圖書,外國圖書分類,則一般可以用樹形或者asp.net 2.0的新增加的導航欄控件
(sitemap),來實現,比如

圖書--->中國圖書;
圖書---->外國圖書
等,而如果這個時候,能在頁面的<title>部分,也能顯示比如"圖書-->中國圖書"這樣,那就更加直觀明顯了,
在asp.net 2.0中,我們可以使用<head>部分的服務端控件來實現了,首先,要添加標記
<head runat="server">

然后可以在page_load事件中,以如下形式改邊其title的內容了,如
page.header.title = "the current time is: " & datetime.now.tostring()
,也可以簡單寫成page.title.

然后,我們可以通過這樣的辦法,將其于sitemap控件結合了,實現方法如下:

const default_unnamed_page_title as string = "untitled page"
    const default_page_title as string = "welcome to my website!!"

    protected sub page_load(byval sender as object, byval e as system.eventargs) handles me.load
        'set the page's title, if needed
        if string.isnullorempty(page.title) orelse page.title = default_unnamed_page_title then
            if sitemap.currentnode is nothing then
                page.title = default_page_title
            else
                page.title = getpagetitlebasedonsitenavigation()

                'can also use the following if you'd rather
                'page.title = getpagetitlebasedonsitenavigationusingrecursion(sitemap.currentnode)
            end if
        end if
    end sub

    private function getpagetitlebasedonsitenavigation() as string
        if sitemap.currentnode is nothing then
            throw new argumentexception("currentnode cannot be nothing")
        end if

        'we are visiting a page defined in the site map - build up the page title
        'based on the site map node's place in the hierarchy

        dim output as string = string.empty
        dim currentnode as sitemapnode = sitemap.currentnode

        while currentnode isnot nothing
            if output.length > 0 then
                output = currentnode.title & " :: " & output
            else
                output = currentnode.title
            end if

            currentnode = currentnode.parentnode
        end while

        return output
    end function

  在上面的代碼中,首先預定義了兩個常量,然后逐步建立sitemap的結點,一開始結點是null的,然后再調用getpagetitlebasedonsitenavigation() 這個過程,在每建立一個sitemap的結點時,用字符串進行連接,最后返回給page.title即可實現,當然也可以用遞歸實現.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天峻县| 厦门市| 奉贤区| 年辖:市辖区| 平顺县| 绵竹市| 霍林郭勒市| 珠海市| 垣曲县| 徐汇区| 柘荣县| 方正县| 河北省| 鄯善县| 荣昌县| 五大连池市| 阳江市| 康马县| 临汾市| 灯塔市| 琼结县| 鄂尔多斯市| 太原市| 丁青县| 利津县| 南川市| 荆州市| 彝良县| 和政县| 达孜县| 惠水县| 买车| 晋中市| 裕民县| 筠连县| 金寨县| 元氏县| 南丰县| 全州县| 河池市| 通江县|