控件页面代码
<div id="appear" runat="server"> <asp:DropDownList ID="ddlFather" runat="server" οnchange="go(this)" ></asp:DropDownList> </div> <input id="Ids" type="hidden" runat="server"/> <input id="tName" type="hidden" runat ="server" /> <script type="text/javascript"> var xmlHttpRequest = null; function go(obj) { var dropdown=document.getElementById("<%=appear.ClientID %>"); var delid=0; while(document.getElementById("<%=this.ClientID %>"+delid)!=null){ dropdown.removeChild(document.getElementById("<%=this.ClientID %>"+delid)); delid++; } var value=obj.value; var tName=document.getElementById("<%=tName.ClientID %>").value; var url="/comm/setclass/getsons.ashx?tname="+tName+"&id="+value; if (window.ActiveXObject) { xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } else { xmlHttpRequest = new XMLHttpRequest(); } xmlHttpRequest.onreadystatechange = function(){readyDo(0);}; xmlHttpRequest.open("GET", url, true); xmlHttpRequest.send(); } function readyDo(idCount) { if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) { var vSons = xmlHttpRequest.responseText; var JsonSons=eval(vSons); disPlay(JsonSons,idCount); //ShowDrop(JsonSons,0); } } function disPlay(sons,idCount){ var dropdown=document.getElementById("<%=appear.ClientID %>"); var select=document.createElement("select"); select.id="<%=this.ClientID %>"+idCount++; select.οnchange=function(){ //delete var delid=idCount; while(document.getElementById("<%=this.ClientID %>"+delid)!=null){ dropdown.removeChild(document.getElementById("<%=this.ClientID %>"+delid)); delid++; } var value=select.value; var tName=document.getElementById("<%=tName.ClientID %>").value; var url="/comm/setclass/getsons.ashx?tname="+tName+"&id="+value; if (window.ActiveXObject) { xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } else { xmlHttpRequest = new XMLHttpRequest(); } xmlHttpRequest.onreadystatechange = function(){readyDo(idCount);}; xmlHttpRequest.open("GET", url, true); xmlHttpRequest.send(); } //new for(var i=0;i<sons.length;i++){ select.options[i]=new Option(sons[i].name,sons[i].id); } dropdown.appendChild(select); } </script>
ashx.cs代码
public class getsons : System.Web.UI.Page { public void Page_Load(object sender, EventArgs e) { Response.ContentType = "text/plain"; Response.Write(getSonById()); } public string getSonById() { string strId=Request["id"]; string strTableName=Request["tName"]; ClsTbProc clsSrc = new ClsTbProc(GlbDef.GetDbConnStr(), strTableName); DataTable dt = clsSrc.GetRec("fId,fName", "where fParentId='" + strId + "'"); if (dt == null || dt.Rows.Count == 0) { return null; } string strJson = "[{\"name\": \"----\", \"id\": \"----\"},"; foreach (DataRow dr in dt.Rows) { string strNameSon=dr["fName"].ToString(); string strIdSon=dr["fId"].ToString(); strJson += "{\"name\": \"" + strNameSon + "\", \"id\": \"" + strIdSon + "\"},"; } strJson = strJson.Trim(',') + "]"; return strJson; } }