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);
    }

}
posted on 2008-05-23 10:30  guojin  阅读(367)  评论(1编辑  收藏  举报