調用頁面通過事件觸發
<asp:dropdownlist id="ddlAgentCity" runat="server" onchange="OnAgentCityChange();"></asp:dropdownlist>
對應函數,使用ajax的方式調用上面頁面得到資料集,並差找出對應記錄
- function OnAgentCityChange()
- {
- var city = $("#<%=ddlAgentCity.ClientID%>").val();
- $.ajax(
- {
- url: '<%=PageUtility.GetRootURL()%>/Order/Report/XML/GetCompanyByCity.aspx?City=' + city ,
- type: 'GET',
- dataType: 'xml',
- timeout: 10000,
- error: function()
- {
- alert('Error loading XML document');
- },
- success: function(xml)
- {
- var str = "";
- $(xml).find("root").find("item").each(function()
- {
- str += "<option value='" + $(this).attr("id") + "'>" + $(this).attr("name") + "</option>"
- });
- $("#<%=ddlAgentID.ClientID%>").html(str);
- }
- });
- }
最後一句是直接賦值
下面是提供查詢資料庫的頁面,會返回xml格式的集合
public class GetCityByAreaRegion : System.Web.UI.Page
private void Page_Load(object sender, System.EventArgs e)
{
string strArea = Request.QueryString["Area"];
if(strArea == null)
strArea = "";
XmlDocument doc = new XmlDocument();
doc.LoadXml("<?xml version=/"1.0/" encoding=/"GB2312/" ?>/n<root/>");
IList lst;
if(strArea.Trim() == "")
{
lst = City.GetAllCitys();
}
else
{
lst = City.GetByAreaRegion(strArea);
}
StringBuilder buffer = new StringBuilder();
buffer.Append("<root>");
buffer.Append("<item code=/"/" name=/"全部/" />");
for(int i=0;i<lst.Count;i++)
{
CityInfo info = (CityInfo)lst[i];
buffer.AppendFormat("<item code=/"{0}/" name=/"{1}/" />",
info.strName,info.strCodeName);
}
buffer.Append("</root>");
doc.DocumentElement.InnerXml = buffer.ToString();
Response.ContentType = "text/xml";
doc.Save(Response.OutputStream);
Response.End();
}
當然記得引入js啦
<script language="javascript" src="<%=PageUtilityTool.GetRootURL()%>/js/jquery.js"></script>
jQuery 中文社區 http://wiki.jquery.org.cn/doku.php