ASP.NET2. 0  开发无刷新页面
在已经发布的 ASP.NET2. 0  中,越来越多的 Ajax 开发包被开发出来的情况下, ASP.NET2. 0  自带的无刷新页面技术没有被很多人了解,甚至不少人认为该功能有些“鸡肋”。但如果我们仅仅是在程序中加入很少部分的 Ajax 特性, Atlas 、 Ajax.Net 等就显得有些“杀鸡用牛刀”的感觉了。而且,我认为使用 ASP.NET2. 0  提供的方法进行开发并不很复杂,相反,使用很少的代码就可以做出来很棒的效果! 
   下面我来一步一步的带大家开发无刷新的页面! 

 第一步:实现 ICallBackEventHandler 接口 

   ICallbackEventHandler接口位于System.Web.UI命名空间下。在beta2时,ICallbackEventHandler只包含一个RaiseCallbackEvent方法,即处理回调事件,又返回处理结果。在正式版中,它变成了包含GetCallbackResult和RaiseCallbackEvent两个成员方法,第一个用来返回回调事件的结果,第二个用来出来回调事件。这个变化主要是为了编写Web控件而做的改动,具体可以看一下GridView等控件中的实现代码。 

建立一个 Web 网站,我们来修改  default .aspx.cs 文件:



  1          public   partial   class   _Default : System.Web.UI.Page, ICallbackEventHandler 


  1       private   string  str;
  2       public   void  RaiseCallbackEvent( string  eventArgument)
  3       {
 4         //可以根据传递的参数不同,调用不同的处理逻辑
 5         str = "从服务器端返回的内容:" + eventArgument;
 6     }
  7  
  8       public   string  GetCallbackResult()
  9       {
10         return str;
11     }
12  
  第二步:注册回调方法    我们在  default .aspx 页面中添加一个 TextBox ,一个 Label 和一个 Html 控件 Button ,并给 Button 添加 onclick事件:


1   < asp:TextBox ID = " TextBox1 "  runat = " server " ></ asp:TextBox >
2   < input id = " Button1 "  type = " button "  value = " 提交到Label1 "  onclick = " CallServer(TextBox1, Label1) " />< br />
3   < asp:Label ID = " Label1 "  runat = " server "  Text = " Label1: " ></ asp:Label >
4  


  1       < script type = " text/javascript " >  
  2          // 由button调用
  3         function CallServer(inputcontrol, context)
  4         
 5             context.innerHTML = "Loading";
 6             arg = inputcontrol.value;
 7             //注册回调方法
 8             <%= ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context")%>; 
 9         }
10           // 在回调方法中注册的接收返回结果的函数
11          function ReceiveServerData(result, context) 
12           {
13             context.innerHTML = result;
14         }
15       </ script >
16  


   好了,一个无刷新的页面就开发完了,它可以将你在 TextBox 中输入的文字,通过服务器代码写回到页面的 Label 中。是不是很简单?你可以运行一下你的程序看看效果啦! 

   下面我们来分析一下这些代码。    首先,我们看


<%=  ClientScript.GetCallbackEventReference( this ,  " arg " ,  " ReceiveServerData " ,  " context " ) %> ;


   ClientScript是System.Web.UI.Page对象的一个属性,它是System.Web.UI.ClientScriptManager对象。用于管理客户端脚本,GetCallbackEventReference方法用于注册一个服务器端事件的客户端回调。它的第四个参数“Context”非常重要,在上面的代码中可以看到,调用CallServer方法时,传递的Context参数就是Label1,而ReceiveServerData的第二个参数“Context”就是被传递过来的Label1。在我的例子中,Context被用于设定一个用来显示服务端返回结果的控件。其实,你可以将任意的对象赋值给Context,它都会被传递给本地端处理回调返回结果的函数,这样,你就可以根据调用前指定的“上下文”灵活的操作返回结果了!在下面给出的完整例子中,你可以看到一个使用Context做的无刷新显示GridView的例子。


      OK,在ASP.NET  2 .0中开发具有Ajax特性的东东不难吧!其实就是两步: 

1 、              在Server端实现ICallbackEventHandler接口,在接口包含的方法中根据传递的参数分别调用不同的处理方法,然后返回结果; 

2 、              在Client端注册回调函数(当然你也可以在Server端注册),然后实现处理回调结果的函数。其中,如果对Context能灵活运用,你就可以做出非常好的效果。 

  

.ASPX代码 

<% @ Page Language = " C# "  AutoEventWireup = " true "   CodeFile = " Default.aspx.cs "  Inherits = " _Default "   %>  

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >  

< html xmlns = " http://www.w3.org/1999/xhtml "   >
< head runat = " server " >
     < title > ASP.NET  2.0  页面提交无刷新演示 </ title >
     < script type = " text/javascript " >  
       function CallServer1(inputcontrol, context)
       
            context.innerHTML = "<IMG SRC='images/pie.gif' />Loading";
            arg = 'ServerMethod1|' + inputcontrol.value;
            <%= ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData1", "context")%>; 
        }
        
        function ReceiveServerData1(result, context) 
         {
            context.innerHTML = context.id + ":" + result;
        }
        
        function CallServer2(obj)
         {
            context = gridspan;
            context.innerHTML = "<IMG SRC='images/pie.gif' />数据加载中";
            arg = "ServerMethod2|" + obj.value;
            <%= ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData2", "context")%>;
        }
        
        function ReceiveServerData2(result, context)
         {
            context.innerHTML = result;
        }
        
     </ script >
