DX:ASPxGridView中EditForm模板中实现省、市、区三级联动的方法

一、首先在前端创建ASPxGridView列:
<主要代码如下>

            <dx:GridViewDataComboBoxColumn FieldName="省" VisibleIndex="5">
            <PropertiesComboBox TextField="ProvinceName" ValueField="ProvinceName" EnableSynchronization="false" DataSecurityMode="Strict"  IncrementalFilteringMode="StartsWith" DataSourceID="SqlDataSource1">
                <ClientSideEvents SelectedIndexChanged="function(s, e) { OnShengChanged(s); }" />
            </PropertiesComboBox>
            </dx:GridViewDataComboBoxColumn>
            <dx:GridViewDataComboBoxColumn FieldName="市" VisibleIndex="6">
            <PropertiesComboBox TextField="CityName" ValueField="CityName" EnableSynchronization="false" DataSecurityMode="Strict" IncrementalFilteringMode="StartsWith" DataSourceID="SqlDataSource2">
                <ClientSideEvents SelectedIndexChanged="function(s, e) { OnShiChanged(s); }" />
            </PropertiesComboBox>
            </dx:GridViewDataComboBoxColumn>
            <dx:GridViewDataComboBoxColumn FieldName="区" VisibleIndex="7">
            <PropertiesComboBox TextField="DistrictName" ValueField="DistrictName" EnableSynchronization="false" DataSecurityMode="Strict" IncrementalFilteringMode="StartsWith" DataSourceID="SqlDataSource3" >
                <ClientSideEvents EndCallback="OnEndCallback" />
            </PropertiesComboBox>
            </dx:GridViewDataComboBoxColumn>

这里的省市区三列均为comboBox控件,可以在设计器中实现,也可前端代码实现。
二、为各控件设置数据源:
创建三个sqlDataSource数据源控件,分别为:sqlDataSource1、sqlDataSource2、sqlDataSource3
三、为各控件设置客户端事件:
省:SelectedIndexChanged事件
市:SelectedIndexChanged事件
区:EndCallback事件
四、前端各事件代码:

        var lastsheng = null;
        function OnShengChanged(cmbSheng) {
            if (ASPxGridView1.GetEditor("市").InCallback())
                lastSheng = cmbSheng.GetValue().toString();
            else
                ASPxGridView1.GetEditor("市").PerformCallback(cmbSheng.GetValue().toString());
                ASPxGridView1.GetEditor("区").PerformCallback("");//此处为省改变时,要清空市、区的值
        }

        var lastshi = null;
        function OnShiChanged(cmbShi) {
            
            if (ASPxGridView1.GetEditor("区").InCallback())
                lastShi = cmbShi.GetValue().toString();
            else
                ASPxGridView1.GetEditor("区").PerformCallback(cmbShi.GetValue().toString());
        }

       
        function OnEndCallback(s, e) {
            if (lastshi) {
                ASPxGridView1.GetEditor("区").PerformCallback(lastshi);
                lastshi = null;
            }
        }

