回调实现无刷新级联下拉框

以northwind数据库为例。选择分类下拉框的一项后,无刷新更改产品下拉框的显示

前台代码:

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

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< head  runat ="server" >
    
< title > 无标题页 </ title >
    
< script  type ="text/javascript" >
        
function CallServer(args,context)
        
{
            context.innerHTML 
= "Loading...";
            
<%= ClientScript.GetCallbackEventReference(this,"args","GetServerData","context"%>
        }

        
function GetServerData(result,context)
        
{
            context.innerHTML 
= result;
        }

    
</ script >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
        
< asp:DropDownList  ID ="ddlCategory"  runat ="server" ></ asp:DropDownList >
        
< span  id ="pnlProduct" >
        
< asp:DropDownList  ID ="ddlProduct"  runat ="server" ></ asp:DropDownList >
        
</ span >
    
</ div >
    
</ form >
</ body >
</ html >

后台代码:

using  System;
using  System.Data;
using  System.Data.SqlClient;
using  System.IO;
using  System.Configuration;
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  _Default : System.Web.UI.Page,ICallbackEventHandler
{
    
protected string callbackResult;

    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            BindToCategory();
            
this.ddlCategory.Attributes.Add("onchange""CallServer(this.value,pnlProduct)");
            
this.ddlProduct.Items.Add(new ListItem("--产品--""-1"));
        }

    }


    
/// <summary>
    
/// 绑定分类
    
/// </summary>

    private void BindToCategory()
    
{
        SqlConnection conn 
= new SqlConnection("Server=(local);database=northwind;uid=sa;pwd=123");     
        SqlDataAdapter sda 
= new SqlDataAdapter("SELECT categoryID,categoryName FROM categories", conn);
        DataTable dt 
= new DataTable();
        sda.Fill(dt);
        
this.ddlCategory.DataSource = dt;
        
this.ddlCategory.DataTextField = "categoryName";
        
this.ddlCategory.DataValueField = "categoryID";
        
this.ddlCategory.DataBind();
        
this.ddlCategory.Items.Insert(0new ListItem("--分类--""-1"));
    }


    
private void BindToProduct(int categoryID)
    
{
        SqlConnection conn 
= new SqlConnection("Server=(local);database=northwind;uid=sa;pwd=123");
        SqlDataAdapter sda 
= new SqlDataAdapter("SELECT productID,productName FROM products WHERE categoryID=" + categoryID, conn);
        DataTable dt 
= new DataTable();
        sda.Fill(dt);
        
this.ddlProduct.DataSource = dt;
        
this.ddlProduct.DataTextField = "productName";
        
this.ddlProduct.DataValueField = "productID";
        
this.ddlProduct.DataBind();
        
if (ddlProduct.Items.Count == 0)
        
{
            
this.ddlProduct.Items.Add(new ListItem("--产品--""-1"));
        }

    }


    
private string RenderElement(Control control)
    
{
        StringWriter writer 
= new StringWriter();
        HtmlTextWriter output 
= new HtmlTextWriter(writer);
        control.RenderControl(output);
        output.Flush();
        output.Close();
        
string htmlCode = writer.ToString();
        writer.Close();
        
return htmlCode;
    }


    
void ICallbackEventHandler.RaiseCallbackEvent(string str)
    
{
        
this.callbackResult = str;
    }


    
string ICallbackEventHandler.GetCallbackResult()
    
{
        
int categoryID = Convert.ToInt32(callbackResult);
        BindToProduct(categoryID);
        callbackResult 
= RenderElement(this.ddlProduct);
        
return callbackResult;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值