</ head >
< body >
     < form id = " form1 "  runat = " server " >
     < div >
         < h1 > Demo1:html按钮提交数据 </ h1 >< br  />
         < asp:TextBox ID = " TextBox1 "  runat = " server " ></ asp:TextBox >
         < input id = " Button1 "  type = " button "  value = " 提交到Label1 "  onclick = " CallServer1(TextBox1, Label1) " />& nbsp;
         < input id = " Button2 "  type = " button "  value = " 提交到Label2 "  onclick = " CallServer1(TextBox1, Label2) " />
         < br  />
         < asp:Label ID = " Label1 "  runat = " server "  Text = " Label1: " ></ asp:Label >
         < br  />
         < asp:Label ID = " Label2 "  runat = " server "  Text = " Label2: " ></ asp:Label >
     </ div >
     < hr  />
     < div >
         < h1 > Demo2:服务器按钮提交数据 </ h1 >< br  />
         < asp:TextBox ID = " TextBox2 "  runat = " server " ></ asp:TextBox >
         < asp:Button ID = " Button3 "  runat = " server "  Text = " Button "   />< br  />
         < asp:Label ID = " Label3 "  runat = " server "  Text = " Label3: " ></ asp:Label ></ div >     
     < hr  />
     < div >
         < h1 > Demo3:下拉列表框和gridview绑定数据 </ h1 >< br  />
         < asp:SqlDataSource ID = " SqlDataSource1 "  runat = " server "  ConnectionString = " <%$ connectionStrings:test %> "
            SelectCommand = " select distinct(country) from customers " ></ asp:SqlDataSource >
         < asp:SqlDataSource ID = " SqlDataSource2 "  runat = " server "  ConnectionString = " <%$ connectionStrings:test %> "
            SelectCommand = " select customerid, companyname, country from customers where country=@Country " >
             < SelectParameters >
                 < asp:ControlParameter Name = " Country "  ControlID = " DropDownList1 "  PropertyName = " SelectedValue "   />
             </ SelectParameters >
         </ asp:SqlDataSource >
         < div >
             < asp:DropDownList ID = " DropDownList1 "  runat = " server "  Width = " 239px "  
                DataSourceID = " SqlDataSource1 "  DataTextField = " Country "  DataValueField = " Country " >
             </ asp:DropDownList >
         </ div >
         < br  />
         < span id = " gridspan " >
             < asp:GridView ID = " GridView1 "  runat = " server "  DataSourceID = " SqlDataSource2 " >
             </ asp:GridView >
         </ span >
     </ div >
     </ form >
</ body >
</ html >  


.CS代码 

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.IO;
using  System.Globalization; 

public  partial  class  _Default : System.Web.UI.Page, ICallbackEventHandler
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //注册客户端事件处理方法
        Button3.Attributes.Add("onclick", "CallServer1(TextBox2, Label3);return false;");
        DropDownList1.Attributes.Add("onchange", "CallServer2(this)");
    } 

    private string serverReturn; 

    public string GetCallbackResult()
    {
        //为便于查看加载效果,添加延时
        System.Threading.Thread.Sleep(2000);
        
        string[] parts = serverReturn.Split('|');
        //根据传递的方法名进行调用,并传递相应的参数,目前只支持一个参数
        return (string)GetType().GetMethod(parts[0]).Invoke(this, new object[] { parts[1] }); 
    } 

    public void RaiseCallbackEvent(string eventArgument)
    {
        serverReturn = eventArgument;
    } 

    //根据从客户端传来的英文国家名或缩写,翻译成相应的中文国家名
    public string ServerMethod1(string arg)
    {
        string s;
        switch (arg.ToLower())
        {
            case "cn":
            case "china":
                s = "中国";
                break;
            case "us":
                s = "美国";
                break;
            default:
                s = "未知国家";
                break;
        }
        return s;
    } 

    //根据从客户端传来的值,对GridView的内容进行更新,并将更新后的GridView的html返回
    public string ServerMethod2(string arg)
    {
        DropDownList1.SelectedValue = arg;
        GridView1.DataBind(); 

        return RenderControl(GridView1);
    } 

    private string RenderControl(Control control)
    {
        StringWriter writer1 = new StringWriter(CultureInfo.InvariantCulture);
        HtmlTextWriter writer2 = new HtmlTextWriter(writer1); 

        control.RenderControl(writer2);
        writer2.Flush();
        writer2.Close(); 

        return writer1.ToString();
    }
}

webconfig 

