用静态页面动态显示

本文介绍了一种使用ASP.NET结合JavaScript动态显示新闻列表的方法,并进一步探讨了通过生成静态页面来提高加载速度的技术方案。

今天学会用静态页面动态显示,方法很简单.

首先建一页面叫aa.aspx.

 aa.aspx用来显示新闻.在 <form id="form1" runat="server">
    <script language="javascript" src="bb.aspx" type="text/javascript"></script>
    </form> 

然后建一页面叫bb.aspx.

在该页下的.
    protected void Page_Load(object sender, EventArgs e)
    {
        string sql = "select * from article";

        DataSet ss = bll.GreatDs(sql);

        DataTable dt = ss.Tables[0];

        Response.Write("document.write('<table cellspacing=0 rules=/"all/" border=1 id=/"ctl00_ContentPlaceHolder1_GridView1/" style=/"border-collapse:collapse;/">');");
        Response.Write("document.write('<tr><th scope=/"col/">序号</th><th scope=/"col/">文章类型</th><th scope=/"col/">标题</th><th scope=/"col/">更新时间</th><th scope=/"col/">点击率</th>');");
        Response.Write("document.write('</tr>');");
        for (int i=0; i < dt.Rows.Count; i++)
        {
            Response.Write("document.write('<tr>');");

            Response.Write("document.write('<td>"+dt.Rows[i]["articleid"].ToString()+"');");
            Response.Write("document.write('</td>');");

            Response.Write("document.write('<td>" + dt.Rows[i]["class"].ToString() + "');");
            Response.Write("document.write('</td>');");

            Response.Write("document.write('<td><a href=/"newsDetail.aspx?id=" + dt.Rows[i]["articleid"].ToString() + "/">" + dt.Rows[i]["title"].ToString() + "</a>');");
            Response.Write("document.write('</td>');");

            Response.Write("document.write('<td>" + dt.Rows[i]["dateandtime"].ToString() + "');");
            Response.Write("document.write('</td>');");

            Response.Write("document.write('<td>" + dt.Rows[i]["hits"].ToString() + "');");
            Response.Write("document.write('</td>');");

            Response.Write("document.write('</tr>');");
        }
        Response.Write("document.write('</table>');");
       
        Response.End();
    }

写上以上语句.就可以像用gridview一样了.

呵呵.简单又方便.

 

这种方法并不能提高网页打开的速度,所以我后来采用用生成静态页的方式来实现。具体如下:

private void createHtml()
    {
        try
        {
            string sql = "select distinct  top 6  a.articleid,b.class,a.title,a.dateandtime,a.hits from article a,aclass b where a.classid=b.classid order by a.articleid desc";

            DataSet ss = spbase.GreatDs(sql);

            DataTable dt = ss.Tables[0];

            if (dt == null) return;
           
            string cription = "";

            string title = "新闻";//这是动态生成的静态页面
            string dhtml = "<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.0 Transitional//CN/"><html><head>" +
             "<meta http-equiv=/"Content-Type/" content=/"text/html; charset=utf-8/">" +
             "<title>" + title + "</title><link href=/"../Style2.CSS/" rel=/"stylesheet/" type=/"text/css/" /></head><body topmargin=0>";
            dhtml += "<table valign=top border=0 cellspacing=0 cellpadding=0 align=left>" +
             "<tr>";//<tr><td height=5>" + title + "</td></tr>
           

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {                 
                    if (dt.Rows[i]["title"].ToString().Length > 25)
                    {
                        cription = dt.Rows[i]["title"].ToString().Substring(0, 25) + "....";
                    }
                    else
                        cription = dt.Rows[i]["title"].ToString();
                    dhtml += "<tr height=5px><td></td></tr>";
                    dhtml += "<td class=/"hotlink/" ><li><a href=/"../../News/newsDetail.aspx?id=" + dt.Rows[i]["articleid"].ToString() + "/"  target=/"_blank/" class=/"hotlink/" title=/"" + dt.Rows[i]["title"].ToString() + "/">" + cription + "</a></td>";
                }              
            }
            dhtml += "</tr></table></body></html>";
            string Filename = "news.html";//DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";//动态的文件名
            string Filepath = Server.MapPath(@"/aa/");
            //string Cname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
            string Filecreatepath = Filepath;//页面要保存的路径+ Cname
            string FileFullPath = Filecreatepath + Filename;

            //动态生成的静态页面按年月保存本年本月的文件夹中
            if (File.Exists(FileFullPath)) //判断当月的文件夹是否存在,
            {              
                //****删除文件
                File.Delete(FileFullPath);
                DirectoryInfo di = Directory.CreateDirectory(Filecreatepath);
                Create_html(FileFullPath, dhtml);
               
                //调用创建html静态页面方法               
               // Create_html(Filecreatepath + Filename, dhtml);
            }
            else
            {
                //创建页面保存到的文件夹
                DirectoryInfo di = Directory.CreateDirectory(Filecreatepath);
                Create_html(Filecreatepath + Filename, dhtml);
            }
        }
        catch (IOException ex)
        {
            throw ex;
        }
    }
    private void Create_html(string allfilename, string htmlcode)
    {      
        FileStream CreateFile = new FileStream(allfilename, FileMode.CreateNew);
        StreamWriter sw = new StreamWriter(CreateFile);
        sw.WriteLine(htmlcode);//将拼好的Html代码写入页面中
        sw.Close();
        Page.RegisterStartupScript("", "<script>window.alert('The file created successfully.!')</script>");
    }  

在调用时:

<iframe src="newsTemp/news.html" frameborder=0 width=100%></iframe>

<html> <head> <title>查看个人KPI报表</title> </head> <style> table tr{ font-size:12px; font-weight:600; } table td{ background-color:#E6E6E6; } </style> [removed] var httpRequest; //创建异步请求对象 function createXMLHttpRequest(){ if(window.XMLHttpRequest){ httpRequest=new XMLHttpRequest(); }else if(window.ActiveXObject){ try{ httpRequest=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try{ httpRequest=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){} } } } function getData(){ var sel=document.getElementById("dept"); var deptId=sel.options[sel.selectedIndex].value; //创建异步请求对象 createXMLHttpRequest(); //设置请求的回调函数 httpRequest.onreadystatechange=callBack; //创建新的http请求,指定请求的方法,url,以及验证信息 httpRequest.open("GET","servlet/BillAction?action=getData&deptId;="+deptId,true); //发送请求至服务器,并接收回应 httpRequest.send(null); } //回调函数 function callBack(){ //如果请求数据接收完毕 if(httpRequest.readyState==4&&httpRequest;.status==200){ //将返回的请求文本(这里实际上就是data.jsp)写入指定的DIV中 document.getElementById("dataDiv")[removed]=httpRequest.responseText; } } [removed] <body> 部门: ${dept.name} <input type="button" id="selBtn" value="显示报表" <div id="dataDiv"> <!—加载httpRequest请求返回的文本(即data.jsp),如红色字体所示代码 --> </body> </html> data.jsp页面的主要内容 <body>  部门名称 被评人/评分人 ${fromEmp.name} KPI成绩   ${data} </body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值