2007年2月19日 星期一

動態的改變title內容

一般來說,在VS.net 2003中是無法直接更改title內容

為了達成這個目的


必須在html標籤和後端程式碼中動手



當然,CodeInline的寫法極為簡單:

<title><%= "動態指定Document 之 Title"%></title>就行了

這是原本asp和php這一類的寫法,但是既然有了.net當然要試試不一樣的





====== 法一 ======


首先把<head>的<title>標籤改為:

<title runat="server" id="formTitle"></title>


再來是於CodeBehide程式碼的最外層(通常是Page_Load前)中加上

Protected WithEvents formTitle As System.Web.UI.HtmlControls.HtmlGenericControl


這樣在VS.net 2003的後端中就可以抓到這玩意了




接著在觸發事件中加上


formTitle.InnerText = "動態指定Document 之 Title"





這是目前看到CodeBehide中最好的寫法(在1.1算來)

====== 法一結束 ======





====== 法二 ======
自創…偽裝成CodeBehide的CodeInline寫法



<%@ Page Language="VB" %>

<html>

<title><asp:Literal id="Literal1" runat="server"></asp:Literal></title>

<body></body>

</html>



<script language="VB" runat="server">


Sub Page_Load(Src As Object, E As EventArgs)

  public title as string

  Literal1.Text = "123456"

End Sub


</script>

====== 法二結束 ======