ASP.NET如何动态生成JS文件,如何读写该JS文件 100
1个回答
展开全部
获得页面所有的控件
根据控件的类型,去脚本配置文件匹配对应的脚本文件名,然后输出脚本文件
如果这个脚本文件已经生成那么则不必重复生成
配置文件类似
<?xml version="1.0" encoding="utf-8" ?>
<javascript-reference>
<!-- 脚本文件对应的名称以及路径 -->
<file-name name="Table" src="js/Table.js"></file-name>
<file-name name="Input" src="js/Input.js"></file-name>
<file-name name="ControlUtils" src="js/ControlUtils.js"></file-name>
<file-name name="Utils" src="js/Utils.js"></file-name>
<file-name name="WebUtils" src="js/WebUtils.js"></file-name>
<!-- 脚本关联文件 -->
<file-reference name="Table">
<file-name name="ControlUtils"></file-name>
<file-name name="Utils"></file-name>
<file-name name="WebUtils"></file-name>
</file-reference>
<file-referenct name="Input">
<file-name name="ControlUtils"></file-name>
<file-name name="WebUtils"></file-name>
</file-referenct>
<!--控件-->
<component name="Table" class="" file-name=""></component>
<component name="ProlinkInput" class="" file-name=""></component>
</javascript-reference>
最后能够自动在页面中生成类似
<script language="javascript" src="../js/Math.js" ></script>
<script language="javascript" src="../js/Utils.js"></script>
<script language="javascript" src="../js/DataSource.js"></script>
<SCRIPT language="javascript" src="../js/ControlUtils.js"></SCRIPT>
<script language="javascript" src="../js/Table.js"></script>
<script language="javascript" src="../js/Input.js"></script>
<script language="javascript" src="../js/BtnTextInput.js"></script>
<script language="javascript" src="../js/Label.js"></script>
<script language="javascript" src="../js/Lookup.js"></script>
<script language="javascript" src="../js/Body.js"></script>
<script language="javascript" src="../js/WebUtils.js"></script>
---------------------------------------------------------------
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("<script language="javascript" src="../js/{0}.js"></script>", 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
{
/// <summary>
/// CreateClientScriptBlock 的摘要说明。
/// </summary>
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 = "<script language=\"javascript\" src=\"../js/" + sFileName + "\"></script>";
if (!_page.IsClientScriptBlockRegistered(sTypeName)){
_page.RegisterClientScriptBlock(sTypeName,sScript);
}
}
}
}
then do in your aspx code behind:
new ProlinkClass.CreateClientScriptBlock(Page).OutputScripts();
根据控件的类型,去脚本配置文件匹配对应的脚本文件名,然后输出脚本文件
如果这个脚本文件已经生成那么则不必重复生成
配置文件类似
<?xml version="1.0" encoding="utf-8" ?>
<javascript-reference>
<!-- 脚本文件对应的名称以及路径 -->
<file-name name="Table" src="js/Table.js"></file-name>
<file-name name="Input" src="js/Input.js"></file-name>
<file-name name="ControlUtils" src="js/ControlUtils.js"></file-name>
<file-name name="Utils" src="js/Utils.js"></file-name>
<file-name name="WebUtils" src="js/WebUtils.js"></file-name>
<!-- 脚本关联文件 -->
<file-reference name="Table">
<file-name name="ControlUtils"></file-name>
<file-name name="Utils"></file-name>
<file-name name="WebUtils"></file-name>
</file-reference>
<file-referenct name="Input">
<file-name name="ControlUtils"></file-name>
<file-name name="WebUtils"></file-name>
</file-referenct>
<!--控件-->
<component name="Table" class="" file-name=""></component>
<component name="ProlinkInput" class="" file-name=""></component>
</javascript-reference>
最后能够自动在页面中生成类似
<script language="javascript" src="../js/Math.js" ></script>
<script language="javascript" src="../js/Utils.js"></script>
<script language="javascript" src="../js/DataSource.js"></script>
<SCRIPT language="javascript" src="../js/ControlUtils.js"></SCRIPT>
<script language="javascript" src="../js/Table.js"></script>
<script language="javascript" src="../js/Input.js"></script>
<script language="javascript" src="../js/BtnTextInput.js"></script>
<script language="javascript" src="../js/Label.js"></script>
<script language="javascript" src="../js/Lookup.js"></script>
<script language="javascript" src="../js/Body.js"></script>
<script language="javascript" src="../js/WebUtils.js"></script>
---------------------------------------------------------------
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("<script language="javascript" src="../js/{0}.js"></script>", 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
{
/// <summary>
/// CreateClientScriptBlock 的摘要说明。
/// </summary>
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 = "<script language=\"javascript\" src=\"../js/" + sFileName + "\"></script>";
if (!_page.IsClientScriptBlockRegistered(sTypeName)){
_page.RegisterClientScriptBlock(sTypeName,sScript);
}
}
}
}
then do in your aspx code behind:
new ProlinkClass.CreateClientScriptBlock(Page).OutputScripts();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询