第一个问题说到底也不是什么问题,主要是看个人喜欢。在浏览器里查看页面源代码时,也许就成了问题了,源代码很不整洁,要是内容多时问题就更突出。本来想找脚本,却在head标签里找不到,只能到其它标签里找了。(不知道有没有哪些开发工具在查看源代码时可以把它们都区分开来,以方便查找)
注册资源文件
/// 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>