zoukankan      html  css  js  c++  java
  • 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);
        }

    }
  • 相关阅读:
    运行 npm run dev 不能自动打开浏览器
    npm run dev 报错:Strings must use singlequote 的解决方法
    new和this
    new Object()、Object.create()、{}三种对象创建方式的区别
    Python 详解修饰器 附带 js使用修饰器
    Python
    react项目使用axios和Charles模拟数据接口
    react切换隐藏或显示状态(包含过渡动画)
    react里使用ref的几种方法
    js对象转数组
  • 原文地址:https://www.cnblogs.com/guojin705/p/1205532.html
Copyright © 2011-2022 走看看