如何在静态页面中更新阅读次数?
using System;using System.Data;
using System.Data.OleDb;
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.IO;
using System.Text;
public partial class Admin_Member_AddNews : System.Web.UI.Page
{
HotelLib.HotelClass lib = new HotelLib.HotelClass();
int strID = -1;
protected void Page_Load(object sender, EventArgs e)
{
//this.btnSave.Attributes.Add("onclick", "javascript:return CheckEdit();");
btnSave.Attributes["onclick"] = "return CheckEdit();";
if (Session["name"] != null)
{
if (!Page.IsPostBack)
{
if (!String.IsNullOrEmpty(Request.QueryString["id"]))
{
int id = 0;
try
{
id = int.Parse(Request.QueryString["id"]);
}
catch
{
Response.Write("参数出错!");
Response.End();
}
if (id > 0)
{
DataSet myds = lib.GetHotelList(" * "," Id=" + id + "");
if (null != myds)
{
if (myds.Tables.Count > 0)
{
if (myds.Tables[0].Rows.Count > 0)
{
this.lblID.Text = myds.Tables[0].Rows[0]["ID"].ToString();
this.txtTitle.Text = myds.Tables[0].Rows[0]["Title"].ToString();
this.txtRemark.Text = myds.Tables[0].Rows[0]["Remark"].ToString();
this.txtContent.Html = myds.Tables[0].Rows[0]["Content"].ToString();
this.lblCMaker.Text = myds.Tables[0].Rows[0]["CMaker"].ToString();
this.lblCDate.Text = myds.Tables[0].Rows[0]["CDate"].ToString();
this.txtShowIndex.Text = myds.Tables[0].Rows[0]["ShowIndex"].ToString();
this.readCount.Text += "阅读:" + myds.Tables[0].Rows[0]["ReadCount"].ToString() + "次";
this.btnSave.Text = "更新保存";
}
}
}
}
}
if (strID != -1)
{
UpdateHit();
BandDataToPage();
}
}
}
else
{
Response.Redirect("../Login.aspx?ReUrl=" + Server.UrlEncode("" + Request.RawUrl.ToString() + ""));
}
}
public void UpdateHit()//更新阅读次数
{
string SqlStr = "update MemberNews set ReadCount=ReadCount+1 where ID=" + strID + "";
string connectStr = new LecanClassLib.GetConnection().GetConn();
OleDbCommand myCommand = new OleDbCommand(SqlStr);
myCommand.Connection = new OleDbConnection(connectStr);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
}
public void BandDataToPage()
{
if (strID > 0)
{
DataSet myds = lib.GetHotelList(" * ", " Id=" + strID + "");
if (null != myds)
{
if (myds.Tables.Count > 0)
{
if (myds.Tables[0].Rows.Count > 0)
{
this.readCount.Text += "阅读:" + myds.Tables[0].Rows[0]["ReadCount"].ToString() + "次";
}
}
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
HotelLib.HotelModel model = new HotelLib.HotelModel();
string strDate = DateTime.Now.ToString("yyMMdd") + "/" + DateTime.Now.ToString("yyyymmddhhmmss");
model.Title = this.txtTitle.Text.TrimEnd();
model.Remark = this.txtRemark.Text;
model.Content = this.txtContent.Html.TrimEnd().Replace("'", "’");
model.CMaker = Session["name"].ToString();
model.ShowIndex = int.Parse("0" + this.txtShowIndex.Text);
model.FilePath = "../../NewsFiles/" + strDate + ".html";//存储到数据库中
string dir = Server.MapPath("../../NewsFiles/" + DateTime.Now.ToString("yyMMdd"));//用来生成文件夹
if (!Directory.Exists(dir))//确定给定路径是否引用磁盘上的现有目录
{
Directory.CreateDirectory(dir);
}
if (String.IsNullOrEmpty(this.lblID.Text))
{
//新增
int PId = lib.AddHotelAndReturnId(model);
if (PId>0)
{
try
{
UpdateHit();
BandDataToPage();
StringBuilder strhtml = new StringBuilder();
//创建StreamReader对象
using (StreamReader sr = new StreamReader(Server.MapPath("../../NewsFiles/") + "\\" + "Template.html", Encoding.GetEncoding("gb2312")))
{
String oneline;
//读取指定的HTML文件模板
while ((oneline = sr.ReadLine()) != null)
{
strhtml.Append(oneline);
}
sr.Close();
}
strhtml = strhtml.Replace("$Title$", model.Title);
strhtml = strhtml.Replace("$Content$", model.Content);
strhtml = strhtml.Replace("$Time$", DateTime.Now.ToString("yyyy/MM/dd"));
strhtml = strhtml.Replace("$ReadCount$", this.readCount.Text);
using (StreamWriter sw = new StreamWriter(Server.MapPath("../../NewsFiles/news/" + "\\" + PId + ".html"), false, System.Text.Encoding.GetEncoding("GB2312")))
{
sw.WriteLine(strhtml);
sw.Flush();
sw.Close();
}
if (lib.AddNewsId(PId, "../../NewsFiles/" + PId + ".html"))
{
}
else
{
}
}
catch (Exception err)
{
//输出异常信息
Response.Write(err.ToString());
}
Response.Write("<script>alert('保存成功!');location.href='MemberNews.aspx';</script>");
}
else
{
Response.Write("<script>alert('保存失败!');location.href='MemberNews.aspx';</script>");
}
}
else
{
model.ID = int.Parse(this.lblID.Text);
string FilePath2 = Server.MapPath("../../NewsFiles/news/" + "\\" + model.ID + ".html");
if (lib.UpdateHotel(model))
{
if (strID != -1)
{
UpdateHit();
BandDataToPage();
}
try
{
if (File.Exists(FilePath2))
{
File.Delete(FilePath2);
}
StringBuilder strhtml = new StringBuilder();
using (StreamReader sr = new StreamReader(Server.MapPath("../../NewsFiles/") + "\\" + "Template.html", Encoding.GetEncoding("gb2312")))
{
String oneline;
//读取指定的HTML文件模板
while ((oneline = sr.ReadLine()) != null)
{
strhtml.Append(oneline);
}
sr.Close();
}
strhtml = strhtml.Replace("$Title$", model.Title);
strhtml = strhtml.Replace("$Content$", model.Content);
strhtml = strhtml.Replace("$Time$", DateTime.Now.ToString("yyyy/MM/dd"));
strhtml = strhtml.Replace("$ReadCount$", this.readCount.Text);
byte[] ByteTmp = Encoding.Default.GetBytes(strhtml.ToString());
using (FileStream writer = File.Create(FilePath2))
{
writer.Write(ByteTmp, 0, ByteTmp.Length);
}
}
catch (Exception err)
{
Response.Write(err.ToString());
}
Response.Write("<script>alert('更新成功!');location.href='MemberNews.aspx';</script>");
}
else
{
Response.Write("<script>alert('更新失败!');location.href='MemberNews.aspx';</script>");
}
}
}
}
--------------------编程问答-------------------- ajax也行,页面加载完成,给他的点数加一。 --------------------编程问答-------------------- 哪位能把源码贴出来呢? --------------------编程问答-------------------- 用ajax或是iframe....
补充:.NET技术 , ASP.NET