JS调用页面

来源:互联网 发布:软件工程学不学高数 编辑:程序博客网 时间:2024/05/17 04:14
<script src=“***.aspx”type=“text/javascript”></script>

使用方法:

(1)在某一页面xx调用<script src=“aa.aspx” type=“text/javascript”></script>

     在aa.aspx页面中,将<%@ Page Language=“C#”......>这句之外的内容全部删除

(2)然后在aa.aspx.cs页面中,使用js输出你所需要的内容

     例如:Response.Write(“document.write('输出你所需要的内容!')”);

这样就会在页面xx显示出所需的内容了

----------------------------------------------------------------------------------

在aspx页面中利用JS实现向另一个aspx页面传值处理并返回显示结果-NET编程

大家一定知道在静态页中,如何实现传值调用动态页并操作数据库,返回显示结果;比如新闻管理系统中的新闻点击数的实现,我说的是可以生成静态页面的新闻管理系统,他用的是<script> src="Count.aspx?id=1" language="javascript"></script>这样的形式,而Count.aspx文件获取传值后,操作数据库,以这样的形式输出返回结果Response.Write("document.write('" + strInsert + "');");,这样就可以在新闻显示页中,显示新闻浏览次数了;下边是现实的方法:

静态页文件代码:
  1. <html>  
  2. <head>  
  3.     <title>JAVASCRIPT传值</title>  
  4. </head>  
  5. <body>  
  6.     <div>  
  7.     <script src="Count.aspx" language="javascript"></script><br /><br />刷新一下看看此处没有传参数的<br /><br />  
  8.     </div>  
  9.     </form>  
  10. </body>  
  11. </html>  
下面是Count.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Count.aspx.cs" Inherits="Count" %>注意我把里面的代码全部清空了,只留下了声明;Count.aspx.cs文件:
  1. using System;   
  2. using System.Collections;   
  3. using System.Configuration;   
  4. using System.Data;   
  5. using System.Linq;   
  6. using System.Web;   
  7. using System.Web.Security;   
  8. using System.Web.UI;   
  9. using System.Web.UI.HtmlControls;   
  10. using System.Web.UI.WebControls;   
  11. using System.Web.UI.WebControls.WebParts;   
  12. using System.Xml.Linq;   
  13. using System.IO;   
  14. public partial class Count : System.Web.UI.Page   
  15. {   
  16.     protected void Page_Load(object sender, EventArgs e)   
  17.      {   
  18.         string strInsert = "0";   
  19.         int intCount = 0;   
  20.          strInsert = FileTxt.ReaderFile("Count.txt");//文件操作类,读取文件中的值   
  21.          intCount = Convert.ToInt32(strInsert) + 1;   
  22.          strInsert = intCount.ToString();   
  23.          FileTxt.WriteFile("Count.txt", strInsert);//文件操作类,向文件中写入值   
  24.          Response.Write("document.write('" + strInsert + "');");   
  25.      }   
  26.   
  27. }  
FileTxt.cs文件操作类:
  1. using System;   
  2. using System.Data;   
  3. using System.Configuration;   
  4. using System.Linq;   
  5. using System.Web;   
  6. using System.Web.Security;   
  7. using System.Web.UI;   
  8. using System.Web.UI.HtmlControls;   
  9. using System.Web.UI.WebControls;   
  10. using System.Web.UI.WebControls.WebParts;   
  11. using System.Xml.Linq;   
  12. using System.IO;   
  13.   
  14. /// <summary>   
  15. ///File 的摘要说明   
  16. /// </summary>   
  17. public class FileTxt   
  18. {   
  19.     /// <summary>   
  20.     /// 将指定字符串写入指定文件   
  21.     /// </summary>   
  22.     /// <param name="strFileName">文件名</param>   
  23.     /// <param name="strContent">写入的字符串</param>   
  24.     public static void WriteFile(string strFileName,string strContent)   
  25.      {   
  26.         string strFileUrl = HttpContext.Current.Server.MapPath(strFileName);   
  27.          StreamWriter sw = null;   
  28.          sw = File.CreateText(strFileUrl);   
  29.          sw.WriteLine(strContent);   
  30.          sw.Flush();   
  31.          sw.Close();   
  32.          sw.Dispose();   
  33.      }   
  34.    /// <summary>   
  35.    /// 读取txt文件中的内容,不存在则创建并写入"0"   
  36.    /// </summary>   
  37.    /// <param name="strFileName">文件名</param>   
  38.    /// <returns>txt文件内容</returns>   
  39.     public static string ReaderFile(string strFileName)   
  40.      {   
  41.         string str = "0";   
  42.         string strFileUrl = HttpContext.Current.Server.MapPath(strFileName);   
  43.         if (!File.Exists(strFileUrl))   
  44.          {   
  45.              WriteFile(strFileName, str);   
  46.             return str;   
  47.          }   
  48.          StreamReader sr = null;   
  49.          sr = File.OpenText(strFileUrl);   
  50.          str = sr.ReadLine();   
  51.          sr.Close();   
  52.          sr.Dispose();   
  53.         return str;   
  54.      }   
  55. }

 

原创粉丝点击