[英语阅读笔记]Using Page Methods in ASP.NET AJAX

阅读: 评论:0

[英语阅读笔记]Using Page Methods in ASP.NET AJAX

[英语阅读笔记]Using Page Methods in ASP.NET AJAX

原文地址:.aspx
作者:Timothy Khouri

看标题很容易联想到updatepanel方式的页面方法调用,而文中所指的是在客户端异步调用webservice的方式.而且用了很简洁的代码来说明调用的方法.

作者用一个contact us页来描述.

首先建立一个表单:

Our "Contact Us" Form In ASP.NET

< form runat ="server">
   < asp:ScriptManager ID ="ScriptManager" runat ="server"
        EnablePageMethods ="true" />
   < fieldset id ="ContactFieldset">
       < label>
           Your Name
           < input type ="text" id ="NameTextBox" />< /label>
       < label>
           Email Address
           < input type ="text" id ="EmailTextBox" />< /label>
       < label>
           Your Message
           < textarea id ="MessageTextBox">< /textarea></label>
       < button onclick ="SendForm();">
           Send< /button>
   < /fieldset>
< /form>
  然后是webservice文件


Our Page Method Exposed to ASP.NET AJAX

using System;
using System.Web.Services;

public partial class ContactUs : System.Web.UI.Page
{
   [WebMethod]
    public static void SendForm( string name, string email, string message)
   {
        if ( string.IsNullOrEmpty(name))
       {
            throw new Exception( "You must supply a name.");
       }

        if ( string.IsNullOrEmpty(email))
       {
            throw new Exception( "You must supply an email address.");
       }

        if ( string.IsNullOrEmpty(message))
       {
            throw new Exception( "Please provide a message to send.");
       }

       // If we get this far we know that they entered enough data, so
       // here is where you would send the email or whatever you wanted
       // to do :)
   }
}


最后是表单文件里需要调用的javascript


function SendForm() {
    var name = $get( "NameTextBox").value;
    var email = $get( "EmailTextBox").value;
    var message = $get( "MessageTextBox").value;

   PageMethods.SendForm(name, email, message,
       OnSucceeded, OnFailed);
}

function OnSucceeded() {
   // Dispaly "thank you."
   $get( "ContactFieldset").innerHTML = "<p>Thank you!</p>";
}

function OnFailed(error) {
   // Alert user to the error.
   _message());
}


所有的一切经过原文作者描述后会发现,原来是这么简单.

转载于:.html

本文发布于:2024-02-04 13:35:14,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170708455256038.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:英语阅读   笔记   Page   Methods   NET
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23