Posted on 2007-10-10 08:50
webabcd 阅读(7222)
评论(21) 编辑 收藏
[索引页]
[源码下载]
步步为营VS 2008 + .NET 3.5(2) - VS 2008新特性之JavaScript Intellisense and Debugging(JavaScript的智能感知和调试)
作者:
webabcd
介绍
VS 2008可以非常完美地支持JavaScript和ASP.NET AJAX的智能感知和调试。.NET 3.5内置了ASP.NET AJAX,并且UpdatePanel终于可以支持WebPart了。
示例
Feature.js(被aspx页引用的js文件)
// 创建一个math类
window.math = function()


{
/// <summary>数学函数类</summary>
}

window.math.prototype =


{
// 为math类创建一个max方法
max: function(x, y)

{
/// <summary>返回两个整数中的最大的一个</summary>
/// <param name="x">需要比较的第一个整数</param>
/// <param name="y">需要比较的第二个整数</param>
if (x > y)
return x;
else
return y;
}
}
Feature2.js(在js文件中智能感知外部js文件的JavaScript和ASP.NET AJAX)
/// <reference path="Feature.js" />
/// <reference name="MicrosoftAjax.js" />

// 外部js文件用这种方法引进来<reference path="Feature.js" />
// <asp:scriptmanager>控件引入的js用这种方法引进来<reference name="MicrosoftAjax.js" />

function refTest()


{
// 因为有了“<reference path="Feature.js" />”,所以会感知到Feature.js提供的JavaScript
var m = new window.math();
var v = m.max(x, y);
// 因为有了“<reference name="MicrosoftAjax.js" />”,所以会感知到ASP.NET AJAX
// $get("testIntellisense");
}

WebServiceMath.asmx(为ASP.NET AJAX提供服务的WebService)

<%
@ WebService Language="C#" Class="WebServiceMath" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Description = "WebService提供的数学函数类", Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebServiceMath : System.Web.Services.WebService
{
/// <summary>
/// 返回两个整数中的最大的一个
/// </summary>
/// <param name="x">需要比较的第一个整数</param>
/// <param name="y">需要比较的第二个整数</param>
/// <returns></returns>
[WebMethod(Description = "返回两个整数中的最大的一个")]
public int Max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
}


JavaScript.aspx

<%
@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="JavaScript.aspx.cs"
Inherits="Feature_JavaScript" Title="JavaScript的智能感知和调试(JavaScript Intellisense and Debugging)" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">

<script src="../JS/Feature.js" type="text/javascript"></script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div style="width: 600px">
VS 2008可以非常完美地支持JavaScript和ASP.NET AJAX的智能感知和调试。.NET 3.5内置了ASP.NET AJAX,并且UpdatePanel终于可以支持WebPart了。
</div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="WebServiceMath.asmx" />
</Services>
</asp:ScriptManager>


<script type="text/javascript">
var x, y;
x = 100;
y = 101

// 新建一个math对象
// 此处输入任意字母,就会出现JavaScript的智能感知
// 输入window.就会感知到Feature.js文件里创建的math类,并提示math类的summary - 数学函数类
var m = new window.math();
// 取x,y之间的最大值
// 输入m.就会感知到math对象的max方法,并提示max方法的summary - 返回两个整数中的最大的一个
// 输入max方法的参数的时候,会提示两个参数的说明 - 需要比较的第一个整数;需要比较的第二个整数
var v = m.max(x, y);
alert(v);
// 可以在任意JavaScript语句处插入断点,然后就可以对JavaScript进行调试
// ASP.NET AJAX调用web service
// WebServiceMath会被智能感知出来
// 输入WebServiceMath.后,就会感知到Max方法
// 输入Max方法后,就会有参数提示 - x, y, onSuccess, onFailed, userContext
WebServiceMath.Max(x, y, onSuccess);
function onSuccess(result)

{
alert(result);
}
OK
[源码下载]
Feedback
VS08的javascript的感知功能完善吗?还有就是能不能在aspx文件里调试啊?
我现在用这台机速度还跟不上,没机会装个08试试,。。。。。。。。
兄弟什么时候有空写一个支持自定义属性的TextBox控件,发上来让大家学习学习。。
@ivw
给我的感觉来说js的感知能做到vs2008的水平就已经很不错了
可以在aspx页中对js语句插断点
关于自定义属性的TextBox控件还没开发,主要原因是自定义属性不只会TextBox用到,其他的控件也可能会用到,所以想单独做一个增强现有控件功能的一个通用控件,其中一些逻辑和使用的方式以及今后的扩展还没想的太周到,所以就没开工。等我想周全了之后就会开始做
支持一下
不过,我用vs2005的时候就有js的智能感知啊
@webabcd
@黑白
是的
vs2005关于js的感知是无法感知到你写的function的
晕啊
js注释还要全手写出来,不能像C#那样输入3个/就全出来吗?
@黑白
现在是这样的
期待着正式版出来后会有所改善
updatapanel 和 webpart 用在一起时候出现只能拖动一次的情况,3.5的,没有sharepoint,不知道怎么解决
@管宇
把
WebPartManager
也放到
UpdatePanel
里
可是我在aspx文件的js里设断点,然后调试,仍然进步了断点的地方啊,为什么呢?
我对js不是很懂 但我完全是按照楼主的例子做的 为什么我的不能自动感知外部js文件呢?
@soldierluo
嗯。。。
这个我也解释不了了,不过我这里确实是没问题的
知道原因了
原来是这两句的问题
/// <reference path="Feature.js" />
/// <reference name="MicrosoftAjax.js" />
我还以为是注释呢,就没打上去了,怪不得看得我莫名其妙
@soldierluo
:)
呵呵
这个例子里3个/的都有用,2个/的是注释