asp.net客户端回调

在ASP.NET中要实现页面的部分回发,除了使用AJAX外还可以使用客户端回调。

使用场景举例:一个很大的TreeView,初始化时只加载根节点,点击节点时加载子结点。

以下是简单的示例代码:(要注意脚本安全验证)


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

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 
  1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
>

< html   >
< head  id ="Head1"  runat ="server" >
    
< title > ASP.NET Example </ title >
< script  type ="text/javascript" >     
    
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  id ="LookUpStockButton"  onclick ="LookUpStock()" > Look Up Stock </ button >
      
< asp:LoginView  id ="LoginView1"  runat ="server" >
      
< LoggedInTemplate >
         
< button  id ="LookUpSaleButton"  onclick ="LookUpSale()" > Look Up Back Order </ button >
      
</ LoggedInTemplate >
      
</ asp:LoginView >
      
< br  />
      Item status: 
< span  id ="Results" ></ span >
    
</ div >
  
</ form >
</ body >
</ html >
CodeBehind:

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  ClientCallback : System.Web.UI.Page,
     System.Web.UI.ICallbackEventHandler
{
    
protected System.Collections.Specialized.ListDictionary catalog;
    
protected System.Collections.Specialized.ListDictionary saleitem;
    
protected String returnValue;
    
protected String validationLookUpStock = "LookUpStock";
    
protected String validationLookUpSale = "LookUpSale";
    
protected void Page_Load(object sender, EventArgs e)
    
{
        Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
            validationLookUpStock, 
"function LookUpStock() {  " +
            
"var lb = document.forms[0].ListBox1; " +
            
"var product = lb.options[lb.selectedIndex].text;  " +
            
@"CallServer(product, ""LookUpStock"");}  "true);
        
if (User.Identity.IsAuthenticated)
        
{
            Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
            validationLookUpSale, 
"function LookUpSale() {  " +
            
"var lb = document.forms[0].ListBox1; " +
            
"var product = lb.options[lb.selectedIndex].text;  " +
            
@"CallServer(product, ""LookUpSale"");} "true);
        }


        String cbReference 
= "var param = arg + '|' + context;" + 
            Page.ClientScript.GetCallbackEventReference(
this,
            
"param""ReceiveServerData""context");
        String callbackScript;
        callbackScript 
= "function CallServer(arg, context)" +
            
"" + cbReference + "} ;";
        Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
            
"CallServer", callbackScript, true);

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

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

    
public void RaiseCallbackEvent(String eventArgument)
    
{
        
string[] argParts = eventArgument.Split('|');
        
if ((argParts == null|| (argParts.Length != 2))
        
{
            returnValue 
= "A problem occurred trying to retrieve stock count.";
            
return;
        }

        
string product = argParts[0];
        
string validationaction = argParts[1];
        
switch (validationaction)
        
{
            
case "LookUpStock":
                
try
                
{
                    Page.ClientScript.ValidateEvent(
"LookUpStockButton", validationaction);
                    
if (catalog[product] == null)
                    
{
                        returnValue 
= "Item not found.";
                    }

                    
else
                    
{
                        returnValue 
= catalog[product].ToString() + " in stock.";
                    }

                }

                
catch
                
{
                    returnValue 
= "Can not retrieve stock count.";
                }
 
                
break;
            
case "LookUpSale":
                
try
                
{
                    Page.ClientScript.ValidateEvent(
"LookUpSaleButton", validationaction);
                    
if (saleitem[product] == null)
                    
{
                        returnValue 
= "Item not found.";
                    }

                    
else
                    
{
                        
if (Convert.ToBoolean(saleitem[product]))
                            returnValue 
= "Item is on sale.";
                        
else
                            returnValue 
= "Item is not on sale.";
                    }

                }

                
catch
                
{
                    returnValue 
= "Can not retrieve sale status.";
                }

                
break;
        }


    }

    
public String GetCallbackResult()
    
{
        
return returnValue;
    }

    
protected override void Render(HtmlTextWriter writer)
    
{
        Page.ClientScript.RegisterForEventValidation(
"LookUpStockButton",
            validationLookUpStock);
        
if (User.Identity.IsAuthenticated)
        
{
            Page.ClientScript.RegisterForEventValidation(
"LookUpSaleButton",
                validationLookUpSale);
        }

        
base.Render(writer);
    }

}

转载于:https://www.cnblogs.com/guojin705/archive/2008/05/23/1205532.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值