Asp.net(C#)读取数据库并生成JS文件制作首页图片切换效果(附demo源码下载)
本文实例讲述了Asp.net(C#)读取数据库并生成JS文件制作首页图片切换效果的方法。分享给大家供大家参考,具体如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Text;
usingSystem.IO;
publicpartialclass_Default:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
}
///<summary>
///利用模板生成静态页面
///</summary>
///<paramname="strTitle">标题</param>
///<paramname="strText">作者</param>
///<paramname="strContent">发布时间</param>
///<paramname="strAuthor">内容</param>
///<returns>生成页面名称</returns>
publicstaticstringWriteFile(stringstrTitle,stringstrAuthor,stringstrDate,stringstrContent)
{
stringpath=HttpContext.Current.Server.MapPath("~/");
Encodingcode=Encoding.GetEncoding("gb2312");
//读取模板文件
stringtemp=HttpContext.Current.Server.MapPath("~/Template.html");
StreamReadersr=null;
StreamWritersw=null;
stringstr="";
try
{
sr=newStreamReader(temp,code);
str=sr.ReadToEnd();//读取文件
}
catch(Exceptionexp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
Randomrd=newRandom();
stringstrRd=rd.Next(0,9999).ToString();
stringhtmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+strRd+".html";
DateTimedtNow=DateTime.Now;
//替换内容
str=str.Replace("$biaoti",strTitle);
str=str.Replace("$author",strAuthor);
str=str.Replace("$datetime",strDate);
str=str.Replace("$content",strContent);
//写文件
try
{
stringpathUrl=path+dtNow.Year+"\\"+dtNow.Month+"\\"+dtNow.Day;
if(!Directory.Exists(pathUrl))
{
Directory.CreateDirectory(pathUrl);
}
sw=newStreamWriter(pathUrl+"\\"+htmlfilename,false,code);
sw.Write(str);
sw.Flush();
}
catch(Exceptionex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
returndtNow.Year.ToString()+"/"+dtNow.Month.ToString()+"/"+dtNow.Day.ToString()+"/"+htmlfilename;
}
protectedvoidButton1_Click(objectsender,EventArgse)
{
WriteFile("title","ttttttt","2011-09-27","测试<br>");
}
}
Template.html
<table> <tr> <tdalign="center">$biaoti</td> </tr> <tr> <tdalign="center">作者:$author  发布时间:$datetime</td> </tr> <tr> <td>$content</td> </tr> </table>
思路:首先读取数据库中图片,链接,说明文字等数据,然后将读取到的数据写入首页图片切换效果的JS文件。
下面代码实现向数据库中增加图片、链接、说明文字等数据和生成JS文件
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.Data.OleDb;
usingSystem.IO;
usingSystem.Text;
publicpartialclassAdmin_Slide:System.Web.UI.PageprotectedvoidPage_Load(objectsender,EventArgse)
{
}
protectedvoidAdd_Btn_Click(objectsender,EventArgse)//增加幻灯片,将信息写入数据库stringimgpath;
imgpath="../UpLoadFiles/SlideImg/"+ImgUp.FileName;
ImgUp.SaveAs(Server.MapPath(imgpath));
MyOleDbmc=newMyOleDb();
mc.ConnOpen();
OleDbCommandcmd=newOleDbCommand("insertintoSlideImg(lnk,pic,txt)values('"+linkarea.Text.ToString()+"','"+imgpath+"','"+imgtitle.Text.ToString()+"');",mc.Conn);
OleDbDataReaderrdr=null;
rdr=cmd.ExecuteReader();
mc.ConnClose();
}
protectedvoidMJS_Btn_Click(objectsender,EventArgse)//生成JS幻灯文件stringjsfile,jstemplete;
stringstrlnk,strpic,strtxt;
strlnk=null;
strpic=null;
strtxt=null;
jsfile=Server.MapPath("~/Js/")+"SlideImg.js";//JS文件路径
jstemplete=Server.MapPath("~/Js/")+"JsTemplete.js";//JS文件模板路径
deljs(jsfile);//删除JS文件
MyOleDbmc=newMyOleDb();
mc.ConnOpen();
OleDbCommandcmd=newOleDbCommand("selecttop"+Img_Num.Text.ToString()+"*fromSlideImgorderbyiddesc",mc.Conn);
OleDbDataReaderrdr=null;
rdr=cmd.ExecuteReader();
while(rdr.Read())strlnk+=rdr["lnk"].ToString()+"|";
strpic+=rdr["pic"].ToString()+"|";
strtxt+=rdr["txt"].ToString()+"|";mc.ConnClose();
Encodingcode=Encoding.GetEncoding("UTF-8");
StreamReadersr=null;
StreamWritersw=null;
stringstr="";
trysr=newStreamReader(jstemplete,code);
str=sr.ReadToEnd();//读取文件catch(Exceptionexp)HttpContext.Current.Response.Write("<scripttype='text/javascript'>alert('读取模板文件错误!')</script>"+exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
//替换内容str=str.Replace("$txt$",strtxt);
str=str.Replace("$pic$",strpic);
str=str.Replace("$lnk$",strlnk);
trysw=newStreamWriter(jsfile,false,code);
sw.Write(str);
sw.Flush();catch(Exceptionex)HttpContext.Current.Response.Write("<scripttype='text/javascript'>alert('生成JS文件出错!')</script>"+ex.Message);
HttpContext.Current.Response.End();finallysw.Flush();
sw.Close();
}
}
//以下是自定义删除原有JS文件函数
protectedvoiddeljs(stringjsfile)if(File.Exists(jsfile))File.Delete(jsfile);elseResponse.Write("<scripttype='text/javascript'>alert('系统中不存在能产生首页切换图片的文件!')</script>");}
}
JS文件模板JsTemplete.js
varfocus_width=300;
varfocus_height=225;
vartext_height=18;
varswf_height=focus_height+text_height;
varpics,links,texts;
texts='$txt$'//将被替换的内容(切换图片的说明文字)
pics='$pic$'//将被替换的内容(切换图片的地址)
links='$lnk$'//将被替换的内容(链接地址)
pics=pics.substr(0,pics.length-1);
links=links.substr(0,links.length-1);
texts=texts.substr(0,texts.length-1);
varfv="pics="+pics+"&links="+links+"&texts="+texts+"&borderwidth="+focus_width+"&borderheight="+focus_height+"&textheight="+text_height;
document.write('<objectclassid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"width="'+focus_width+'"height="'+swf_height+'">');
document.write('<paramname="allowScriptAccess"value="sameDomain"><paramname="movie"value="../Plugin/slide.swf"><paramname="quality"value="high"><paramname="bgcolor"value="#E5ECF4">');
document.write('<paramname="menu"value="false"><paramname=wmodevalue="opaque">');
document.write('<paramname="FlashVars"value="pics='+pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'">');
document.write('<embedsrc="pixviewer.swf"wmode="opaque"FlashVars="pics='+pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'"menu="false"bgcolor="#009900"quality="high"width="'+focus_width+'"height="'+focus_height+'"allowScriptAccess="sameDomain"type="application/x-shockwave-flash"pluginspage="http://www.macromedia.com/go/getflashplayer"/>');
document.write('</object>');
办法三
<scriptlanguage="javascript"src="js.aspx?classid=2"></script>
js.aspx输出的是js内容就可以了
然后在这个abc.aspx里读取数据库,并生成document.write输出新闻的语句
<%@PageLanguage="C#"AutoEventWireup="true"%> varfocus_width="asdasdasdwer"; document.write(focus_width);
完整实例代码点击此处本站下载。
更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net文件操作技巧汇总》、《asp.netajax技巧总结专题》及《asp.net缓存操作技巧总结》。
希望本文所述对大家asp.net程序设计有所帮助。