自定义控件获取邮政编码(包含国家、省市联动)
说明:由于项目需要,制作一个复杂的 TextBox 自定义控件,功能是控件内部可以通过国家、省市联动来获取邮政编码,显示在 Text 中,该控件的对外接口就是 Text 属性,其他程序可以通过 Text 属性来 设置或获取邮政编码。
源码下载:自定义控件获取邮政编码(包含国家、省市联动)
其中有一些问题,希望能和大家一起探讨。
该控件分三大块:控件逻辑代码(GetZip.cs)、js 代码(ajaxData.js)和 异步数据获取代码(ZipCode.ashx),
详细如下:
1、GetZip.cs:
Code
2、ajaxData.js
var xmlDom;
function createXMLHTTP()
{
if(window.XMLHttpRequest)
{
xmlDom=new XMLHttpRequest();//mozilla浏览器
}
else if(window.ActiveXObject)
{
try
{
xmlDom=new ActiveXObject("Msxml2.XMLHTTP");//IE老版本
}
catch(e)
{}
try
{
xmlDom=new ActiveXObject("Microsoft.XMLHTTP");//IE新版本
}
catch(e)
{}
if(!xmlDom)
{
window.alert("不能创建XMLHttpRequest对象实例!");
return false;
}
}
}
function GetValues(obj,targetObj,type)
{
if(obj.value!="")
{
createXMLHTTP();
var strURL="UserFunction/HTTPProcess/ZipCode.ashx?id="+obj.value+"&type="+type;
xmlDom.open("GET",strURL,true);
xmlDom.onreadystatechange = function(){ShowValues(targetObj,type);};
xmlDom.send(null);
}
}
function ShowValues(targetObj,type)
{
if (xmlDom.readyState == 4 && xmlDom.status == 200)
{
var arr = xmlDom.responseText;
if(arr != ""|| arr!=null)
{

// if(arr.indexOf("=")>0)
// {
// arr = arr.substr(arr.indexOf("=")+1);
// }
var array = arr.split(",");
var result = array;
// var targetObj = document.getElementById(array[array.length-2]);
// var type = array[array.length-1];
if(targetObj!=null)
{
if(type=="3")
{
targetObj.value = result[0];
}
else
{
delValues(targetObj);//delete
// var piArray = result.split(",");
if(type=="1")
{
targetObj.options.add(new Option("Select a State/Province","0"));
}
else if(type=="2")
{
targetObj.options.add(new Option("Select a Metro or City","0"));
}
for(var i=0;i<result.length;i++)
{
var ary1 = result[i].toString().split("|");
targetObj.options.add(new Option(ary1[1].toString(),ary1[0].toString()));
}
}
}
}
else
{
alert("sorry It has no data in database");
}
}
}
//delete values
function delValues(targetObject)
{
targetObject.innerHTML="";
}
function getZipCode(targerControl,originControl)
{
targerControl.value = originControl.value;
Panle_Hide(targerControl.Container.FloatContainer);
}

