ASP.NET 2.0 中的无刷新页面

 这几天在微软网站看了一编客户端回客户端回调实现 (C#) 示例 
(网址为:http://msdn2.microsoft.com/zh-CN/library/ms178210.aspx
用它就能实现基本Ajax的原System.Web.UI.ICallbackEventHandler理功能,而不学习一些Ajax框架的东西,感觉挺爽呀,可是运行起报错,后经过仔细查找原来是System.Web.UI.ICallbackEventHandler的接口不是它那样写的,可能它是原来的接口吧。后我照2.0的接口改成下面的样子,运行吗ok,是一个不错无刷新技术。
它的基本原理,微软的试例已经介绍非常祥细了,在此我就不搬门弄斧了,有兴趣的朋友去,去http://www.cnblogs.com/RicCC/archive/2006/09/10/500268.html这个网址看看吧

<%@ 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>无标题页</title>
  
<script type="text/javascript">
    function LookUpStock()
    
{
        var lb 
= document.forms[0].ListBox1;
        var product 
= lb.options[lb.selectedIndex].text 
        CallServer(product, 
"");
    }

    
    function ReceiveServerData(rValue)
    
{
        Results.innerText 
= rValue;
    }

  
</script>
</head>
<body>
  
<form id="form1" runat="server">
    
<div>
      
<asp:ListBox ID="ListBox1" Runat="server"></asp:ListBox>
      
<br />
      
<br />
      
<button onclick="LookUpStock()">Look Up Stock</button>
      
<br />
      
<br />
      Items 
in stock: <span ID="Results"></span>
      
<br />
    
</div>
  
</form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;


public partial class _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
    
protected System.Collections.Specialized.ListDictionary catalog;

    
protected void Page_Load(object sender, EventArgs e)
    
{
        String cbReference 
=
            Page.ClientScript.GetCallbackEventReference(
this,
            
"arg""ReceiveServerData""context");
        String callbackScript;
        callbackScript 
= "function CallServer(arg, context)" +
            
"" + cbReference + "} ;";
        Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
            
"CallServer", callbackScript, true);

        catalog 
= new System.Collections.Specialized.ListDictionary();
        catalog.Add(
"monitor"12);
        catalog.Add(
"laptop"10);
        catalog.Add(
"keyboard"23);
        catalog.Add(
"mouse"17);

        ListBox1.DataSource 
= catalog;
        ListBox1.DataTextField 
= "key";
        ListBox1.DataBind();
    }


    String returnValue;
    
public void RaiseCallbackEvent(String eventArgument)
    
{
        
        
if (catalog[eventArgument] == null)
        
{
            returnValue 
= "-1";
        }

        
else
        
{
            returnValue 
= catalog[eventArgument].ToString();
        }

        
//
    }


    
public string GetCallbackResult()
    
{
     
return returnValue;
    }



}

posted @ 2006-09-10 08:50  不可以  阅读(466)  评论(0编辑  收藏  举报
使用Live Messenger联系我
关闭