您的位置:首页 > Web前端 > JavaScript

js代码动态生成代码(此为一张页面的后端cs代码)

2010-06-10 08:11 561 查看
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;
public partial class Examples_CreatJs_demo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
CreateJsFile("tt",800,300,0);
}
}
protected void CreateJsFile(string jsFileName, int width, int height, int adType)
{
string jsDirctory = Server.MapPath("~") + "/Examples/CreatJs/newJs";
string jsPath = Server.MapPath("~") + "/Examples/CreatJs/newJs/" + jsFileName + ".js";
StringBuilder sb = new StringBuilder();
if (Directory.Exists(jsDirctory))
{
try
{
Directory.CreateDirectory(jsDirctory);
}
catch(Exception msg)
{
throw new Exception(msg.ToString());
}
}
if (File.Exists(jsPath))
{
try
{
File.Delete(jsPath);
}
catch(Exception msg)
{
throw new Exception(msg.ToString());
}
}
sb = GetJs(adType,width,height);
if (sb == null)
{
Response.Write("<mce:script language='javascript'><!--
alert('没有此广告类别或没有对此广告类别进行处理!')
// --></mce:script>");
return;
}
try
{
File.AppendAllText(jsPath, sb.ToString());
//StreamWriter sw = new StreamWriter(jsPath, false, System.Text.Encoding.UTF8);   //根据路径生成文件
}
catch (Exception msg)
{
throw new Exception(msg.ToString());
}

}
protected StringBuilder GetJs(int adType,int width,int height)
{
StringBuilder sb = new StringBuilder();
switch (adType)
{
case 0:
sb = BigSlidePhoto(width, height);
break;
default:
return null;
}
return sb;
}
/// <summary>
/// 大幻灯片广告
/// </summary>
protected StringBuilder BigSlidePhoto(int width, int height)
{
StringBuilder sb = new StringBuilder();
//s += "document.writeln(/"<link type='text/css' rel='stylesheet' href="galleryview.css" mce_href="galleryview.css" />/")/n";
//s += "document.writeln(/"<mce:script type='text/javascript' src="js/jquery-1.4.min.js" mce_src="js/jquery-1.4.min.js"></mce:script>/")/n";
//s += "document.writeln(/"<mce:script type='text/javascript' src="js/jquery.easing.1.3.js" mce_src="js/jquery.easing.1.3.js"></mce:script>/")/n";
//s += "document.writeln(/"<mce:script type='text/javascript' src="js/jquery.galleryview-1.1.js" mce_src="js/jquery.galleryview-1.1.js"></mce:script>/")/n";
//s += "document.writeln(/"<mce:script type='text/javascript' src="js/jquery.timers-1.1.2.js" mce_src="js/jquery.timers-1.1.2.js"></mce:script>/")/n";
sb.Append("document.writeln(/"<div id='photos' class='galleryview'>/")/n");
sb.Append("document.writeln(/"  <div class='panel'>/")/n");
sb.Append("document.writeln(/"     <img src="img/01.jpg" mce_src="img/01.jpg" /> /")/n");
sb.Append("document.writeln(/"    <div class='panel-overlay'>/")/n");
sb.Append("document.writeln(/"      <h2>Effet du soleil sur le paysage</h2>/")/n");
sb.Append("document.writeln(/"      <p>Photo by <a href="http://www.sxc.hu/profile/tomharry" mce_href="http://www.sxc.hu/profile/tomharry" target='_blank'>tomharry</a>.  View full-size photo <a href="http://www.sxc.hu/photo/158829" mce_href="http://www.sxc.hu/photo/158829" target='_blank'>here</a>.</p>/")/n");
sb.Append("document.writeln(/"    </div>/")/n");
sb.Append("document.writeln(/"  </div>/")/n");
sb.Append("document.writeln(/"  <div class='panel'>/")/n");
sb.Append("document.writeln(/"     <img src="img/02.jpg" mce_src="img/02.jpg" /> /")/n");
sb.Append("document.writeln(/"    <div class='panel-overlay'>/")/n");
sb.Append("document.writeln(/"      <h2>Eden</h2>/")/n");
sb.Append("document.writeln(/"      <p>Photo by <a href="http://www.sxc.hu/profile/emsago" mce_href="http://www.sxc.hu/profile/emsago" target='_blank'>emsago</a>.  View full-size photo <a href="http://www.sxc.hu/photo/152865" mce_href="http://www.sxc.hu/photo/152865" target='_blank'>here</a>.</p>/")/n");
sb.Append("document.writeln(/"    </div>/")/n");
sb.Append("document.writeln(/"  </div>/")/n");
sb.Append("document.writeln(/"  <div class='panel'>/")/n");
sb.Append("document.writeln(/"     <img src="img/03.jpg" mce_src="img/03.jpg" /> /")/n");
sb.Append("document.writeln(/"    <div class='panel-overlay'>/")/n");
sb.Append("document.writeln(/"      <h2>Snail on the Corn</h2>/")/n");
sb.Append("document.writeln(/"      <p>Photo by <a href="http://www.sxc.hu/profile/baines" mce_href="http://www.sxc.hu/profile/baines" target='_blank'>baines</a>.  View full-size photo <a href="http://www.sxc.hu/photo/34453" mce_href="http://www.sxc.hu/photo/34453" target='_blank'>here</a>.</p>/")/n");
sb.Append("document.writeln(/"    </div>/")/n");
sb.Append("document.writeln(/"  </div>/")/n");
sb.Append("document.writeln(/"  <div class='panel'>/")/n");
sb.Append("document.writeln(/"     <img src="img/04.jpg" mce_src="img/04.jpg" /> /")/n");
sb.Append("document.writeln(/"    <div class='panel-overlay'>/")/n");
sb.Append("document.writeln(/"      <h2>Flowers</h2>/")/n");
sb.Append("document.writeln(/"      <p>Photo by <a href="http://www.sxc.hu/profile/jazza" mce_href="http://www.sxc.hu/profile/jazza" target='_blank'>jazza</a>.  View full-size photo <a href="http://www.sxc.hu/photo/990169" mce_href="http://www.sxc.hu/photo/990169" target='_blank'>here</a>.</p>/")/n");
sb.Append("document.writeln(/"    </div>/")/n");
sb.Append("document.writeln(/"  </div>/")/n");
sb.Append("document.writeln(/"  <div class='panel'>/")/n");
sb.Append("document.writeln(/"     <img src="img/06.jpg" mce_src="img/06.jpg" /> /")/n");
sb.Append("document.writeln(/"    <div class='panel-overlay'>/")/n");
sb.Append("document.writeln(/"      <h2>Alone Beach 2B</h2>/")/n");
sb.Append("document.writeln(/"      <p>Photo by <a href="http://www.sxc.hu/profile/sgursozlu" mce_href="http://www.sxc.hu/profile/sgursozlu" target='_blank'>sgursozlu</a>.  View full-size photo <a href="http://www.sxc.hu/photo/738279" mce_href="http://www.sxc.hu/photo/738279" target='_blank'>here</a>.</p>/")/n");
sb.Append("document.writeln(/"    </div>/")/n");
sb.Append("document.writeln(/"  </div>/")/n");
sb.Append("document.writeln(/"  <div class='panel'>/")/n");
sb.Append("document.writeln(/"     <img src="img/05.jpg" mce_src="img/05.jpg" /> /")/n");
sb.Append("document.writeln(/"    <div class='panel-overlay'>/")/n");
sb.Append("document.writeln(/"      <h2>Sunrise Trees</h2>/")/n");
sb.Append("document.writeln(/"      <p>Photo by <a href="http://www.sxc.hu/profile/a_glitch" mce_href="http://www.sxc.hu/profile/a_glitch" target='_blank'>a_glitch</a>.  View full-size photo <a href="http://www.sxc.hu/photo/289581" mce_href="http://www.sxc.hu/photo/289581" target='_blank'>here</a>.</p>/")/n");
sb.Append("document.writeln(/"    </div>/")/n");
sb.Append("document.writeln(/"  </div>/")/n");
sb.Append("document.writeln(/"  <div class='panel'>/")/n");
sb.Append("document.writeln(/"     <img src="img/07.jpg" mce_src="img/07.jpg" /> /")/n");
sb.Append("document.writeln(/"    <div class='panel-overlay'>/")/n");
sb.Append("document.writeln(/"      <h2>Waterfall</h2>/")/n");
sb.Append("document.writeln(/"      <p>Photo by <a href="http://www.sxc.hu/profile/thesaint" mce_href="http://www.sxc.hu/profile/thesaint" target='_blank'>thesaint</a>.  View full-size photo <a href="http://www.sxc.hu/photo/174331" mce_href="http://www.sxc.hu/photo/174331" target='_blank'>here</a>.</p>/")/n");
sb.Append("document.writeln(/"    </div>/")/n");
sb.Append("document.writeln(/"  </div>/")/n");
sb.Append("document.writeln(/"  <div class='panel'>/")/n");
sb.Append("document.writeln(/"     <img src="img/08.jpg" mce_src="img/08.jpg" /> /")/n");
sb.Append("document.writeln(/"    <div class='panel-overlay'>/")/n");
sb.Append("document.writeln(/"      <h2>Death Valley</h2>/")/n");
sb.Append("document.writeln(/"      <p>Photo by <a href="http://www.sxc.hu/profile/djkmart" mce_href="http://www.sxc.hu/profile/djkmart" target='_blank'>djkmart</a>.  View full-size photo <a href="http://www.sxc.hu/photo/270109" mce_href="http://www.sxc.hu/photo/270109" target='_blank'>here</a>.</p>/")/n");
sb.Append("document.writeln(/"    </div>/")/n");
sb.Append("document.writeln(/"  </div>/")/n");
sb.Append("document.writeln(/"  <ul class='filmstrip'>/")/n");
sb.Append("document.writeln(/"    <li><img src="img/frame-01.jpg" mce_src="img/frame-01.jpg" alt='Effet du soleil' title='Effet du soleil' /></li>/")/n");
sb.Append("document.writeln(/"    <li><img src="img/frame-02.jpg" mce_src="img/frame-02.jpg" alt='Eden' title='Eden' /></li>/")/n");
sb.Append("document.writeln(/"    <li><img src="img/frame-03.jpg" mce_src="img/frame-03.jpg" alt='Snail on the Corn' title='Snail on the Corn' /></li>/")/n");
sb.Append("document.writeln(/"    <li><img src="img/frame-04.jpg" mce_src="img/frame-04.jpg" alt='Flowers' title='Flowers' /></li>/")/n");
sb.Append("document.writeln(/"    <li><img src="img/frame-06.jpg" mce_src="img/frame-06.jpg" alt='Alone Beach' title='Alone Beach' /></li>/")/n");
sb.Append("document.writeln(/"    <li><img src="img/frame-05.jpg" mce_src="img/frame-05.jpg" alt='Sunrise Trees' title='Sunrise Trees' /></li>/")/n");
sb.Append("document.writeln(/"    <li><img src="img/frame-07.jpg" mce_src="img/frame-07.jpg" alt='Waterfall' title='Waterfall' /></li>/")/n");
sb.Append("document.writeln(/"    <li><img src="img/frame-08.jpg" mce_src="img/frame-08.jpg" alt='Death Valley' title='Death Valley' /></li>/")/n");
sb.Append("document.writeln(/"  </ul>/")/n");
sb.Append("document.writeln(/"</div>/")/n");
sb.Append("document.writeln(/"<mce:script type='text/javascript'><!--
/")/n");
sb.Append("document.writeln(/"	$(document).ready(function(){/")/n");
sb.Append("document.writeln(/"		$(/'#photos/').galleryView({/")/n");
sb.Append("document.writeln(/"			panel_width: " + width + ",/")/n");
sb.Append("document.writeln(/"			panel_height: " + height + ",/")/n");
sb.Append("document.writeln(/"			frame_width: 100,/")/n");
sb.Append("document.writeln(/"			frame_height: 100/")/n");
sb.Append("document.writeln(/"		});/")/n");
sb.Append("document.writeln(/"	});/")/n");
sb.Append("document.writeln(/"
// --></mce:script>/")/n");
//sw.Write(s);
//sw.Flush();
//sw.Close();
return sb;
}
protected void Button1_Click(object sender, EventArgs e)
{
int width = int.Parse(TextBox1.Text.ToString());
int height = int.Parse(TextBox2.Text.ToString());
CreateJsFile("tt", width, height, 0);
}
} 


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息