asp.net环境下的链接点击计数--方案1



 

 

一、 首先创建网站工程。

在创建网站工程之前,必须安装ajax的扩展组件:ASPAJAXExtSetup.msi。安装之后打开vs2005,新建网站项目,在模板列表中点击ASP.NET AJAX-Enabled Web Site选项创建网站。如下图:



在网站项目的资源管理器窗口,创建文件opus.aspx,



打开opus.aspx文件,在该文件中额外添加了ScriptManager组件,代码如下:

<body>

    <form id="form1" runat="server">

        <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server">

            <Scripts>

                <asp:ScriptReference Path="~/activity/Clicks.js" />

            </Scripts>

            <Services>

                <asp:ServiceReference Path="~/WSClicks.asmx" />

            </Services>

        </asp:ScriptManager>

        <div id="opus" style="width: 672px; height: 402px; background: #e5e5e5; padding: 40px 0 40px 51px;">

            <asp:Repeater ID="Repeater1" runat="server">

                <ItemTemplate>

                    <dl>

                        <dt><a href="/activity/flash/<%#Eval("FF_FileName") %>" target="_blank"onclick="SuperLink_onClick(<%#Eval("FF_Id") %>)">

                            <img src="/activity/images/<%#Eval("FF_Thumnails")%>" /></a></dt>

                        <dd>

                            <a href="/activity/flash/<%#Eval("FF_FileName") %>" target="_blank" onclick="SuperLink_onClick(<%#Eval("FF_Id") %>)">

                                <%#Eval("FF_Description") %>

                            </a>

                        </dd>

                    </dl>

                </ItemTemplate>

            </asp:Repeater>

            <br />

            <cc1:Page04 ID="Page021" runat="server" Page_Index="opus.aspx" Page_Size="9" Page_Width="622"

                Height="402px" Page_Background="images/bg_38.jpg" Tagp_Background="images/bg_38.jpg">

            </cc1:Page04>

        </div>

    </form>

</body>

这一组件将ajax脚本和webservice文件关联起来。连接中的客户端事件onclick="SuperLink_onClick(<%#Eval("FF_Id") %>)"调用的是Clicks.js文件中的代码,该文件代码如下:

function SuperLink_onClick(id)

{

//这里调用webservice方法,调用格式如下:

//命名空间+类名+方法名(参数,,回调函数)

WebService1.WSClicks.IncreaseClicks(id,HandleStateChange);

//调用页面的后台方法,该方法必须是静态方法,调用格式同上。

    //PageMethods.IncreaseClicks(id,HandleStateChange);

}

//回调函数

function HandleStateChange(result)

{

    if(xmlHttp.readyState==4)

    {

        if(xmlHttp.status==200)

        {

            alert(result);

        }

    }

}

Webservice文件WSClicks.asmx的代码如下:

using System;

using System.Web;

using System.Collections;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Web.Script.Services;

using Maticsoft;

 

namespace WebService1

{

    /// <summary>

    /// WebService 的摘要说明

    /// </summary>

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    [ScriptService]

    public class WSClicks : System.Web.Services.WebService

    {

        public WSClicks()

        {

            //如果使用设计的组件,请取消注释以下行

            //InitializeComponent();

        }

 

        [WebMethod]

        public void IncreaseClicks(object FF_Id)

        {

            int id = Convert.ToInt32(FF_Id);

            Tb_FlashFile bll = new Tb_FlashFile();

            bll.GetModel(id);

            bll.FF_Clicks += 1;

            try

            {

                bll.Update();

            }

            catch

            {

            }

        }

    }

}

该文件注意三点:1、添加命名空间using System.Web.Script.Services;

2、在类WSClicks前添加属性[ScriptService]

3、在方法名IncreaseClicks前添加属性[WebMethod]

上述设置完毕,编译、运行即可。

整个流程是:点击链接,激发客户端事件onClick,事件调用ajax脚本文件中的方法(Clicks.js),ajax脚本方法调用webservice方法,webservice方法完成具体功能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值