作者:Dflying Chen (http://dflying.cnblogs.com/)
PS:又是好久沒更新了,慚愧啊……希望大家繼續支援我。
在這個系列的上面幾篇(開發ASP.NET Atlas伺服器端Extender控制項——基本概念以及預先需求,開發ASP.NET Atlas伺服器端Extender控制項——編寫用戶端Behavior,開發ASP.NET Atlas伺服器端Extender控制項——編寫伺服器端Extender & Dflying近期動向)中,我們完整地開發了一個Atlas伺服器端Extender控制項。本篇是其中的最後一篇,將介紹如何使用前面編寫的這個控制項。
首先,建立一個Atlas Web Site,並添加對上面編寫的控制項的引用,請參考:
然後,編寫用於檢查使用者名稱的Web Service,這裡我僅僅簡單的假定使用者名稱不能取Dflying。當然在具體的實現中您可以從資料庫中查詢並驗證。將其儲存為UserRegisterService.asmx:
[WebService(Namespace = "http://www.dflying.net/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class UserRegisterService : System.Web.Services.WebService {
[WebMethod]
public bool CheckUserName(string userNameCandidate) {
if (userNameCandidate == "Dflying")
return false;
return true;
}
}
然後,在頁面中添加Atlas的ScriptManager:
<atlas:ScriptManager ID="ScriptManager1" runat="server" />
添加一個TextBox,用來輸入使用者名稱;一個Label,用來顯示驗證結果。注意到這兩個控制項都是ASP.NET的伺服器端控制項,也就是說您可以採用所見即所得 (WYSIWYG)的開發方式,從Toolbox中將它們拖拽到頁面上,和我們熟悉的ASP.NET開發模型一樣。
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server"></asp:Label>
然後是本執行個體的核心部分,ValidateUserNameExtender的聲明。在頁面中輸入下面的代碼,指定我們的控制項的屬性。當然,如果你願意,也可以將其添加到Toolbox中,並拖拽到頁面上。
<dflying:ValidateUserNameExtender ID="validateUserName" runat="server">
<dflying:ValidateUserNameProperties CheckResultLabelID="Label1" TargetControlID="TextBox1"
ServiceName="UserRegisterService.asmx" MethodName="CheckUserName" ValidMessage="You can use this user name."
InvalidMessage="This user name has already been used, please choose another." />
</dflying:ValidateUserNameExtender>
注意到輸入的同時IDE的智能感知功能:
屬性視窗中也列出了所有可選屬性,這些都大大加快了我們的開發速度:
大功告成,瀏覽器中測試一下,可以使用的使用者名稱:
不可以使用的使用者名稱:
ValidateUserNameExtender和本樣本程式的代碼可以在此下載:http://files.cnblogs.com/dflying/ValidateUserName.zip