一款异步加载下拉框插件

一款不错的基于JQuery的auto-complete插件,这款插件最大的好处就是我们能够得到{value, text}结构的数据,这与传统的下拉框很相似,非常适合用于项目开发。

生成后的Html页面代码

代码
<div style="width: 200px;" class="dhx_combo_box ">
<input type="text" autocomplete="off" class="dhx_combo_input" style="width: 181px;">
<input type="hidden" name="LocalityObject" value="14523">
<input type="hidden" name="LocalityObject_new_value" value="false">
<img class="dhx_combo_img" src="http://www.cnblogs.com/Scripts/DHTMLX/codebase/imgs/combo_select.gif">
</div>

 

以下是基于MVC的一个例子

 

前台调用

代码
<script type="text/javascript">

$(
function() {
window.dhx_globalImgPath
= "http://www.cnblogs.com/Scripts/DHTMLX/codebase/imgs/";
var z = new dhtmlXCombo("localobj", "LocalityObject", 200, 22);
z.enableFilteringMode(
true, "http://www.cnblogs.com/Community/AsynchronousLocality", true, true);
});
</script>

 

后台代码

代码
public ActionResult AsynchronousLocality()
{
XmlDocument xmldoc
= new XmlDocument();
var content
= BuildXML();
try
{
xmldoc.LoadXml(content);
}
catch(Exception e) {

}

return new XmlResult(xmldoc);
}

private string BuildXML()
{
string qstring = Request.QueryString["mask"].SafeString();
string start = Request.QueryString["pos"].SafeString();

StringBuilder sb
= new StringBuilder();

sb.Append(
"<?xml version=\"1.0\" ?>");
sb.AppendFormat(
"<complete{0}>", start.SafeInt().Equals(0)? string.Empty : " add=\"true\"");
if (!string.IsNullOrEmpty(qstring))
{
var communities
= Service.ListLocalityByName(qstring);

for (var i = 0; i < communities.Count; i++)
{
if (i > 50) break;
sb.AppendFormat(
"<option value=\"{0}\">{1}</option>", communities[i].Id, communities[i].LocalityName);
}
}
sb.Append(
"</complete>");

return sb.ToString().Replace("&", "&amp;");
}

 

XmlResult类

代码
public class XmlResult : ActionResult
{
private object objectToSerialize;

/// <summary>
/// Initializes a new instance of the <see cref="XmlResult"/> class.
/// </summary>
/// <param name="objectToSerialize">The object to serialize to XML.</param>
public XmlResult(object objectToSerialize)
{
this.objectToSerialize = objectToSerialize;
}

/// <summary>
/// Gets the object to be serialized to XML.
/// </summary>
public object ObjectToSerialize
{
get { return this.objectToSerialize; }
}

/// <summary>
/// Serialises the object that was passed into the constructor to XML and writes the corresponding XML to the result stream.
/// </summary>
/// <param name="context">The controller context for the current request.</param>
public override void ExecuteResult(ControllerContext context)
{
if (this.objectToSerialize != null)
{
context.HttpContext.Response.Clear();
var xs
= new System.Xml.Serialization.XmlSerializer(this.objectToSerialize.GetType());
context.HttpContext.Response.ContentType
= "text/xml";
xs.Serialize(context.HttpContext.Response.Output,
this.objectToSerialize);
}
}
}
posted @ 2010-07-12 13:22  C.Jun  阅读(539)  评论(0编辑  收藏  举报