Atlas 学习笔记: ajax 改进 by Atlas
废话不多说,今天试试atlas
1. 加入atlas 的 scriptManager 因为是后台直接调webservice 所以加上webservice的地址
<atlas:ScriptManager ID="scriptManager" runat="server">
<Services>
<atlas:ServiceReference Path="WebService.asmx" />
</Services>
</atlas:ScriptManager>
<Services>
<atlas:ServiceReference Path="WebService.asmx" />
</Services>
</atlas:ScriptManager>
2. OK 在来一个text 注意在后面 spanid 要加一个名字 这个名字要和下面对应起来
<input id="Text1" type="text" /><span id="Text1__autocomplete"></span>
3. 这段代码大家看看 指定 text1 要完成的行为 behaviors 里面指定了autoComplete 指定了方法serviceMethod="GetCompletionList" minimumPrefixLength ="1" 指定你输入多少个字符就开始触发这个auto事件最后 completionList ="Text1__autocomplete" 这个名字和第二步的名字 要统一
<script type="text/xml-script">
<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
<components>
<label id="Text1" targetElement="Text1">
<behaviors>
<autoComplete serviceURL="WebService.asmx" serviceMethod="GetCompletionList" minimumPrefixLength="1" completionList="Text1__autocomplete" />
</behaviors>
</label>
</components>
<references>
</references>
</page>
</script>
<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
<components>
<label id="Text1" targetElement="Text1">
<behaviors>
<autoComplete serviceURL="WebService.asmx" serviceMethod="GetCompletionList" minimumPrefixLength="1" completionList="Text1__autocomplete" />
</behaviors>
</label>
</components>
<references>
</references>
</page>
</script>
4 下面来写webservice时候实现 的代码 基本上就是一个查询 注意返回值的类型
[WebMethod]
public string[] GetCompletionList(string prefixText)
{
string temp = "";
List<string> suggestions = new List<string>();
if (prefixText != "")
{
string mySelectQuery = "SELECT ContactName FROM Customers where ContactName like '" + prefixText + "%'";
SqlConnection myConnection = new SqlConnection(@"server=ZTE-WUANCHENG\wuancheng_zte;database=Northwind;User ID=sa;password=;Persist Security Info=true;");
SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
myConnection.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
try
{
while (myReader.Read())
{
suggestions.Add(myReader.GetString(0));
temp = temp + myReader.GetString(0) + ", ";
}
}
finally
{
// always call Close when done reading.
myReader.Close();
// always call Close when done reading.
myConnection.Close();
}
}
return suggestions.ToArray();
}
public string[] GetCompletionList(string prefixText)
{
string temp = "";
List<string> suggestions = new List<string>();
if (prefixText != "")
{
string mySelectQuery = "SELECT ContactName FROM Customers where ContactName like '" + prefixText + "%'";
SqlConnection myConnection = new SqlConnection(@"server=ZTE-WUANCHENG\wuancheng_zte;database=Northwind;User ID=sa;password=;Persist Security Info=true;");
SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
myConnection.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
try
{
while (myReader.Read())
{
suggestions.Add(myReader.GetString(0));
temp = temp + myReader.GetString(0) + ", ";
}
}
finally
{
// always call Close when done reading.
myReader.Close();
// always call Close when done reading.
myConnection.Close();
}
}
return suggestions.ToArray();
}
5 . ok 看看效果
还可以把 参考了altas 的例子