您的位置:首页 > 其它

实现textbox输入时模糊查询

2010-05-06 14:50 447 查看
时间有点紧,有点浮躁,暂时只把相关的文件挑出来看下,思路有时间再整理

1。引用的一个文件

代码

public partial class AutoComplete : System.Web.UI.Page
{
CommBO bo = new CommBO();
string flag = string.Empty;// 标示符
string InputValue = string.Empty;//输入值
string ReturnStr = string.Empty;//xml

// 页面加载事件
protected void Page_Load(object sender, EventArgs e)
{
try
{
flag = Request.QueryString["flag"] ?? "";//查询条件标示符
InputValue = Request.QueryString["InputValue"];//输入值
AutoCompletXML();//填充xml
HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);//禁用本地缓存
HttpContext.Current.Response.Cache.SetNoStore();
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
HttpContext.Current.Response.Write( ReturnStr);
HttpContext.Current.Response.End();
}
catch
{ ; }
}
// 填充xml
private void AutoCompletXML()
{
try
{
Dictionary<string, object> dict = new Dictionary<string, object>();
DataSet ds = new DataSet();
int intRowCount = 10;//默认自动完成显示最大行数
intRowCount = Convert.ToInt32(ConfigurationManager.AppSettings["AutoCompleteRowCount"].Trim());
if (flag.Equals("1"))
{
dict.Add("@" + ConstMessage.TABLE_FIELD_NAME.Product.PRODUCTCODE ,"%" + InputValue +"%");
}
else if (flag.Equals("2"))
{
dict.Add("@" + ConstMessage.TABLE_FIELD_NAME.Product.PRODUCTNAME ,"%"+ InputValue +"%");
}
else
{
return;
}
//执行数据库操作
ds = bo.GetByProduct(dict,intRowCount);
ReturnStr = ds.GetXml();//生成xml
}
catch
{
ReturnStr = string.Empty;
}

}
}

3。使用页面,对应的页面控件

----添加js引用

----页面添加控件

<div>
<asp:TextBox ID="txtProductCode" runat="server" CssClass="text_Mandatory" MaxLength="25"
AutoCompleteType="Disabled"></asp:TextBox>
<asp:TextBox ID="txtProductName" runat="server" CssClass="text_ProductName" MaxLength="30"
AutoCompleteType="Disabled"></asp:TextBox>
</div>

---cs中为txtbox添加js事件

this.txtProductCode.Attributes.Add("onkeyup", "findNames('" + this.txtProductCode.ClientID + "','" + this.txtProductName.ClientID + "','1');");
this.txtProductName.Attributes.Add("onkeyup", "findNames('" + this.txtProductName.ClientID + "','" + this.txtProductCode.ClientID + "','2');");
this.txtProductCode.Attributes.Add("onblur", "lostfocus(this);");
this.txtProductName.Attributes.Add("onblur", "lostfocus(this);");
4。数据库调用层根据编码或名称的模糊查询略
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