大家一定知道在静态页中,如何实现传值调用动态页并操作数据库,返回显示结果;比如新闻管理系统中的新闻点击数的实现,我说的是可以生成静态页面的新闻管理系统,他用的是<script> src="Count.aspx?id=1" language="javascript"></script>这样的形式,而Count.aspx文件获取传值后,操作数据库,以这样的形式输出返回结果Response.Write("document.write('" + strInsert + "');");,这样就可以在新闻显示页中,显示新闻浏览次数了;下边是现实的方法:
静态页文件代码:- <html>
- <head>
- <title>JAVASCRIPT传值</title>
- </head>
- <body>
- <div>
- <script src="Count.aspx" language="javascript"></script><br /><br />刷新一下看看此处没有传参数的<br /><br />
- </div>
- </form>
- </body>
- </html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Count.aspx.cs" Inherits="Count" %>注意我把里面的代码全部清空了,只留下了声明;Count.aspx.cs文件:
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- using System.IO;
- public partial class Count : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- string strInsert = "0";
- int intCount = 0;
- strInsert = FileTxt.ReaderFile("Count.txt");//文件操作类,读取文件中的值
- intCount = Convert.ToInt32(strInsert) + 1;
- strInsert = intCount.ToString();
- FileTxt.WriteFile("Count.txt", strInsert);//文件操作类,向文件中写入值
- Response.Write("document.write('" + strInsert + "');");
- }
- }
- using System;
- using System.Data;
- using System.Configuration;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- using System.IO;
- /// <summary>
- ///File 的摘要说明
- /// </summary>
- public class FileTxt
- {
- /// <summary>
- /// 将指定字符串写入指定文件
- /// </summary>
- /// <param name="strFileName">文件名</param>
- /// <param name="strContent">写入的字符串</param>
- public static void WriteFile(string strFileName,string strContent)
- {
- string strFileUrl = HttpContext.Current.Server.MapPath(strFileName);
- StreamWriter sw = null;
- sw = File.CreateText(strFileUrl);
- sw.WriteLine(strContent);
- sw.Flush();
- sw.Close();
- sw.Dispose();
- }
- /// <summary>
- /// 读取txt文件中的内容,不存在则创建并写入"0"
- /// </summary>
- /// <param name="strFileName">文件名</param>
- /// <returns>txt文件内容</returns>
- public static string ReaderFile(string strFileName)
- {
- string str = "0";
- string strFileUrl = HttpContext.Current.Server.MapPath(strFileName);
- if (!File.Exists(strFileUrl))
- {
- WriteFile(strFileName, str);
- return str;
- }
- StreamReader sr = null;
- sr = File.OpenText(strFileUrl);
- str = sr.ReadLine();
- sr.Close();
- sr.Dispose();
- return str;
- }
- }