< configuration >
  < appSettings />
   < connectionStrings >
     < add name = " test "  connectionString = " server=maxwin;Integrated security=true;database=TestCallBackAjax " />
   </ connectionStrings >
  < system.web >
   < compilation debug = " true " />
  </ system.web >
</ configuration >

转载于:https://www.cnblogs.com/gentlewolf/archive/2007/08/05/844056.html

ASP.NET2.0 开发无刷新页面相关推荐

  1. ASP.NET2.0实现无刷新客户端回调

    Asp.Net2.0的客户端回调是一种很让人激动的方法,他能够让我们控制要提交什么数据给服务器而不用提交整个页面,同时服务器也只返回你所需要的数据而不要发回整个页面. 首先我们要说一个很重要的方法:G ...

  2. Asp.NET 2.0中无刷新页面的开发

    .NET 2.0正式版中无刷新页面的开发 在已经发布的 ASP.NET2.0 中,无刷新页面开发相关部分同 beta2 有不少改动.而且在越来越多的 Ajax 开发包被开发出来的情况下, ASP.NE ...

  3. 在.NET 2.0正式版中开发无刷新页面

    在已经发布的 ASP.NET2.0 中,无刷新页面开发相关部分同 beta2 有不少改动.而且在越来越多的 Ajax 开发包被开发出来的情况下, ASP.NET2.0 自带的无刷新页面技术没有被很多人 ...

  4. ASP.NET2.0中的全球化与本地化UICulture,Culture

    ASP.NET2.0中的全球化与本地化UICulture,Culture 来源:转载 日期:2007-2-28 14:59:01 阅读:324 评论:0   一.简介 全球化和本地化是每一位开发者在创 ...

  5. ASP.NET 2.0 正式版中无刷新页面的开发

    ASP.NET 2.0 正式版中无刷新页面的开发 在已经发布的 ASP.NET2.0 中,无刷新页面开发相关部分同 beta2 有不少改动.而且在越来越多的 Ajax 开发包被开发出来的情况下, AS ...

  6. ASP.NET 2.0 正式版中无刷新页面(客户端回调)的开发

    在已经发布的 ASP.NET2.0 中,无刷新页面开发相关部分同 beta2 有不少改动.而且在越来越多的 Ajax 开发包被开发出来的情况下, ASP.NET2.0 自带的无刷新页面技术没有被很多人 ...

  7. .net2.0无刷新页面

    [此文来自于互联网] 在已经发布的 ASP.NET2.0 中,无刷新页面开发相关部分同 beta2 有不少改动.而且在越来越多的 Ajax 开发包被开发出来的情况下, ASP.NET2.0 自带的无刷 ...

  8. ASP.NET2.0自定义控件组件开发 第六章 深入讲解控件的属性

    深入讲解控件的属性持久化(一) 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开发 第一章 第二篇 接着待续 ASP.NET自定义控件组件开发 第一章 第 ...

  9. ASP.NET2.0国际化/本地化应用程序的实现总结(多语言,多文化页面的实现)

    相关文章导航 Sql Server2005 Transact-SQL 新兵器学习总结之-总结 Flex,Fms3相关文章索引 FlexAir开源版-全球免费多人视频聊天室,免费网络远程多人视频会议系统 ...

最新文章

  1. java前台传多个id用什么接收_jsp 页面传多个id 到java后台的处理方式
  2. sql_INSERT DELETE
  3. hdu4982 暴搜+剪枝(k个数和是n,k-1个数的和是平方数)
  4. 聊聊Batch Normalization在网络结构中的位置
  5. vmware搭建ubuntu虚拟机(包含安装搜狗输入法和apt,dpkg的使用教程)
  6. halcon求取区域顶点
  7. 为什么 Linux 上的 Asp.NET 5 需要 Kestrel ?
  8. Codeforces Round #556 (Div. 1)
  9. 互联网大事件:《江南style》如何蹿红?(转)
  10. Java —— eclipse.zip
  11. linux卸载openJDK
  12. QtCreator中导入“.lib(.a)”和“.dll(.so)”文件的方法
  13. webstorm设置注释颜色_PDF中的注释怎么用?这里有方法
  14. 《人人都该买保险》读书笔记
  15. python如何进行人口预测_如何使用matlab建立人口预测模型
  16. python数据分析入门到实战 知了课堂 百度云_零基础入门Python数据分析,只需要看懂这一张图,附下载链接!...
  17. ESP8266 WIFI模块学习基础入门
  18. Solidworks或CAD怎样输出高质量图片?
  19. 计算机三级数据库应用题设计题,计算机考试三级数据库练习题
  20. Bigemap GIS Office软件 报价单

热门文章

  1. 2013大佬扎堆:难以忽视的互联网变局
  2. IDEA Unable to ping server at 1099问题
  3. 使用 Python 进行年龄和性别检测,这个结果搞笑了
  4. centos 7 内核参数优化
  5. matlab中画带箭头向量,几何画板中如何画带箭头的向量
  6. Java里那些高大上的名称
  7. css多行文本溢出省略号显示
  8. Python输出十二星座的符号
  9. 怎么理解后App时代的轻应用技术
  10. 华为console密码重置