用户控件内实现文本框自动完成扩展

在开发的站点中,想实现一个搜索功能。由于需要放在站点的头部,这样的话,每一个网页都可以使用这个搜索功能。
开发设计,就把这个搜索功能独立出来,写成一个用户控件(ascx),然后拉至母版(MasterPage)上,文本框内有使用Ajax自动完成扩展功能。满怀信心完成...
效果如下:

在网页运行时,发现文本框的自动完成的功能无效果!更多的AutoComplete 的实现,可参考:http://www.cnblogs.com/insus/archive/2013/03/28/2986217.html 此实现只能在正常aspx网页正常运行。一旦移至用户控件之后,Autocomplete的功能,就随之消失。

 难道在用户控件不能使用Ajax的AutoCompleteExtender? 几经测试,有一次竟然成功了!成功的原因,原来WebMethod只能写在aspx.cs页面内:

View Code
[System.Web.Services.WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public static string[] GetDisplayUserName(string prefixText, int count)
        {
            ActiveDirectoryInfo objActiveDirectoryInfo = new ActiveDirectoryInfo();

            DataTable dt = objActiveDirectoryInfo.GetDisplayName(prefixText, count);
            ArrayList array = new ArrayList();

            foreach (DataRow dataRow in dt.Rows)
            {
                array.Add(dataRow["displayName"].ToString());
            }

            return (string[])array.ToArray(typeof(string));
        }  


由于网站很多网页,Insus.NET不可能在每一个aspx.cs 都复制与粘贴这段代码的。解决的方法,写一个BasePage.cs这个类继承System.Web.UI.Page

public class BasePage : System.Web.UI.Page


然后把上面的的webMethod方法代码写在这个BasePage内,站点的网页再继承这个BasePage类即可。

其实,问题是因为没有使用Web Service导致的。如果把它改为Web service或Wcf service,一切都没有这样复杂。

 

posted @ 2013-04-05 14:06  Insus.NET  阅读(1580)  评论(0编辑  收藏  举报