当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  使用JScript.NET创建asp.net页面

 使用JScript.NET创建asp.net页面

点击次数:12 次 发布日期:2008-11-26 12:16:16 作者:源代码网
源代码网推荐      大家一定习惯于使用vb.net和C#来创建asp.net页面,但是不知大家知不知道我们还可以使用Jscript.net编写asp.net.
源代码网推荐  这可能是自微软1996年推出基于IE3.0的Jscript1.0以来在功能上最大的飞跃。JScript 传统上被用作开发客户端脚本。在internet上它普遍存在,特别是在Active sever page(ASP)中 。
源代码网推荐  当脚本变得很大的时候,程序员需要编写更有效的代码;并且程序变得越来越复杂,程序员往往受到Jscript的局限性。
源代码网推荐  如果你对Jscript很熟悉的话,你将很快的在.net平台上使用Jscript.net,应为Jscript.net就像是Jscript的升级版,而不是一门新的语言。
源代码网推荐  Jscript.net最新的特性是Jscript.net是真正的编译语言。这使它完全可以与vb.net和C#相媲美。从语法方面,Jscript.net改善了变量类型的定义,不但支持明确的变量类型定义,还支持模糊的变量类型定义。模糊定义是一项激动人心的技术,它可以分析你脚本中使用的变量,并且推断出变量的类型。这就意味着你可以使用未定义的变量并以更快的速度执行。
源代码网推荐  请参照下面的例子:
源代码网推荐  function test()
源代码网推荐  {
源代码网推荐  for (var x = 0; x < 100; x++)
源代码网推荐  {
源代码网推荐  print(x);
源代码网推荐  }
源代码网推荐  }
源代码网推荐  当Jscript.net编译这个程序的时候,它分析变量X的用途并确定变量X只用做数值型,结果变量被安全的定义为数值型。技术进步在于把变量定义为数值型要明显优于把变量定义为generic Object 或 variant。
源代码网推荐  为了实现Jscript.net的推断变量类型的功能,你需要遵守以下几条规则!
源代码网推荐  一. 永远声明你的局部变量。这看起来像是显而易见的,但是这是非常重要的,Jscript.net只能推断你的局部变量,而不是全局变量。如果你没有声明它,直接使用,它就将成为全局变量,将不能被优化。
源代码网推荐  二. 仅使用一种数据类型,如果你声明一个数值型变量,却用来储存字符型的数据,Jscript将把该变量定义为generic Object 或 variant。
源代码网推荐  //无法推断类型 -- glob 是一个全局变量
源代码网推荐  var glob = 42;
源代码网推荐  function myfunc()
源代码网推荐  {
源代码网推荐  //无法推断类型-- s 没有定义因此它被当作全局变量
源代码网推荐  s = "hello";
源代码网推荐  // 可以推断类型
源代码网推荐  var i = 0;
源代码网推荐  //无法推断类型—q被指派成其他的类型
源代码网推荐  var q = new Date();
源代码网推荐  q = 3.14159;
源代码网推荐  }
源代码网推荐  尽管类型推断是非常好的功能,但是它还是有一定的缺点的。它无法帮助我们捕获类型不匹配或其他的错误。为了解决这个问题,Jscript.net提供一种方法明确定义变量的类型。通过例子,你将很容易的了解它的使用方法。
源代码网推荐  // 定义数值类型
源代码网推荐  var myInt : int = 42;
源代码网推荐  // 定义一个函数,返回一个字符串
源代码网推荐  function GetName() : String
源代码网推荐  {
源代码网推荐  // 程序行
源代码网推荐  }
源代码网推荐  // 定义一个带两个参数的函数返回一个逻辑类型
源代码网推荐  function CheckNumber(dVal : double) : Boolean
源代码网推荐  {
源代码网推荐  // function code
源代码网推荐  }
源代码网推荐  下面给出一个完成的函数,仔细的体会一下。
源代码网推荐  function getConditions(strCity : String) : String
源代码网推荐  {
源代码网推荐  var now : Date = new Date();
源代码网推荐  switch (strCity.toUpperCase())
源代码网推荐  {
源代码网推荐  case "LONDON":
源代码网推荐  if (now.getMonth() <= 7 || now.getMonth() >= 9)
源代码网推荐  {
源代码网推荐  return "overcast";
源代码网推荐  }
源代码网推荐  else
源代码网推荐  {
源代码网推荐  return "partly overcast and humid";
源代码网推荐  }
源代码网推荐  break;
源代码网推荐  case "SEATTLE":
源代码网推荐  if (now.getMonth() == 7 && now.getDay() == 4)
源代码网推荐  {
源代码网推荐  return "torrential rain";
源代码网推荐  }
源代码网推荐  else
源代码网推荐  {
源代码网推荐  return "rain";
源代码网推荐  }
源代码网推荐  break;
源代码网推荐  case "LA":
源代码网推荐  return "smoggy";
源代码网推荐  break;
源代码网推荐  case "PHOENIX":
源代码网推荐  return "damn hot";
源代码网推荐  break;
源代码网推荐  default:
源代码网推荐  return "partly cloudy with a chance of showers";
源代码网推荐  }
源代码网推荐  }
源代码网推荐  使用Jscript.net,你也可以定义其他.net架构的类型,通过引用命名空间和派生类可以向Jscript.net引入新的数据类型。这样在两者的数据类型中就有可能产生重叠。参见下表:
源代码网推荐  Boolean .NET Framework Boolean / JScript boolean
源代码网推荐  Number .NET Framework Double / JScript number
源代码网推荐  String .NET Framework String / JScript string
源代码网推荐  Int .NET Framework Int32
源代码网推荐  Long .NET Framework Int64
源代码网推荐  Float .NET Framework Single
源代码网推荐  Double .NET Framework Double
源代码网推荐  Object .NET Framework Object / JScript Object
源代码网推荐  Date JScript Date object
源代码网推荐  Array JScript Array
源代码网推荐  Function JScript Function object
源代码网推荐  在Jscript中定义类通过类声明, 包含方法和对象和var 声明。对于类的派生通过下面两个程序的对比,你讲清楚地明白。
源代码网推荐  JScript 5.5 Code
源代码网推荐  // Simple object with no methods
源代码网推荐  function Car(make, color, year)
源代码网推荐  {
源代码网推荐  this.make = make;
源代码网推荐  this.color = color;
源代码网推荐  this.year = year;
源代码网推荐  }
源代码网推荐  function Car.prototype.GetDescription()
源代码网推荐  {
源代码网推荐  return this.year + " " + this.color + " " + this.make;
源代码网推荐  }
源代码网推荐  // Create and use a new Car object
源代码网推荐  var myCar = new Car("Accord", "Maroon", 1984);
源代码网推荐  print(myCar.GetDescription());
源代码网推荐  JScript.NET Code
源代码网推荐  // Wrap the function inside a class statement.
源代码网推荐  class Car
源代码网推荐  {
源代码网推荐  var make : String;
源代码网推荐  var color : String;
源代码网推荐  var year : int;
源代码网推荐  function Car(make, color, year)
源代码网推荐  {
源代码网推荐  this.make = make;
源代码网推荐  this.color = color;
源代码网推荐  this.year = year;
源代码网推荐  }
源代码网推荐  function GetDescription()
源代码网推荐  {
源代码网推荐  return this.year + " " + this.color + " " + this.make;
源代码网推荐  }
源代码网推荐  }
源代码网推荐  var myCar = new Car("Accord", "Maroon", 1984);
源代码网推荐  print(myCar.GetDescription());
源代码网推荐  Jscript.net还支持定义private和protected property通过GET和SET进行读写。
源代码网推荐  如下例:
源代码网推荐  class Person
源代码网推荐  {
源代码网推荐  private var m_sName : String;
源代码网推荐  private var m_iAge : int;
源代码网推荐  function Person(name : String, age : int)
源代码网推荐  {
源代码网推荐  this.m_sName = name;
源代码网推荐  this.m_iAge = age;
源代码网推荐  }
源代码网推荐  // Name 只读
源代码网推荐  function get Name() : String
源代码网推荐  {
源代码网推荐  return this.m_sName;
源代码网推荐  }
源代码网推荐  // Age 读写但是只能用SET
源代码网推荐  function get Age() : int
源代码网推荐  {
源代码网推荐  return this.m_sAge;
源代码网推荐  }
源代码网推荐  function set Age(newAge : int)
源代码网推荐  {
源代码网推荐  if ((newAge >= 0) && (newAge <= 110))
源代码网推荐  this.m_iAge = newAge;
源代码网推荐  else
源代码网推荐  throw newAge + " is not a realistic age!";
源代码网推荐  }
源代码网推荐  }
源代码网推荐  var fred : Person = new Person("Fred", 25);
源代码网推荐  print(fred.Name);
源代码网推荐  print(fred.Age);
源代码网推荐  // 这将产生一个编译错误,name是只读的。
源代码网推荐  fred.Name = "Paul";
源代码网推荐  // 这个将正常执行
源代码网推荐  fred.Age = 26;
源代码网推荐  // 这将得到一个 run-time 错误, 值太大了
源代码网推荐  fred.Age = 200;
源代码网推荐  Jscript.net可以用JScript 或任意NET 框架语言(如 C #,VB7.0) 通过增加extends主题词在类声明以后来继承和扩展现有类。这能力允许Jscript.net非常容易地利用 NET 平台的丰厚资源。为了说明这些,给出一个程序。这个程序扩展了NET 框架的ServiceBase 类。
源代码网推荐  // 导入需要的.net命名空间
源代码网推荐  import System;
源代码网推荐  import System.ServiceProcess;
源代码网推荐  import System.Diagnostics;
源代码网推荐  import System.Timers;
源代码网推荐  class SimpleService extends ServiceBase
源代码网推荐  {
源代码网推荐  private var timer : Timer;
源代码网推荐  function SimpleService()
源代码网推荐  {
源代码网推荐  CanPauseAndContinue = true;
源代码网推荐  ServiceName = "JScript Service";
源代码网推荐  timer = new Timer();
源代码网推荐  timer.Interval = 1000;
源代码网推荐  timer.AddOnTimer(OnTimer);
源代码网推荐  }
源代码网推荐  protected override function OnStart(args : String[])
源代码网推荐  {
源代码网推荐  EventLog.WriteEntry("JScript Service started");
源代码网推荐  timer.Enabled = true;
源代码网推荐  }
源代码网推荐  protected override function OnStop()
源代码网推荐  {
源代码网推荐  EventLog.WriteEntry("JScript Service stopped");
源代码网推荐  timer.Enabled = false;
源代码网推荐  }
源代码网推荐  protected override function OnPause()
源代码网推荐  {
源代码网推荐  EventLog.WriteEntry("JScript Service paused");
源代码网推荐  timer.Enabled = false;
源代码网推荐  }
源代码网推荐  protected override function OnContinue()
源代码网推荐  {
源代码网推荐  EventLog.WriteEntry("JScript Service continued");
源代码网推荐  timer.Enabled = true;
源代码网推荐  }
源代码网推荐  function OnTimer(source : Object, e : EventArgs)
源代码网推荐  {
源代码网推荐  EventLog.WriteEntry("Hello World from JScript!");
源代码网推荐  }
源代码网推荐  }
源代码网推荐  ServiceBase.Run(new SimpleService());
源代码网推荐  
源代码网推荐  如何在asp+中使用Jscript.net这才是我们关键的问题。我们将通过一个例子来说明这个问题。
源代码网推荐  访问sqlserver数据库
源代码网推荐  第一个aps+例子是使用Jscript.net和.net的数据访问类来访问sqlserver数据库,
源代码网推荐  这里我还将使用大家熟悉的<% %>格式来编写,访问pubs中的authors表,我知道这很简单但是它可以体现一些新的特性。
源代码网推荐  <%@ Import Namespace="System.Data" %>
源代码网推荐  <%@ Import Namespace="System.Data.SQL" %>
源代码网推荐  <%@ language="JScript" %>
源代码网推荐  <link rel="STYLESHEET" type="text/css" href=http://www.ddvip.com/web/aspnet/index4/"style.css">
源代码网推荐  <%
源代码网推荐  // 设置数据库连接
源代码网推荐  var myConnection:SQLConnection = new SQLConnection("server=scripting;uid=sa;pwd=;database=pubs");
源代码网推荐  // 执行查询
源代码网推荐  var myCommand:SQLDataSetCommand = new SQLDataSetCommand("select * from Authors", myConnection);
源代码网推荐  // 声明变量
源代码网推荐  var ds:DataSet = new DataSet();
源代码网推荐  var myTable:DataTable
源代码网推荐  var myColumns:ColumnsCollection
源代码网推荐  var myCol:DataColumn
源代码网推荐  var myRows:RowsCollection
源代码网推荐  var myRow:DataRow
源代码网推荐  // 通过FillDataSet方法获取数据
源代码网推荐  myCommand.FillDataSet(ds, "Authors");
源代码网推荐  myTable = ds.Tables[0]
源代码网推荐  %>
源代码网推荐  <h1>
源代码网推荐  <%=ds.Tables[0].TableName%>
源代码网推荐  </h1>
源代码网推荐  <br>
源代码网推荐  <TABLE>
源代码网推荐  <THEAD>
源代码网推荐  <TR>
源代码网推荐  <%
源代码网推荐  //在表格的最上面输出字段名
源代码网推荐  myColumns = myTable.Columns
源代码网推荐  for (myCol in myColumns)
源代码网推荐  {
源代码网推荐  %>
源代码网推荐  <TH class="spec">
源代码网推荐  <%=myCol.ColumnName%>
源代码网推荐  </TH>
源代码网推荐  <%
源代码网推荐  }
源代码网推荐  %>
源代码网推荐  </TR>
源代码网推荐  </THEAD>
源代码网推荐  <%
源代码网推荐  // 输出所有的纪录
源代码网推荐  myRows = myTable.Rows
源代码网推荐  for (myRow in myRows)
源代码网推荐  {
源代码网推荐  %>
源代码网推荐  <TR>
源代码网推荐  <%
源代码网推荐  for(var i:int=0;i<myColumns.Count;i++)
源代码网推荐  {
源代码网推荐  %>
源代码网推荐  <TD class="spec">
源代码网推荐  <%=myRow[i]%>
源代码网推荐  </TD>
源代码网推荐  <%
源代码网推荐  }
源代码网推荐  %>
源代码网推荐  </TR>
源代码网推荐  <%
源代码网推荐  }
源代码网推荐  %>
源代码网推荐  </TABLE>
源代码网推荐  
源代码网推荐  例子2
源代码网推荐  <%@ WebService Language="JScript" class="Weather"%>
源代码网推荐  import System
源代码网推荐  import System.Web.Services
源代码网推荐  class Weather {
源代码网推荐  WebMethodAttribute function getConditions(strCity : String) : String
源代码网推荐  {
源代码网推荐  var now = new Date();
源代码网推荐  switch (strCity.toUpperCase())
源代码网推荐  {
源代码网推荐  case "LONDON":
源代码网推荐  if (now.getMonth() <= 7||now.getMonth() >=9)
源代码网推荐  {
源代码网推荐  return "overcast"
源代码网推荐  }
源代码网推荐  if
源代码网推荐  {
源代码网推荐  return "partly overcast"
源代码网推荐  }
源代码网推荐  break;
源代码网推荐  case "SEATTLE":
源代码网推荐  if (now.getMonth() == 7 && now.getDay()==4)
源代码网推荐  {
源代码网推荐  return "torrential rain"
源代码网推荐  }
源代码网推荐  else
源代码网推荐  {
源代码网推荐  return "rain"
源代码网推荐  }
源代码网推荐  break;
源代码网推荐  case "LA":
源代码网推荐  return "smoggy"
源代码网推荐  break;
源代码网推荐  case "PHOENIX":
源代码网推荐  return "damn hot"
源代码网推荐  break;
源代码网推荐  default:
源代码网推荐  return "partly cloudy with a chance of showers"
源代码网推荐  }
源代码网推荐  }
源代码网推荐  }
源代码网推荐  
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华