後台動態設定前台標籤內容和屬性

來源:互聯網
上載者:User

和以前的asp不同,在asp.net中為了徹底的代碼分離,我們一般不採用<%=%>嵌入標籤中來設定一些屬性和內容。 
一般來說有2種情況: 
(一)設定標籤的內容,比如<title>這裡</title> 
(二)設定標籤的屬性,比如<body bgcolor=這裡> 
(三)動態載入一些script 
先看一下(一)和(二): 
前台 
<title id="mytitle" runat="server"></title> 
<body id="mybody" runat="server"> 
後台 
protected HtmlGenericControl mytitle; 
protected HtmlGenericControl mybody; 
this.mytitle.InnerText="test"; 
this.mybody.Attributes["bgcolor"]="#cccccc"; 
這裡說明3點: 
(1)前台標籤runat=server是必須的 
(2)HtmlGenericControl的命名空間是System.Web.UI.HtmlControls 
(3).InnerText和.InnerHtml是有區別的 
比如: 
前台 
<span runat="server" id="myspan"></span> 
後台 
this.myspan.InnerText="<b>test</b>" 
得到的結果是 
<span id="myspan">&lt;b&gt;test&lt;/b&gt;</span> 
把後台改為 
this.myspan.InnerHtml="<b>test</b>" 
得到的結果是 
<span id="myspan"><b>test</b></span> 
再來看看動態載入一些script 
一般來說有三種情況 
(1)在前台放置<asp:Literal Runat="server"></asp:Literal>指定位置輸出 
(2)後台通過RegisterStartupScript和RegisterClientScriptBlock輸出 
(3)Response.Write輸出 
舉例如下 
(1) 
前台 
<asp:Literal ID="myLiteral" Runat="server"></asp:Literal> 
後台 
this.myLiteral.Text+="<script>alert('test');</script>"; 
查看原始碼可以看到原來的 
<asp:Literal ID="myLiteral" Runat="server"></asp:Literal> 
位置變成了 
<script>alert('test');</script> 
(2) 
為了更加清楚看到兩個的區別,前台如下 
<form id="Form1" method="post" runat="server"> 
<input type="hidden" value="test" id="myhiden"> 
</form> 
後台如下 
Page.RegisterStartupScript("","<script>alert('RegisterStartupScript:'+document.all.myhiden.value)</script>"); 
Page.RegisterClientScriptBlock("","<script>alert('RegisterClientScriptBlock:'+document.all.myhiden.value)</script>"); 
結果如下:只出現對話方塊顯示RegisterStartupScript:test 
查看原始碼如下: 
<form name="Form1" method="post" action="WebForm4.aspx" id="Form1"> 
<input type="hidden" name="__VIEWSTATE" value="dDwtNjU0MzcyMTk1Ozs+NmbLf6dWkF/Q/FLKKsdPPfhFXr0=" /> 
<script>alert('RegisterClientScriptBlock:'+document.all.myhiden.value)</script> 
<input type="hidden" value="test" id="myhiden"> 
<script>alert('RegisterStartupScript:'+document.all.myhiden.value)</script> 
</form> 
兩者區別不用多說了吧 
(3) 
我們再加一句Response.Write("test") 
查看原始碼發現test字樣出現在最前面 
test 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 
用它來輸出轉向代碼比較合適 
<script>location.href='page.aspx'</script> 

最後在提醒一下大家,有很多人在給一個控制項加js代碼的時候會這麼寫: 
this.mybody.Attributes["onclick"]="<script>alert('test');</script>"; 
這麼寫是不對的 
應該是 
this.mybody.Attributes["onclick"]="alert('test');"; 
比如你在前台書寫<a onclick=>的時候你會寫<a onclick='<script>alert('test')<script>'>嗎? 
道理是一樣的! 
但是相反,有的人在 
Page.RegisterStartupScript("","<script>alert('test');</script>");的時候不寫裡面的<script>也是不行的 
(雖然名字叫Register...Script) 

原文地址:http://www.cnblogs.com/lovecherry/archive/2005/03/25/125485.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.