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


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

    2019-11-05 17:08:07 864记忆 阅读数 4更多

    一、首先在前端创建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;
                }
            }
    

    五、后台事件及方法:

        /// <summary>
        /// EditForm的单元格编辑初始化事件
        /// </summary>
        /// <param name="sender">EditForm</param>
        /// <param name="e">是指EditForm中的控件</param>
        protected void ASPxGridView1_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
        {
            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;
                string 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;
                string 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);
        }
    

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

  • 相关阅读:
    CompositeConfiguration的用法
    Intellij IDEA连接Git@OSC
    Spring4.1.6 标签注解大全以及解释
    React制作吸顶功能总结
    总结常见的ES6新语法特性。
    12个常规前端面试题及小结
    JavaScript 扩展运算符
    Promise,Generator(生成器),async(异步)函数
    Vue中过度动画效果应用
    vue的双向绑定原理及实现
  • 原文地址:https://www.cnblogs.com/grj001/p/12223456.html
Copyright © 2020-2023  润新知