一个城市自定义控件

抽点时间写了一个自定义空间,功能是这样的,实现省份,城市的级联更新.数据源是数据库,设计思路是这样的,利用prototype1.5 提供的AJAX功能实现控件级联无刷新功能,请求一个Httphandler,读取数据.

控件呈现:

 

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Text;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  Ucar.Common;
using  System.Data;
using  Ucar.Components.Caching;

namespace  BitAuto.Ucar.CarSelectSortData
{
    [DefaultProperty(
"Text")]
    [ToolboxData(
"<{0}:CitySelect runat=server></{0}:CitySelect>")]
    
public class CitySelect : WebControl, INamingContainer, IPostBackDataHandler
    
{
        
private string _validationExpression = @"^[1-9]d*$";

        
/// <summary>
        
/// 控件集合
        
/// </summary>

        private Label lblProvince = new Label();
        
private DropDownList dropProvince = new DropDownList();
        
private Label lblCity = new Label();
        
private DropDownList dropCity = new DropDownList();

        
private RegularExpressionValidator valeCityValue = new RegularExpressionValidator();

        
//控件属性

        
protected override void RenderContents(HtmlTextWriter output)
        
{
            
if (this.Page != null)
            
{
                
this.Page.VerifyRenderingInServerForm(this);
            }

            output.AddAttribute(HtmlTextWriterAttribute.Type, 
"hidden");
            output.AddAttribute(HtmlTextWriterAttribute.Name, 
this.UniqueID + "_hdProvinceId");
            output.AddAttribute(HtmlTextWriterAttribute.Id, 
this.UniqueID + "_hdProvinceId");
            output.AddAttribute(HtmlTextWriterAttribute.Value, 
this.ProvinceId.ToString());
            output.RenderBeginTag(HtmlTextWriterTag.Input);
            output.RenderEndTag();

            output.AddAttribute(HtmlTextWriterAttribute.Type, 
"hidden");
            output.AddAttribute(HtmlTextWriterAttribute.Name, 
this.UniqueID);
            output.AddAttribute(HtmlTextWriterAttribute.Id, 
this.UniqueID + "_hdCityId");
            output.AddAttribute(HtmlTextWriterAttribute.Value, 
this.CityId.ToString());
            output.RenderBeginTag(HtmlTextWriterTag.Input);
            output.RenderEndTag();
            
if (RepeatDriection == RepeatDriections.Vertical)
            
{
                output.RenderBeginTag(HtmlTextWriterTag.Div);
                output.Write(
"<DIV class='" + this.ProvinceCssClass + "' >");
                lblProvince.RenderControl(output);
                output.Write(
"&nbsp;&nbsp;");
                dropProvince.RenderControl(output);
                output.Write(
"</DIV>");
                output.Write(
"<DIV class='" + this.CityCssClass + "' >");
                lblCity.RenderControl(output);
                output.Write(
"&nbsp;&nbsp;");
                dropCity.RenderControl(output);
                output.Write(
"</DIV>");
            }

            
else
            
{
                output.RenderBeginTag(HtmlTextWriterTag.Div);
                lblProvince.RenderControl(output);
                output.Write(
"&nbsp;&nbsp;");
                dropProvince.RenderControl(output);
                output.Write(
"&nbsp;&nbsp;");
                lblCity.RenderControl(output);
                output.Write(
"&nbsp;&nbsp;");
                dropCity.RenderControl(output);
            }

            valeCityValue.RenderControl(output);
        }


        
protected override void CreateChildControls()
        
{
            
//省份标签
            lblProvince.Text = "省份";
            lblProvince.ID 
= "lblCarProducer";
            lblProvince.Visible 
= LableVisible;
            
this.Controls.Add(lblProvince);

            dropProvince.ID 
= "dropProvince";
            
this.Controls.Add(dropProvince);

            
//城市
            lblCity.Text = "城市";
            lblCity.ID 
= "lblCarBrand";
            lblCity.Visible 
= LableVisible;
            
this.Controls.Add(lblCity);

            dropCity.ID 
= "dropCity";
            
this.Controls.Add(dropCity);

            valeCityValue.ID 
= "_valeCityValue";

            valeCityValue.ControlToValidate 
= this.dropCity.ID;
            valeCityValue.Display 
= ValidatorDisplay.None;
            valeCityValue.ErrorMessage 
= this.ErrorMessage;
            valeCityValue.ValidationExpression 
= _validationExpression;
            valeCityValue.ValidationGroup 
= this.ValidationGroup;
            valeCityValue.SetFocusOnError 
= true;
            valeCityValue.Enabled 
= this.EnabledRequiredFieldValidator;
            
this.Controls.Add(valeCityValue);

            dropProvince.Attributes.Add(
"onchange""cityLoad(" + this.dropProvince.ClientID + "," + this.dropCity.ClientID + "," + this.UniqueID + "_hdProvinceId" + "," + this.UniqueID + "_hdCityId" + ");");
            dropCity.Attributes.Add(
"onchange""SetCityID(" + this.UniqueID + "_hdCityId" + "," + this.dropCity.ClientID + ");");
        }


        
protected override void OnInit(EventArgs e)
        
{
            Page.RegisterRequiresControlState(
this);
            Page.ClientScript.RegisterClientScriptResource(
typeof(CitySelect), "BitAuto.Ucar.CarSelectSortData.prototype1.5.1.js");
            Page.ClientScript.RegisterClientScriptResource(
typeof(CitySelect), "BitAuto.Ucar.CarSelectSortData.CityChange.js");
            
base.OnInit(e);
        }


        
protected override void OnPreRender(EventArgs e)
        
{
            InitProvince();
            
if (this.CityId != 0)
            
{
                
this.ProvinceId = City.GetProvinceIdByCityId(this.CityId);
            }

            
if (this.ProvinceId != 0)
            
{
                dropProvince.SelectedValue 
= ProvinceId.ToString();
            }

            InitCity();
            
if (CityId != 0)
            
{
                ControlHelper.InitDropDownListByValue(dropCity, CityId.ToString());
                
//dropCity.SelectedValue = CityId.ToString();
                
//dropCity.SelectedIndex = dropCity.Items.IndexOf(dropCity.Items.FindByValue(CityId.ToString()));
            }

            
base.OnPreRender(e);
        }


        
IPostBackDataHandler 成员

        
//绑定方法

        
/// <summary>
        
/// 布局方式
        
/// </summary>