function selectZip()
{
// if(document.all["details"].style.visibility=="hidden")
// {
// document.all["details"].style.visibility="visible";
// }
// else
// {
// document.all["details"].style.visibility=="hidden";
// }
hiddenPanel();
}
function hiddenPanel()
{
document.getElementById("showPanel").style.display = "none";
}
function Zip_FloatPanel_Init()
{
for( var i = 0; i < Zip_Controls.length; i++ ) {
var info = Zip_Controls[ i ];
Zip_FloatPanel_Load( info );
}


}
function Zip_FloatPanel_Load( info )
{
var ZipCode = document.getElementById( info.ID );
var Container = document.getElementById( info.ContainerID );
var FloatContainer = document.getElementById( info.FloatContainerID );
var Text = document.getElementById( info.TextID );
var CodeText = document.getElementById( info.CodeTextID );
var Button = document.getElementById( info.ButtonID );
var GetButton = document.getElementById( info.GetButtonID );
var Country = document.getElementById( info.CountryID );
var State = document.getElementById( info.StateID );
var City = document.getElementById( info.CityID );
ZipCode.style.display = "";
Container.FloatContainer = FloatContainer;
Container.Text = Text;
Container.Button = Button;
FloatContainer.Container = Container;
Text.Container = Container;
Button.Container = Container;
FloatContainer.CodeText = CodeText;
FloatContainer.GetButton = GetButton;
FloatContainer.Country = Country;
FloatContainer.State = State;
FloatContainer.City = City;
CodeText.FloatContainer = FloatContainer;
GetButton.FloatContainer = FloatContainer;
Country.FloatContainer = FloatContainer;
State.FloatContainer = FloatContainer;
City.FloatContainer = FloatContainer;
FloatContainer.style.display = "none";
FloatContainer.style.position = "absolute";
FloatContainer.style.left = "0px";
FloatContainer.style.zIndex = 10000;
// showPanel.size = ( info.ListSize > List.options.length ) ? List.options.length : info.ListSize;
FloatContainer.multiple = false;
FloatContainer.IsShowing = false;
// FloatContainer.Show = Panle_Show;
// FloatContainer.Hide = Panle_Hide;
}
function Zip_Button_Toggle(button) {
if ( button.Container.FloatContainer.IsShowing == true ) {
// this.FloatContainer.Hide();
Panle_Hide(button.Container.FloatContainer)
} else {
Panle_Show(button.Container.FloatContainer);
}
}
function Panle_Show(floatContainer) {
if ( !floatContainer.IsShowing && !floatContainer.disabled ) {
// if ( typeof ( this.Container.Text.dataSrc ) != "undefined" ) { // to overcome misaligniment in IE
// this.style.top = ( this.Container.offsetHeight + 10 ) + "px";
// } else {
floatContainer.style.top = ( floatContainer.Container.offsetHeight ) + "px";
// }
// this.style.width = this.Container.offsetWidth + "px";
// this.style.width = "100px";
floatContainer.style.display = "";
floatContainer.focus();
floatContainer.IsShowing = true;
}
}
function Panle_Hide(floatContainer) {
if ( floatContainer.IsShowing ) {
floatContainer.style.display = "none";
floatContainer.IsShowing = false;
}
}
3、ZipCode.ashx(这里的逻辑可以根据自己的数据库结构来定),该页面是独立出来的,放在被引用的项目中,没有和自定义控件一起编译进去,这里是一个问题,有没有朋友解决一下把这个文件一起编译到 zip 的自定义控件中,这样一来就更方便了。
Code
有一个问题需要大家帮忙,就是在国家、省市联动那一块,我用的是 ajax 来实现的,而没有用微软自带的 ICallbackEventHandler 功能来实现,原因是用 ICallbackEventHandler 来实现出现一个问题。就是在引用的页面代码里加 Response.Write() 功能时,该控件的联动不可用,代码就不贴出来了,以下有源码下载,请朋友帮忙.
源码:自定义控件(问题)
源码下载:自定义控件获取邮政编码(包含国家、省市联动)
其中有一些问题,希望能和大家一起探讨。
该控件分三大块:控件逻辑代码(GetZip.cs)、js 代码(ajaxData.js)和 异步数据获取代码(ZipCode.ashx),
详细如下:
1、GetZip.cs:
var xmlDom;
function createXMLHTTP()
{
if(window.XMLHttpRequest)
{
xmlDom=new XMLHttpRequest();//mozilla浏览器
}
else if(window.ActiveXObject)
{
try
{
xmlDom=new ActiveXObject("Msxml2.XMLHTTP");//IE老版本
}
catch(e)
{}
try
{
xmlDom=new ActiveXObject("Microsoft.XMLHTTP");//IE新版本
}
catch(e)
{}
if(!xmlDom)
{
window.alert("不能创建XMLHttpRequest对象实例!");
return false;
}
}
}
function GetValues(obj,targetObj,type)
{
if(obj.value!="")
{
createXMLHTTP();
var strURL="UserFunction/HTTPProcess/ZipCode.ashx?id="+obj.value+"&type="+type;
xmlDom.open("GET",strURL,true);
xmlDom.onreadystatechange = function(){ShowValues(targetObj,type);};
xmlDom.send(null);
}
}
function ShowValues(targetObj,type)
{
if (xmlDom.readyState == 4 && xmlDom.status == 200)
{
var arr = xmlDom.responseText;
if(arr != ""|| arr!=null)
{
// if(arr.indexOf("=")>0)
// {
// arr = arr.substr(arr.indexOf("=")+1);
// }
var array = arr.split(",");
var result = array;
// var targetObj = document.getElementById(array[array.length-2]);
// var type = array[array.length-1];
if(targetObj!=null)
{
if(type=="3")
{
targetObj.value = result[0];
}
else
{
delValues(targetObj);//delete
// var piArray = result.split(",");
if(type=="1")
{
targetObj.options.add(new Option("Select a State/Province","0"));
}
else if(type=="2")
{
targetObj.options.add(new Option("Select a Metro or City","0"));
}
for(var i=0;i<result.length;i++)
{
var ary1 = result[i].toString().split("|");
targetObj.options.add(new Option(ary1[1].toString(),ary1[0].toString()));
}
}
}
}
else
{
alert("sorry It has no data in database");
}
}
}
//delete values
function delValues(targetObject)
{
targetObject.innerHTML="";
}
function getZipCode(targerControl,originControl)
{
targerControl.value = originControl.value;
Panle_Hide(targerControl.Container.FloatContainer);
}
function selectZip()
{
// if(document.all["details"].style.visibility=="hidden")
// {
// document.all["details"].style.visibility="visible";
// }
// else
// {
// document.all["details"].style.visibility=="hidden";
// }
hiddenPanel();
}
function hiddenPanel()
{
document.getElementById("showPanel").style.display = "none";
}
function Zip_FloatPanel_Init()
{
for( var i = 0; i < Zip_Controls.length; i++ ) {
var info = Zip_Controls[ i ];
Zip_FloatPanel_Load( info );
}

}
function Zip_FloatPanel_Load( info )
{
var ZipCode = document.getElementById( info.ID );
var Container = document.getElementById( info.ContainerID );
var FloatContainer = document.getElementById( info.FloatContainerID );
var Text = document.getElementById( info.TextID );
var CodeText = document.getElementById( info.CodeTextID );
var Button = document.getElementById( info.ButtonID );
var GetButton = document.getElementById( info.GetButtonID );
var Country = document.getElementById( info.CountryID );
var State = document.getElementById( info.StateID );
var City = document.getElementById( info.CityID );
ZipCode.style.display = "";
Container.FloatContainer = FloatContainer;
Container.Text = Text;
Container.Button = Button;
FloatContainer.Container = Container;
Text.Container = Container;
Button.Container = Container;
FloatContainer.CodeText = CodeText;
FloatContainer.GetButton = GetButton;
FloatContainer.Country = Country;
FloatContainer.State = State;
FloatContainer.City = City;
CodeText.FloatContainer = FloatContainer;
GetButton.FloatContainer = FloatContainer;
Country.FloatContainer = FloatContainer;
State.FloatContainer = FloatContainer;
City.FloatContainer = FloatContainer;
FloatContainer.style.display = "none";
FloatContainer.style.position = "absolute";
FloatContainer.style.left = "0px";
FloatContainer.style.zIndex = 10000;
// showPanel.size = ( info.ListSize > List.options.length ) ? List.options.length : info.ListSize;
FloatContainer.multiple = false;
FloatContainer.IsShowing = false;
// FloatContainer.Show = Panle_Show;
// FloatContainer.Hide = Panle_Hide;
}
function Zip_Button_Toggle(button) {
if ( button.Container.FloatContainer.IsShowing == true ) {
// this.FloatContainer.Hide();
Panle_Hide(button.Container.FloatContainer)
} else {
Panle_Show(button.Container.FloatContainer);
}
}
function Panle_Show(floatContainer) {
if ( !floatContainer.IsShowing && !floatContainer.disabled ) {
// if ( typeof ( this.Container.Text.dataSrc ) != "undefined" ) { // to overcome misaligniment in IE
// this.style.top = ( this.Container.offsetHeight + 10 ) + "px";
// } else {
floatContainer.style.top = ( floatContainer.Container.offsetHeight ) + "px";
// }
// this.style.width = this.Container.offsetWidth + "px";
// this.style.width = "100px";
floatContainer.style.display = "";
floatContainer.focus();
floatContainer.IsShowing = true;
}
}
function Panle_Hide(floatContainer) {
if ( floatContainer.IsShowing ) {
floatContainer.style.display = "none";
floatContainer.IsShowing = false;
}
}有一个问题需要大家帮忙,就是在国家、省市联动那一块,我用的是 ajax 来实现的,而没有用微软自带的 ICallbackEventHandler 功能来实现,原因是用 ICallbackEventHandler 来实现出现一个问题。就是在引用的页面代码里加 Response.Write() 功能时,该控件的联动不可用,代码就不贴出来了,以下有源码下载,请朋友帮忙.
源码:自定义控件(问题)

浙公网安备 33010602011771号