Un saludo.
Código:
Imports System.ComponentModel Imports System.Web.UI Imports System.Web.UI.WebControls Public Class controlCPProvincia : Inherits Control : Implements INamingContainer Private _idTxt As String Private _cpInicial As String Public Property IDTxt() As String Get Return _idTxt End Get Set(ByVal Value As String) _idTxt = Value End Set End Property Public Property CPInicial() As String Get Return _cpInicial End Get Set(ByVal Value As String) _cpInicial = Value End Set End Property Protected Overrides Sub CreateChildControls() Dim txt As New TextBox txt.MaxLength = "5" txt.Columns = "1" txt.ID = IDTxt txt.Text = CPInicial Dim lbl As New Label lbl.ID = "lbl" & IDTxt lbl.Text = "Provincia no identificada" 'Script con vectores***** Dim sbScript As New System.Text.StringBuilder sbScript.Append("<script language=javascript>" & vbCrLf) sbScript.Append("var arrProvincias = new Array(" & _ "'Álava','Albacete','Alicante','Almería'," & _ "'Ávila','Badajoz','Baleares','Barcelona'," & _ "'Burgos','Cáceres','Cádiz','Castellón'," & _ "'Ciudad Real','Córdoba','Coruña','Cuenca'," & _ "'Gerona','Granada','Guadalajara','Guipúzcoa'," & _ "'Huelva','Huesca','Jaén','León'," & _ "'Lérida','La Rioja','Lugo','Madrid'," & _ "'Málaga','Murcia','Navarra','Orense'," & _ "'Asturias','Palencia','Las Palmas','Pontevedra'," & _ "'Salamanca','Santa Cruz de Tenerife','Cantabria','Segovia'," & _ "'Sevilla','Soria','Tarragona','Teruel'," & _ "'Toledo','Valencia','Valladolid','Vizcaya'," & _ "'Zamora','Zaragoza','Ceuta','Melilla');" & vbCrLf) sbScript.Append("var arrCodigos = new Array('01','02','03','04','05','06','07','08','09','10'," & _ "'11','12','13','14','15','16','17','18','19','20'," & _ "'21','22','23','24','25','26','27','28','29','30'," & _ "'31','32','33','34','35','36','37','38','39','40'," & _ "'41','42','43','44','45','46','47','48','49','50'," & _ "'51','52');" & vbCrLf) sbScript.Append("function provinciaCP(txt,lbl) {" & vbCrLf) sbScript.Append("var cp = String(txt).substring(0,2);" & vbCrLf) sbScript.Append("var ind, pos;" & vbCrLf) sbScript.Append("for(ind=0; ind<arrCodigos.length; ind++)" & vbCrLf) sbScript.Append("{" & vbCrLf) sbScript.Append("if (arrCodigos[ind] == cp)" & vbCrLf) sbScript.Append("break;" & vbCrLf) sbScript.Append("}" & vbCrLf) sbScript.Append("pos = (ind < arrCodigos.length)? ind : -1;" & vbCrLf) sbScript.Append("if(arrProvincias[pos] != undefined)" & vbCrLf) sbScript.Append("{document.getElementById(lbl).innerHTML = arrProvincias[pos];}" & vbCrLf) sbScript.Append("else {document.getElementById(lbl).innerHTML = 'Provincia no identificada';}" & vbCrLf) sbScript.Append("}" & vbCrLf) sbScript.Append("</script>" & vbCrLf) Me.Controls.Add(New LiteralControl(sbScript.ToString)) '************************ Me.Controls.Add(txt) Me.Controls.Add(New LiteralControl(" ")) Me.Controls.Add(lbl) txt.Attributes.Add("onChange", "provinciaCP(this.value,'" & lbl.ClientID & "');") Me.Controls.Add(New LiteralControl("<script>provinciaCP('" & txt.Text & "','" & lbl.ClientID & "');</script>")) End Sub End Class