五、后台事件及方法:
注意:string sheng="";
string shi="";
写在方法外面

    /// <summary>
    /// EditForm的单元格编辑初始化事件
    /// </summary>
    /// <param name="sender">EditForm</param>
    /// <param name="e">是指EditForm中的控件</param>
    protected void ASPxGridView1_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
    {
         //当前新增时
        if (ASPxGridView1.IsNewRowEditing)
        {
            if (!ASPxGridView1.IsEditing || (e.Column.FieldName != "市" && e.Column.FieldName != "区"))
            {
                return;
            }
            if (e.Column.FieldName == "省")
            {
               //当为“省”这一列时,取出省的值,存在sheng变量中备用
                ASPxComboBox sheng_combo = e.Editor as ASPxComboBox;
                sheng = (string)sheng_combo.Value;
            }
            if (e.Column.FieldName == "市")
            {
                //当为“市”这一列时,取出市的值,存在shi变量中备用
                ASPxComboBox shi_combo = e.Editor as ASPxComboBox;//市combobox下拉框对象
                shi = (string)shi_combo.Value;
                FillCityCombo(shi_combo, sheng);//填充市下拉框对象
                shi_combo.Callback += new CallbackEventHandlerBase(cmbCity_OnCallback);
            }
            if (e.Column.FieldName == "区")
            {
                ASPxComboBox combo2 = e.Editor as ASPxComboBox;//市combobox下拉框对象
                FillDistrictCombo(combo2, shi);//填充市下拉框对象
                combo2.Callback += new CallbackEventHandlerBase(cmbDistrict_OnCallback);
            }
        }
        else//当为修改时
        {
            if (!ASPxGridView1.IsEditing || (e.Column.FieldName != "市" && e.Column.FieldName != "区"))
            {
                return;
            }
            if (e.KeyValue == DBNull.Value || e.KeyValue == null)
            {
                return;
            }
            if (e.Column.FieldName == "市")
            {
                object val = ASPxGridView1.GetRowValuesByKeyValue(e.KeyValue, "省");//根据本行主键值获取本行中的省列位值
                if (val == DBNull.Value) return;
                sheng = (string)val;//省名
                ASPxComboBox combo = e.Editor as ASPxComboBox;//市combobox下拉框对象
                FillCityCombo(combo, sheng);//填充市下拉框对象
                combo.Callback += new CallbackEventHandlerBase(cmbCity_OnCallback);
            }
            if (e.Column.FieldName == "区")
            {
                object val = ASPxGridView1.GetRowValuesByKeyValue(e.KeyValue, "市");//根据本行主键值获取本行中的市列位值
                if (val == DBNull.Value) return;
                shi = (string)val;//市名
                ASPxComboBox combo2 = e.Editor as ASPxComboBox;//市combobox下拉框对象
                FillDistrictCombo(combo2, shi);//填充市下拉框对象
                combo2.Callback += new CallbackEventHandlerBase(cmbDistrict_OnCallback);
            }
        }
    }


    //市绑定下拉项
    /// <summary>
    /// 根据省名获取市以填充市下拉框
    /// </summary>
    /// <param name="cmb"></param>
    /// <param name="sheng"></param>
    protected void FillCityCombo(ASPxComboBox cmb, string sheng)
    {
        if (string.IsNullOrEmpty(sheng)) return;

        List<string> cities = GetCities(sheng);
        cmb.Items.Clear();
        foreach (string city in cities)
            cmb.Items.Add(city);
    }

    /// <summary>
    /// 获取省下面的市
    /// </summary>
    /// <param name="sheng"></param>
    /// <returns></returns>
    List<string> GetCities(string sheng)
    {
        List<string> ls = new List<string> { };
        object osheng_bh=SqlHelper.ExecuteScalar("select ProvinceID from S_Province where ProvinceName='" + sheng + "'", new SqlParameter[] { });
        if (osheng_bh != null)
        {
            string sheng_bh = Convert.ToString(osheng_bh);
            DataTable shi_dt = SqlHelper.ExecuteDataTable("select CityName from S_City where ProvinceID=" + sheng_bh, new SqlParameter[] { });
            
            foreach (DataRow dr in shi_dt.Rows)
            {
                ls.Add(dr["CityName"].ToString());
            }
        }
        
        return ls;
    }

    void cmbCity_OnCallback(object source, CallbackEventArgsBase e)
    {
        FillCityCombo(source as ASPxComboBox, e.Parameter);
    }




    //区绑定下拉项
    /// <summary>
    /// 根据市名称获取市下面的区以填充区下拉框
    /// </summary>
    /// <param name="cmb"></param>
    /// <param name="shi"></param>
    protected void FillDistrictCombo(ASPxComboBox cmb, string shi)
    {
        if (string.IsNullOrEmpty(shi))
        {
            cmb.Items.Clear();
            return;
        }
        List<string> Districts = GetDistricts(shi);
        cmb.Items.Clear();
        if (Districts != null)
        {
            foreach (string District in Districts)
                cmb.Items.Add(District);
        }
    }
    /// <summary>
    /// 获取市下面的区
    /// </summary>
    /// <param name="shi"></param>
    /// <returns></returns>
    List<string> GetDistricts(string shi)
    {
        List<string> ls = new List<string> { };
        object oshi_bh = SqlHelper.ExecuteScalar("select CityID from S_City where CityName='" + shi + "'", new SqlParameter[] { });
        if (oshi_bh != null)
        {
            string shi_bh = Convert.ToString(oshi_bh);
            DataTable District_dt = SqlHelper.ExecuteDataTable("select DistrictName from S_District where CityID=" + shi_bh, new SqlParameter[] { });
            
            foreach (DataRow dr in District_dt.Rows)
            {
                ls.Add(dr["DistrictName"].ToString());
            }
        }
        return ls;
    }
    /// <summary>
    /// 区回调函数
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    void cmbDistrict_OnCallback(object source, CallbackEventArgsBase e)
    {
        FillDistrictCombo(source as ASPxComboBox, e.Parameter);
    }

至此省、市、区三级联动实现。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值