第一個問題說到底也不是什么問題,主要是看個人喜歡。在瀏覽器里查看頁面源代碼時,也許就成了問題了,源代碼很不整潔,要是內(nèi)容多時問題就更突出。本來想找腳本,卻在head標(biāo)簽里找不到,只能到其它標(biāo)簽里找了。(不知道有沒有哪些開發(fā)工具在查看源代碼時可以把它們都區(qū)分開來,以方便查找)
注冊資源文件
/// summary>
/// 注冊資源文件
/// /summary>
/// param name="path">路徑/param>
/// param name="key">要搜索的客戶端資源的鍵,防止/param>
/// param name="type">資源文件類型/param>
public void RegisterResource(string path, string key, ResType type)
{
string resStr = string.Empty;
switch (type)
{
case ResType.Js:
resStr = string.Format("script type=\"text/javascript\" language=\"javascript\" src=\"{0}\">/script>", path);
break;
case ResType.Css:
resStr = string.Format("link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />", path);
break;
}
//是否已輸出
if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), key))
{
if (Page.Header != null)
{
LiteralControl link = new LiteralControl();
link.Text = "\r\n" + resStr;
Page.Header.Controls.Add(link);
}
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), key, "", false);//注冊資源key
}
}
注冊腳本塊(或者樣式塊)
/// summary>
/// 注冊腳本塊(或者樣式塊)
/// /summary>
/// param name="script">/param>
/// param name="key">/param>
/// param name="type">/param>
public void RegisterScript(string script, string key)
{
//是否已輸出
if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), key))
{
if (Page.Header != null)
{
LiteralControl link = new LiteralControl();
link.Text = "\r\n" + script;
Page.Header.Controls.Add(link);
}
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), key, "", false);//注冊資源key
}
}
body {
}
div { height:200px; background-color:Blue}
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
html xmlns="http://www.w3.org/1999/xhtml">
head runat="server">
title>/title>
/head>
body>
form id="form1" runat="server">
div>
/div>
/form>
/body>
/html>