ASP.NET 2.0中callback的一些變化+使用示例(下)

本文地址:http://qzone.qq.com/blog/101766959-1217964961

 
 
Callback.aspx.cs:
using System;
using System.IO;
using System.Collections;
using System.Globalization;
using System.Reflection;
using System.Web;
using System.Web.UI;
 
public partial class Callback : Page, ICallbackEventHandler
{
    private string _callbackEventArgument;
 
    protected void Page_Load(object sender, EventArgs e)
    {
               ddlCategory.Attributes.Add("onchange", "CallServer('FillProduct|'+this.value,_span1)");
               ddlProduct.Attributes.Add("onchange", "CallServer('ShowBuy|'+this.value,_span2)");
           }
 
 
    ICallbackEventHandler Members#region ICallbackEventHandler Members
 
    public string GetCallbackResult()
    {
        string[] parts = _callbackEventArgument.Split('|');
        object[] args = null;
        string result = "";
 
        if (parts.Length > 1)
        {
                   args = new object[parts.Length - 1];
                   Array.Copy(parts, 1, args, 0, args.Length);
               }
 
               MethodInfo method = this.GetType().GetMethod(parts[0]);
 
        if (method != null)
        {
                   result = (string)method.Invoke(this, args);
               }
 
        return result;
           }
 
    public void RaiseCallbackEvent(string eventArgument)
    {
               _callbackEventArgument = eventArgument;
           }
 
    void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
    {
        this.RaiseCallbackEvent(eventArgument);
           }
 
    string ICallbackEventHandler.GetCallbackResult()
    {
        return this.GetCallbackResult();
           }
 
    #endregion
 
 
    public string FillProduct(string categoryID)
    {
               ddlCategory.SelectedValue = categoryID;
               ddlProduct.DataBind();
 
        return RenderControl(ddlProduct);
           }
 
    public string ShowBuy(string ProductID)
    {
               btnBuy.Enabled = !string.IsNullOrEmpty(ProductID);
 
        return RenderControl(btnBuy);
           }
 
    protected void btnBuy_Click(object sender, EventArgs e)
    {
               _div1.Visible = false;
               Label1.Text = "Buy: " + Request.Form[ddlProduct.UniqueID];
           }
 
    private string RenderControl(Control control)
    {
               StringWriter writer1 = new StringWriter(CultureInfo.InvariantCulture);
               HtmlTextWriter writer2 = new HtmlTextWriter(writer1);
 
               control.RenderControl(writer2);
               writer2.Flush();
               writer2.Close();
 
        return writer1.ToString();
           }
}

 

 

ASP.NET 2.0中callback的一些變化+使用示例(上)

ASP.NET 2.0中callback的一些變化+使用示例(中)

ASP.NET 2.0中callback的一些變化+使用示例(下)

創作者介紹

歡迎~ 程式設計 學生 作業 專題製作 程式外包 家教...

goodlucky 發表在 痞客邦 PIXNET 留言(0) 人氣()