Asp.net(C#)读取数据库并生成JS文件制作首页图片切换效果(附demo源码下载)
程序员文章站
2023-12-19 11:27:46
本文实例讲述了asp.net(c#)读取数据库并生成js文件制作首页图片切换效果的方法。分享给大家供大家参考,具体如下:
using system;
using...
本文实例讲述了asp.net(c#)读取数据库并生成js文件制作首页图片切换效果的方法。分享给大家供大家参考,具体如下:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.text; using system.io; public partial class _default : system.web.ui.page { protected void page_load(object sender, eventargs e) { } /// <summary> /// 利用模板生成静态页面 /// </summary> /// <param name="strtitle">标题</param> /// <param name="strtext">作者</param> /// <param name="strcontent">发布时间</param> /// <param name="strauthor">内容</param> /// <returns>生成页面名称</returns> public static string writefile(string strtitle, string strauthor, string strdate, string strcontent) { string path = httpcontext.current.server.mappath("~/"); encoding code = encoding.getencoding("gb2312"); // 读取模板文件 string temp = httpcontext.current.server.mappath("~/template.html"); streamreader sr = null; streamwriter sw = null; string str = ""; try { sr = new streamreader(temp, code); str = sr.readtoend(); // 读取文件 } catch (exception exp) { httpcontext.current.response.write(exp.message); httpcontext.current.response.end(); sr.close(); } random rd = new random(); string strrd = rd.next(0, 9999).tostring(); string htmlfilename = datetime.now.tostring("yyyymmddhhmmss") + strrd + ".html"; datetime dtnow = datetime.now; // 替换内容 str = str.replace("$biaoti", strtitle); str = str.replace("$author", strauthor); str = str.replace("$datetime", strdate); str = str.replace("$content", strcontent); // 写文件 try { string pathurl = path + dtnow.year + "\\" + dtnow.month + "\\" + dtnow.day; if (!directory.exists(pathurl)) { directory.createdirectory(pathurl); } sw = new streamwriter(pathurl + "\\" + htmlfilename, false, code); sw.write(str); sw.flush(); } catch (exception ex) { httpcontext.current.response.write(ex.message); httpcontext.current.response.end(); } finally { sw.close(); } return dtnow.year.tostring() + "/" + dtnow.month.tostring() + "/" + dtnow.day.tostring() + "/" + htmlfilename; } protected void button1_click(object sender, eventargs e) { writefile("title" , "ttttttt" , "2011-09-27", "测试 <br>"); } }
template.html
<table> <tr> <td align="center">$biaoti</td> </tr> <tr> <td align="center">作者:$author 发布时间:$datetime</td> </tr> <tr> <td>$content</td> </tr> </table>
思路:首先读取数据库中图片,链接,说明文字等数据,然后将读取到的数据写入首页图片切换效果的js文件。
下面代码实现向数据库中增加 图片、链接、说明文字等数据 和 生成js文件
using system; using system.data; using system.configuration; using system.collections; using system.web; using system.web.security; using system.web.ui; using system.web.ui.webcontrols; using system.web.ui.webcontrols.webparts; using system.web.ui.htmlcontrols; using system.data.oledb; using system.io; using system.text; public partial class admin_slide : system.web.ui.page protected void page_load(object sender, eventargs e) { } protected void add_btn_click(object sender, eventargs e) //增加幻灯片,将信息写入数据库 string imgpath; imgpath = "../uploadfiles/slideimg/" + imgup.filename; imgup.saveas(server.mappath(imgpath)); myoledb mc = new myoledb(); mc.connopen(); oledbcommand cmd = new oledbcommand("insert into slideimg(lnk,pic,txt) values ('" + linkarea.text.tostring() + "','" + imgpath + "','" + imgtitle.text.tostring() + "');", mc.conn); oledbdatareader rdr = null; rdr = cmd.executereader(); mc.connclose(); } protected void mjs_btn_click(object sender, eventargs e) //生成js幻灯文件 string jsfile,jstemplete; string strlnk, 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文件 myoledb mc = new myoledb(); mc.connopen(); oledbcommand cmd = new oledbcommand("select top " + img_num.text.tostring() + " * from slideimg order by id desc", mc.conn); oledbdatareader rdr = null; rdr = cmd.executereader(); while (rdr.read()) strlnk += rdr["lnk"].tostring() + "|"; strpic += rdr["pic"].tostring() + "|"; strtxt += rdr["txt"].tostring() + "|"; mc.connclose(); encoding code = encoding.getencoding("utf-8"); streamreader sr = null; streamwriter sw = null; string str = ""; try sr = new streamreader(jstemplete, code); str = sr.readtoend(); // 读取文件 catch (exception exp) httpcontext.current.response.write("<script type='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); try sw = new streamwriter(jsfile, false, code); sw.write(str); sw.flush(); catch (exception ex) httpcontext.current.response.write("<script type='text/javascript'>alert('生成js文件出错!')</script>" + ex.message); httpcontext.current.response.end(); finally sw.flush(); sw.close(); } } //以下是自定义删除原有js文件函数 protected void deljs(string jsfile) if (file.exists(jsfile)) file.delete(jsfile); else response.write("<script type='text/javascript'>alert('系统中不存在能产生首页切换图片的文件!')</script>"); } }
js文件模板 jstemplete.js
var focus_width=300; var focus_height=225; var text_height=18; var swf_height = focus_height+text_height; var pics,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); var fv="pics="+pics+"&links="+links+"&texts="+texts+"&borderwidth="+focus_width+"&borderheight="+focus_height+"&textheight="+text_height; document.write('<object classid="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('<param name="allowscriptaccess" value="samedomain"><param name="movie" value="../plugin/slide.swf"><param name="quality" value="high"><param name="bgcolor" value="#e5ecf4">'); document.write('<param name="menu" value="false"><param name=wmode value="opaque">'); document.write('<param name="flashvars" value="pics='+pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'">'); document.write('<embed src="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>');
办法三
<script language="javascript" src="js.aspx?classid=2"> </script>
js.aspx输出的是js内容就可以了
然后在这个abc.aspx里读取数据库,并生成document.write输出新闻的语句
<%@ page language="c#" autoeventwireup="true" %> var focus_width="asdasdasdwer"; document.write(focus_width);
完整实例代码点击此处。
更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。
希望本文所述对大家asp.net程序设计有所帮助。