開發ASP.NET Atlas伺服器端Extender控制項——在實際開發中使用編寫好的控制項

來源:互聯網
上載者:User
作者: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.