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

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

https://blog.csdn.net/hmwz0001/article/details/102919709/

一、首先在前端创建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数据源控件,分别为:sqlDataSource1sqlDataSource2sqlDataSource3

三、为各控件设置客户端事件:

省: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);

    }

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

————————————————

版权声明:本文为CSDN博主「864记忆」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/hmwz0001/article/details/102919709/

渝公网安备 50011902000225号

渝ICP备18008233号-1