asp.net页面生命周期的理解onprerender

uxspy uxspy 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

/// <summary>
/// 最后一次HTML改动之前注册js,css
/// </summary>
/// <param name="e"></param>
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
AddCss("Control.css.jquery.dd.css");
AddJs("Control.js.jquery.js", "Control.js.jquery.dd.js");
if (this.Page != null)
{
if (!this.Page.ClientScript.IsStartupScriptRegistered(string.Format("{0}_ExtendDropDownInited", this.ClientID)))
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), string.Format("{0}_ExtendDropDownInited", this.ClientID), string.Format("<script>MSDropDown.init('#{0}');</script>", this.ClientID));
}
}
}

/// <summary>
/// 向页面添加css引用
/// </summary>
/// <param name="cssNames"></param>
private void AddCss(params string[] cssNames)
{
if (cssNames is string[])
{
if (this.Page == null || this.Page.Header == null)
{
return;
}

foreach (string cssName in cssNames)
{
if (!this.Page.ClientScript.IsStartupScriptRegistered(string.Format("_bpms_{0}_", cssName)))
{
HtmlLink css = new HtmlLink();

css.Attributes.Add("type", "text/css");
css.Attributes.Add("rel", "stylesheet");
css.Attributes.Add("src", this.Page.ClientScript.GetWebResourceUrl(this.GetType(), cssName));

if (this.Page.Header != null)
{
this.Page.Header.Controls.Add(css);
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), string.Format("_bpms_{0}_", cssName), "");
}
}
}
}
}

/// <summary>
/// 向页面添加js引用
/// </summary>
/// <param name="jsNames"></param>
private void AddJs(params string[] jsNames)
{
if (jsNames is string[])
{
if (this.Page == null || this.Page.Header == null)
{
return;
}

foreach (string jsName in jsNames)
{
if (!this.Page.ClientScript.IsStartupScriptRegistered(string.Format("_bpms_{0}_", jsName)))
{
HtmlGenericControl script = new HtmlGenericControl("script");

script.Attributes.Add("type", "text/css");
script.Attributes.Add("rel", "stylesheet");
script.Attributes.Add("src", this.Page.ClientScript.GetWebResourceUrl(this.GetType(), jsName));

if (this.Page.Header != null)
{
this.Page.Header.Controls.Add(script);
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), string.Format("_bpms_{0}_", jsName), "");
}
}
}
}
}


这样在我生成的页面中,当我用/WebResource.axd?XXXXXXXX请求js时,页面提示的是下载,而不是直接输出在浏览器中,导致我的<script>标签实际上并没有请求到资源,我很奇怪,我不知道是不是我不改把js和css的添加放在prerender事件中?
...全文
评论 7
收藏 已收藏
分享
返回顶部
uxspy
.NET社区 62,274 成员 668,968 内容