        public enum RepeatDriections
        
{
            Vertical,
//垂直
            Horizontal//水平
        }

    }

}

 

Handler实现:

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Data;
using  System.Web;
using  Ucar.Common;
using  Ucar.Components.Caching;

namespace  BitAuto.Ucar.CarSelectSortData
{
    
public class CityHandler : IHttpHandler
    
{
        
IHttpHandler 成员
        
/// <summary>
        
/// 输出
        
/// </summary>

        protected void OutputCity(HttpContext context)
        
{
            
string ProvinceIdStr =  context.Request["ProvinceId"];
            
if (!string.IsNullOrEmpty(ProvinceIdStr))
            
{
                DataView dvCity 
= City.GetCityCacheByProvinceId(ProvinceIdStr);
                StringBuilder sb 
= new StringBuilder();
                
int i = 0;
                
for (i = 0; i < dvCity.Table.Rows.Count; i++)
                
{
                    sb.Append(dvCity.Table.Rows[i][
"city_Name"+ "," + dvCity.Table.Rows[i]["city_Id"+ ",");
                }

                 context.Response.Write(sb.ToString().TrimEnd(
','));
            }

        }


    }


}

JS:

 

  function  cityLoad(dropProvinceid,dropCityid,hdProvinceId,hdCityId)
    
{
        
var father= $F(dropProvinceid);
        $(hdProvinceId).value
=father;
        $(hdCityId).value 
= '';
        
if(father!=0)
        
{
            
var url = "~/AjaxCity.aspx";
            
var pars = "provinceID=" + father;
            
var myAjax = new Ajax.Request(url,{method: 'get', parameters: pars, onComplete:function(originalRequest){showResponse(originalRequest,dropCityid)}});
         }

         
else
         
{
            
var sel = $(dropCityid);
            sel.update(
"");
            CreatOptions(sel,
"请选择城市","");
         }

    }

    
function  SetCityID(hdCityId,dropCity)
    
{
        $(hdCityId).value
=$F(dropCity);
    }

    
function  showResponse(result,dropCityid)
    
{
        
var sel = $(dropCityid);
        
var val = result.responseText.split(',');
        sel.update(
"");
        CreatOptions(sel,
"请选择城市","");
        
for(i=0;i<val.length;i++)
        
{
            text 
= val[i];
            value 
= val[++i];
            CreatOptions(sel,text,value);
        }

    }

    
function  CreatOptions(colls,text,value)
    
{
        colls.options.add(
new Option(text,value));
    }

在使用的时候配下hander和连接字符串.

 

< httpHandlers >
            
< remove  verb ="*"  path ="*.asmx" />
           
  < add  verb ="*"  path ="AjaxCity.aspx"  validate ="false"  type ="BitAuto.Ucar.CarSelectSortData.CityHandler,BitAuto.Ucar.CarSelectSortData" />
        
</ httpHandlers >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值