1.今天在写一条Sql语句的时候犯了2个错误,语句如下:
Select distinct top @Count contactid,email from contact
Where contact like ‘@prefix%’
我是在后台代码中写SQL,首先是提示异常”@Count附近有语法错误”,找了很久没发现,后来在一个英文网上找到方法,加上()就可以了:select (@Count)…,具体原因没看到解释,可能是SQL Server不支持吧。然后是like的写法,按那样写我查到的结果总为0,后来突然想到,干嘛要把%写在语句中呢,直接赋给参数的Value不就可以了吗,哎,真傻。
2. 今天使用AjaxControlToolKit的AutoCompleteExtender,这个控件还挺有意思的,用这个完成自动提示功能很方便。
使用如下,比如要控制TextBox控件
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<AjaxControlToolkit:AutoCompleteExtender ID="autoCmp" runat="server" ServiceMethod="GetData" MinimumPrefixLength="2" TargetControlID="txt1" CompletionInterval="100">
</AjaxControlToolkit:AutoCompleteExtender>
首先,不管使用AjaxToolKit的哪个Control,都要有一个ScriptManager控件,TargetControlID表示要控制的控件ID,ServiceMethod表示服务方法如Webmethod,ServicePath指服务的地址,如asmx文件,通常使用Web服务来响应,但也可以用Page的PageMethod,即public,static的方法,并且要符合形式,返回string[],带有2个参数string prefix和int count,或3个参数string prefix,int count和string contextKey,并且还要有[WebMethod]标签,如下:
[WebMethod]
public static string[] GetData(string prefixText, int count)
{
//To Do
}
或
[WebMethod]
public static string[] GetData(string prefixText, int count,string contextKey)
{
//To Do
}
还有一个问题,通常选提示框得到Text,但有时需要得到Value,可以使用以下方法,首先用AutoCompleteExtender 的CreateAutoCompleteItem 方法创建Item对象,CreateAutoCompleteItem(string text,string value),得到一个包含text和value的item;然后在客户端使用事件
OnClientItemSelected,当选择某一项时,value值赋给某个隐藏控件,如下:
function AutoCmpFromItemSelected(source, eventArgs) {
document.getElementById("hdFromContactID").value = eventArgs.get_value();
}
hdFromContactID为某个hidden控件的ID.