web自定义控件是否能有自己的AJAX---asp.net2.0

解决方案 »

  1.   

    用AJAXPRO可以在前台JS调用后台方法
      

  2.   

    给你段代码,
    testAjax.ascx<%@ Control Language="C#" AutoEventWireup="true" CodeFile="testAjax.ascx.cs" Inherits="testAjax" %>
        <script   type="text/javascript">   
              function   LookUpStock()   
              {   
                      var   product   =   'ppp';     
                      CallServer(product,   "");   
              }   
                
              function   ReceiveServerData(rValue)   
              {   
                      Results.innerText   =   rValue;   
              }   
          </script>   
        
      <table>   
              <tr>   
                      <td>   
                  <button   onclick="LookUpStock()">Look   Up   Stock</button>   
                  <br   />   
                  <br   />   
                  Items   in   stock:   <span   ID="Results"></span>   
                  <br   />   
        
                      </td>   
              </tr>   
      </table>testAjax.ascx.csusing 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 testAjax : System.Web.UI.UserControl, ICallbackEventHandler
    {
        private string str;    protected void Page_Load(object sender, EventArgs e)
        {
            String cbReference =
                    this.Page.ClientScript.GetCallbackEventReference(this,
                    "arg", "ReceiveServerData", "context");
            String callbackScript;
            callbackScript = "function   CallServer(arg,   context)" +
                    "{   " + cbReference + "}   ;";
            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
                    "CallServer", callbackScript, true);
        }    protected void Button2_Click(object sender, EventArgs e)
        {    }    string ICallbackEventHandler.GetCallbackResult()
        {
            return str + "xxx";
        }    void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
        {
            str = eventArgument;
        }
    }