Label
获得页面所有的控件
根据控件的类型,去脚本配置文件匹配对应的脚本文件名,然后输出脚本文件
如果这个脚本文件已经生成那么则不必重复生成
配置文件类似
<javascript-reference>
javascript-reference>
最后能够自动在页面中生成类似
---------------------------------------------------------------
void GetScriptFile(Control c)
{
OutputScriptFile(c);
foreach(Control cc in c.Controls)
GetScriptFile(cc);
}
void OutputScriptFile(Control c)
{
//if (c is Label)
{
string sTypeName = c.GetType().Name;
if (!Page.IsClientScriptBlockRegistered(sTypeName))
{
Page.RegisterClientScriptBlock(sTypeName, String.Format("", sTypeName));
}
}
....
}
GetScriptFile(Page);
or use Page.RegisterClientScriptBlock method or Page.RegisterStartupScript method, make sure you use
1. give different script different key
2. use Page.IsClientScriptBlockRegistered(ScriptKey) or Page.IsStartupScriptRegistered(ScriptKey) to check
is your aspx derived from deriving from CreateClientScriptBlock??
if not, either pass Page object to your static method or use the HttpContext.Current.Handler, for example
namespace ProlinkClass
{
///
/// CreateClientScriptBlock 的摘要说明。
///
public class CreateClientScriptBlock
{
private Page _page;
public CreateClientScriptBlock(Page p)
{
_page = p;
}
public CreateClientScriptBlock()
{
_page = (Page)System.Web.HttpContext.Current.Handler;
}
public void OutputScripts() { GetScriptFile(_page); }
prviate void GetScriptFile(Control c){
OutputScriptFile(c);
foreach(Control cc in c.Controls){
if (cc.ID != null){
GetScriptFile(cc);
}
}
}
private void OutputScriptFile(Control c){
string sTypeName = c.GetType().Name;
string sFileName = "";
if (sTypeName.Equals("Label"))
sFileName = "Label.js";
else if (sTypeName.Equals("TextBox"))
sFileName = "Input.js";
string sScript = "";
if (!_page.IsClientScriptBlockRegistered(sTypeName)){
_page.RegisterClientScriptBlock(sTypeName,sScript);
}
}
}
}
then do in your aspx code behind:
new ProlinkClass.CreateClientScriptBlock(Page).OutputScripts(); 本文作者:佚名 来源:http://www.bjcan.com
CIO之家 www.ciozj.com 微信公众号:imciow
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读