ASP.ENT Ajax自动补全(用户控件版)

网友投稿 556 2022-11-15

ASP.ENT Ajax自动补全(用户控件版)

ASP.ENT Ajax自动补全(用户控件版)

​​源码-​

​​其他-​​

ASP.ENT自动补全(用户控件版) ,支持中文,支持回车回调,支持Tab回调,支持一个页面多次使用不冲突,兼容IE5、IE6、IE7、IE8、IE9、IE10、IE11、火狐、谷歌、360浏览器(兼容模式、快速模式),其他浏览器

通过大量测试并升级的最终使用效果:

新增AjaxAutocompleteTable.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AjaxAutocompleteTable.ascx.cs" Inherits="Unisoft.ICS.Web.UserControls.Common.AjaxAutocompleteTable" %><%--//请注意:使用当前用户控件,需要将此段代码复制到引用页的script块内,否则控件会出现异常。function ajaxComplete[此处填写引用控件初始化时指定的AjaxCompleteID]CallBack(e) {//e=> ID:e.id();txt:e.text(); e.id();//读取ID e.text();//读取text [此处填写引用控件初始化时指定的AjaxCompleteID].setdata("");//设置文本框内容 [此处填写引用控件初始化时指定的AjaxCompleteID].searchdata();//读取文本框内容 [此处填写引用控件初始化时指定的AjaxCompleteID].focus();//设置文本框焦点}function ajaxComplete[此处填写引用控件初始化时指定的AjaxCompleteID]KeyTabCallBack(e) {}--%>

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace Unisoft.ICS.Web.UserControls.Common{ public partial class AjaxAutocompleteTable : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { } ///

/// 初始化自动补全控件(参数加密版) /// this.ucAjaxAutocompleteTable.BindAutoComplete("[UserName],[LoginName]", "OA_USER", "UserID as ID,userName,LoginName", "UserID asc", "UserName,LoginName"); /// /// 设置AjaxComplete动态变量名(必填;说明=>传入Customer,生成的变量为:ajaxCompleteCustomer,生成回调方法为:ajaxCompleteCustomerCallBack,生成回调方法为:ajaxCompleteCustomerKeyTabCallBack) /// 初始文本框显示内容(选填;) /// 文本框显示的提示信息(选填;) /// 显示列名(用户名称;登陆帐号) /// 表,视图名称(必填;table_name/view_name) /// 显示字段(必填;第一列必须命名为ID,作为主键值;例:UserID as ID,UserName,LoginName) /// 排序字段(选填) /// 筛选字段(必填;字段1,字段2,字段3) /// 筛选条件(必填;IsEnable=1 and ColumnName like '%NET%') /// 显示条数(选填,不填则写0;默认10条) /// 结果列表div宽度(选填,不填则写"";默认265px) public void BindAutoComplete(string AjaxCompleteID, string AjaxCompleteText, string Placeholder, string ColArray, string TableName, string SelectorString, string SortExpression, string FilterString, string FilterColumnString, int TopCount, string Width) { ViewState["ucAjaxComplete_ColArray_" + this.AjaxComplete.ClientID] = ColArray; ViewState["ucAjaxComplete_TableName_" + this.AjaxComplete.ClientID] = Utility.Web.Security.DESEncrypt(TableName); ViewState["ucAjaxComplete_SelectorString_" + this.AjaxComplete.ClientID] = Utility.Web.Security.DESEncrypt(SelectorString); ViewState["ucAjaxComplete_SortExpression_" + this.AjaxComplete.ClientID] = Utility.Web.Security.DESEncrypt(SortExpression); ViewState["ucAjaxComplete_FilterString_" + this.AjaxComplete.ClientID] = Utility.Web.Security.DESEncrypt(FilterString); ViewState["ucAjaxComplete_FilterColumnString_" + this.AjaxComplete.ClientID] = Utility.Web.Security.DESEncrypt(FilterColumnString); ViewState["ucAjaxComplete_TopCount_" + this.AjaxComplete.ClientID] = TopCount; ViewState["ucAjaxComplete_Width_" + this.AjaxComplete.ClientID] = Width; ViewState["ucAjaxComplete_AjaxCompleteText_" + this.AjaxComplete.ClientID] = AjaxCompleteText; ViewState["ucAjaxComplete_Placeholder_" + this.AjaxComplete.ClientID] = Placeholder; ViewState["ucAjaxComplete_AjaxCompleteID_" + this.AjaxComplete.ClientID] = "ajaxComplete" + AjaxCompleteID; ViewState["ucAjaxComplete_CallBackFunctionName_" + this.AjaxComplete.ClientID] = "ajaxComplete" + AjaxCompleteID + "CallBack"; ViewState["ucAjaxComplete_KeyTabCallBackFunctionName_" + this.AjaxComplete.ClientID] = "ajaxComplete" + AjaxCompleteID + "KeyTabCallBack"; } }}

WebService:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;using System.Data;using Unisoft.ICS.Utility.Data;using Unisoft.ICS.Utility.Web;using System.Web.Script.Services;namespace Unisoft.ICS.Web.WebServices{ ///

/// AutoComplete 的摘要说明 /// [WebService(Namespace = " [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 [System.Web.Script.Services.ScriptService] public class AutoComplete : System.Web.Services.WebService { /// /// 自动补全控件(参数加密版) /// GetAjaxCompleteData("OA_USER", "UserID as ID,userName,LoginName", "UserID asc", "UserName,LoginName","IsEnable=1","a",10); /// /// 表,视图名称(必填;table_name/view_name) /// 显示字段(必填;第一列必须命名为ID,作为主键值;例:UserID as ID,UserName,LoginName) /// 排序字段(选填) /// 筛选字段(必填;字段1,字段2,字段3) /// 筛选条件(必填;IsEnable=1 and ColumnName like '%NET%') /// 筛选内容(必填;张丽瑕) /// 显示条数(选填;默认10条) /// /// 输入'a',查询结果: /// return Table /// 1000 admin admin /// 1001 宣网晴 xuanwangqing /// 1002 张丽瑕 zhanglixia /// return Json:[{"ID":1000,"userName":"admin","loginName":"admin"},{"ID":1001,"userName":"宣网晴","loginName":"xuanwangqing"},{"ID":1002,"userName":"张丽瑕","loginName":"zhanglixia"}] /// [WebMethod] public void GetAjaxCompleteData(string Col_1, string Col_2, string Col_3, string Col_4, string Col_5, string Col_6, int Col_7) { try { Col_1 = Utility.Web.Security.DESDecrypt(Col_1); Col_2 = Utility.Web.Security.DESDecrypt(Col_2); Col_3 = Utility.Web.Security.DESDecrypt(Col_3); Col_4 = Utility.Web.Security.DESDecrypt(Col_4); Col_5 = Utility.Web.Security.DESDecrypt(Col_5); if (Col_1 != string.Empty && Col_2 != string.Empty && Col_4 != string.Empty && Col_6 != string.Empty && Col_7 > 0) { string[] inputSplit = Col_4.Split(','); Col_4 = string.Empty; foreach (string item in inputSplit) { Col_4 += ((Col_4 != string.Empty ? " or " : "") + (item + " like ''%" + Col_6 + "%''")); } if (Col_5 != string.Empty) { Col_4 = "(" + Col_4 + ") and " + Col_5; } DataSet ds = DbHelperSQL.Query(string.Format("exec usp_Common_Sel_Table '{0}','{1}','{2}','{3}',{4}", Col_1, Col_2, Col_3, Col_4, Col_7)); if (ds != null && ds.Tables.Count > 0) { Context.Response.Charset = "UTF-8"; //设置字符集类型 Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); Context.Response.Write(JsonHelper.DataTableToJson(ds.Tables[0])); } } else { Context.Response.Charset = "UTF-8"; //设置字符集类型 Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); Context.Response.Write("缺少必须的参数!"); } } catch (Exception ex) { Context.Response.Charset = "UTF-8"; //设置字符集类型 Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); Context.Response.Write(ex.Message); } } }}

JsonHelper

#region DataTable序列化json ///

/// DataTable转为json /// /// DataTable /// json数据 public static string DataTableToJson(DataTable dt) { List> list = new List>(); foreach (DataRow dr in dt.Rows) { Dictionary result = new Dictionary(); foreach (DataColumn dc in dt.Columns) { result.Add(dc.ColumnName, dr[dc]); } list.Add(result); } return SerializeDataTableToJson(list); } /// /// 序列化对象为Json字符串 /// /// 要序列化的对象 /// 序列化对象的深度,默认为100 /// Json字符串 public static string SerializeDataTableToJson(this object obj) { JavaScriptSerializer serialize = new JavaScriptSerializer(); serialize.RecursionLimit = 100; return serialize.Serialize(obj); } #endregion

查询存储过程

ALTER PROCEDURE [dbo].[usp_Common_Sel_Table] -- Add the parameters for the stored procedure here @TableName nvarchar(128) ,@SelectorString nvarchar(500) = '*' ,@SortExpression nvarchar(200) ,@FilterString nvarchar(2000) ,@TopCount int = 0ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here DECLARE @Filter NVARCHAR(2050); DECLARE @SqlString NVARCHAR(4000); DECLARE @SortExp NVARCHAR(250); DECLARE @SelectTop NVARCHAR(20); IF @FilterString is NULL or @FilterString = '' BEGIN SET @Filter = '' END ELSE BEGIN SET @Filter = ' WHERE ('+ @FilterString +') ' END IF @SortExpression is NULL or @SortExpression = '' BEGIN SET @SortExp = '' END ELSE BEGIN SET @SortExp = ' ORDER BY ' + @SortExpression END IF @TopCount > 0 BEGIN SET @SelectTop = ' TOP('+Cast(@TopCount as Nvarchar)+') ' END ELSE BEGIN SET @SelectTop = '' END /* Specify the parameter format one time. */ SET @SqlString = N' SELECT ' + @SelectTop + ' ' + @SelectorString + ' FROM [' + @TableName + '] ' + @Filter + ' ' + @SortExp --PRINT @SqlString EXECUTE sp_executesql @SqlStringEND

aspx

<%@ Register Src="~/UserControls/Common/AjaxAutocompleteTable.ascx" TagName="AjaxAutocompleteTable" TagPrefix="uc" %>

aspx.cs Page_Load

this.ucAjaxAutocompleteTable.BindAutoComplete("", this.hidFromContractNum.Value, "请输入合同编号或合同名称查询", "[合同编号],[合同名称]","tbl_Sale_SaleContract", "ContractID as ID,ContractNum,htname", "ContractID desc", "ContractNum,htname", "CompanyID=" + this.LogonUser.CompanyID, 10, "265px");

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:第一章 微型计算机系统导论【微机原理】
下一篇:CAD添加页眉或页脚数据(com接口)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~