静态页引用动态显示

 静态页的浏览量统计:
为什么要用静态页,这个不是讨论重点。
静态页的流量统计:用于统计浏览该页的次数(请求该页的次数)。
静态页本身不具有动态显示数据的能力,因此要想在静态页中显示动态数据我们可以在该静态页中内嵌一个动态页面进来,这样就解决了纯静态的问题(又因为这个动态页的数据交互比较少,不会过多的增加服务器的负担),具体来时如下:
1:完成数据库交互的方法,并在动态页中显示(建议用写入页面的方法,不要用控件);
2.创建静态页,用ifream标签将动态页引入该静态页。
代码如下:
静态页:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>无标题页</title>
</head>
<body>
<!--$id<iframe frameborder="0" src="Default.aspx?id=26" id="IFRAME1" />-->
<iframe frameborder="0" height="184" id="frm" scrolling="no" src="../../../../../dfjinshuHits/Default.aspx?id=$id" width="100%"></iframe>
</body>
</html>
数据库方法:
 public static readonly string Constr = "Data Source=.;Initial Catalog=jinshu;Integrated Security=True";
    //获取制定记录的ID
    public static int Hits(int NewsID)
    {
        int count = 0;
        string mySelectQuery = "select BaseHits from JinShu_News where NewsID=" + NewsID;
        SqlConnection myConnection = new SqlConnection(Constr);
        SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
        myCommand.CommandTimeout = 30;
        myConnection.Open();
        SqlDataReader myReader = myCommand.ExecuteReader();
        //   Always   call   Read   before   accessing   data.  
        while (myReader.Read())
        {
            if (myReader.GetInt32(0).ToString().Length != 0)
            {
                count = myReader.GetInt32(0);
            }
            else
            {
                count = 99;
            }
        }
        //   always   call   Close   when   done   reading.  
        myReader.Close();
        //   Close   the   connection   when   done   with   it.  
        myConnection.Close();
        return count;
    }
    //根据ID更新浏览次数
    public static bool UpdateHits(int NewsID)
    {
        bool bl = false;
        string mySQL = "update JinShu_News set BaseHits=BaseHits+1 where NewsID=" + NewsID;
        SqlConnection con = new SqlConnection(Constr);//System.Configuration.ConfigurationSettings.AppSettings["connectionString"]);
        SqlCommand cmd = new SqlCommand(mySQL, con);
        cmd.CommandTimeout = 30;
        con.Open();
        int count = cmd.ExecuteNonQuery();
        if (count != 0)
        {
            bl = true;
        }
        con.Close();
        return bl;
    }
    //根据记录的标题获取记录的ID
    public static int ID(string NewsTitle)
    {
        int id = 0;
        string mySelectQuery = "select NewsID from JinShu_News where NewsTitle='" + NewsTitle + "'";
        SqlConnection myConnection = new SqlConnection(Constr);
        SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
        myCommand.CommandTimeout = 30;
        myConnection.Open();
        SqlDataReader myReader = myCommand.ExecuteReader();
        //   Always   call   Read   before   accessing   data.  
        while (myReader.Read())
        {
            id = myReader.GetInt32(0);
        }
        //   always   call   Close   when   done   reading.  
        myReader.Close();
        //   Close   the   connection   when   done   with   it.  
        myConnection.Close();
        return id;
    }
动态页:
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            int id = Int32.Parse(Request.QueryString["id"]);//Hits.NewsID("10月15日泰安市场碳结钢价格行情");
            //Response.Write(id);
            if (Hits.Updatehits(id))
            {
                //Response.Write("<br />");
                Response.Write(Hits.BaseHits(id).ToString());
            }           
        }
    }
    //生成静态页的页面
public string XX(int id)
    {
        // 生成静态页(class:html)测试
        string htmlName = "test.html";
        // 先确定html文件名,是需要传入的:htmlName
        string htmlDir = html.CreDir(html.HtmlDir.yyyy_MM_dd);
        // 再确定由年月日组成的文件夹
        string DirPath = Request.PhysicalApplicationPath.ToString() + "/news";
        // 根目录

        string PathhtmlDir = DirPath + htmlDir;
        // 需要传入的:dir
        string DirhtmlName = htmlDir + htmlName;
        // 是需要存入数据库的路径和文件名

        //string url = "src='Default.aspx?id=" + id.ToString() + "'";
        string[,] key = new string[1, 2];
        key[0, 0] = "$id";
        key[0, 1] = 26.ToString();//"<iframe frameborder='0' " + url +  " id='IFRAME1' />";

        string pd = html.SChtml(PathhtmlDir, Request.PhysicalApplicationPath.ToString() + "HTMLPage.htm", key, htmlName);
        return pd;
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (XX(26) == "1")
        {
            Response.Redirect("~/Default3.aspx"); 
        }        
    }

注意:实现上述功能需要引入HTML类(外部类库,在前面的文章中有过介绍,请参阅),并且该程序生成的页面在本程序运行时有效,关闭程序或者有文件打开静态页面则计数效果无效。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值