DEV checkbox PerformCallback实现级联 ,无刷新操作

<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="ClientAPI.aspx.cs"
    Inherits="ASPxComboBox_ClientAPI" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentHolder" runat="Server">
    <script type="text/javascript">
    // <![CDATA[
        var lastCountry = null;
        function OnCountryChanged(cmbCountry) {
            if (cmbCity.InCallback() ) {
                lastCountry = cmbCountry.GetValue().toString();
                }
                else { 
                    cmbCity.PerformCallback(cmbCountry.GetValue().toString());
                }
        }
        function OnEndCallback(s,e){ 
            if (lastCountry){
                cmbCity.PerformCallback(lastCountry);
                lastCountry = null;
            }
        } 
    // ]]> 
    </script>
    <table class="OptionsTable">
        <tr>
            <td>
                <dx:ASPxLabel runat="server" Text="Country:" ID="CountryLabel" AssociatedControlID="CmbCountry" />
            </td>
            <td>
                <dx:ASPxComboBox runat="server" ID="CmbCountry" DropDownStyle="DropDownList" IncrementalFilteringMode="StartsWith"
                    DataSourceID="AccessDataSourceCountry" TextField="Country" ValueField="Country"
                    EnableSynchronization="False">
                    <ClientSideEvents SelectedIndexChanged="function(s, e) { OnCountryChanged(s); }" />
                </dx:ASPxComboBox>
            </td>
            <td class="LeftPadding">
                <dx:ASPxLabel runat="server" Text="City:" ID="ASPxLabel1" AssociatedControlID="CmbCity" />
            </td>
            <td>
                <dx:ASPxComboBox runat="server" ID="CmbCity" ClientInstanceName="cmbCity" OnCallback="CmbCity_Callback"
                    DropDownStyle="DropDown" DataSourceID="AccessDataSourceCities" TextField="City"
                    ValueField="City" IncrementalFilteringMode="StartsWith" EnableSynchronization="False">
                    <ClientSideEvents EndCallback=" OnEndCallback"/>
                </dx:ASPxComboBox>
            </td>
        </tr>
    </table>
    <asp:AccessDataSource ID="AccessDataSourceCountry" runat="server" DataFile="~/App_Data/WorldCities.mdb"
        SelectCommand="SELECT cr.Country, c.City as Capital FROM [Cities] c, [Countries] cr WHERE c.CityId = cr.CapitalId order by cr.Country">
    </asp:AccessDataSource>
    <asp:AccessDataSource ID="AccessDataSourceCities" runat="server" DataFile="~/App_Data/WorldCities.mdb"
        SelectCommand="SELECT c.City FROM [Cities] c, [Countries] cr WHERE (c.CountryId = cr.CountryId) AND (cr.Country = ?) order by c.City">
        <SelectParameters>
            <asp:Parameter Name="?" />
        </SelectParameters>
    </asp:AccessDataSource>
</asp:Content>
using System;
using System.Data;
using System.Web.UI;
using DevExpress.Web.ASPxClasses;

public partial class ASPxComboBox_ClientAPI : Page {
    protected void Page_Load(object sender, EventArgs e) {
        if (!IsCallback) {
            CmbCountry.Value = "Mexico";
            FillCityCombo("Mexico");
        }
    }
    protected void CmbCity_Callback(object source, CallbackEventArgsBase e) {
        FillCityCombo(e.Parameter);
    }

    protected void FillCityCombo(string country) {
        if (string.IsNullOrEmpty(country)) return;

        AccessDataSourceCities.SelectParameters[0].DefaultValue = country;
        CmbCity.DataBind();

        // Select the current country capital in the CmbCity
        DataTable table = ((DataView)AccessDataSourceCountry.Select(DataSourceSelectArguments.Empty)).Table;
        DataRow[] foundRows = table.Select(string.Format("Country = '{0}'", country)); 
        if(foundRows.Length > 0) 
            CmbCity.Value = (string)foundRows[0]["Capital"]; 
    }
}

 

posted @ 2013-11-19 12:52  KainJC  阅读(608)  评论(0编辑  收藏  举报