熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

“事件存取器”使用示例

2013-11-13 09:58:35  來源: .NET編程 

  本文的示例是一個登陸窗口
    主要組成
    一個用戶控件(存放登陸所用的控件)
    一個登陸頁面(放置上面的用戶控件)

  用戶控件的HTML代碼為
    <%@ Control Language=C# AutoEventWireup=true CodeBehind=LoginCtrlascxcs
    Inherits=CNLanPeiESPlusPAMSWebAppHomeUserCtrlsLoginCtrl %>
    <table border= cellpadding= cellspacing=>
        <tr>
            <td align=right valign=middle >用戶名&nbsp;</td>
            <td align=left valign=bottom>
                <asp:TextBox ID=txtUserName runat=server Width=px></asp:TextBox>
            </td>
            <td rowspan= align=right valign=middle>
                <asp:ImageButton SkinID=LoginImageBtn ID=ImageBtnLogin runat=server/>
            </td>
        </tr>
        <tr >
            <td colspan=></td>
        </tr>
        <tr>
            <td align=right valign=middle >密碼&nbsp;</td>
            <td align=left valign=top >
                <asp:TextBox ID=txtUserPwd runat=server Width=px TextMode=Password></asp:TextBox>
            </td>
        </tr>
    </table>

  下面就是用戶控件的CS代碼裡面就是事件存取器的使用
    using System;
    using SystemWebUI;

  namespace CNLanPeiESPlusPAMSWebAppHomeUserCtrls
    {
        /// <summary>
        /// 登錄控件
        /// </summary>
        public partial class LoginCtrl : SystemWebUIUserControl
        {
            /// <summary>
            /// 獲取用戶名
            /// </summary>
            public string UserName
            {
                get
                {
                    return thistxtUserNameTextTrim();
                }
            }

  /// <summary>
            /// 獲取用戶密碼
            /// </summary>
            public string UserPwd
            {
                get
                {
                    return thistxtUserPwdTextTrim();
                }
            }

  // 登錄事件關鍵字
            private static readonly string loginEvent = _LoginEvent;

  /// <summary>
            /// 添加或移除登錄事件
            /// </summary>
            public event EventHandler LoginEvent
            {
                add
                {
                    thisEventsAddHandler(loginEvent value);
                }
                remove
                {
                    thisEventsRemoveHandler(loginEvent value);
                }
            }

  /// <summary>
            /// 控件初始化函數
            /// </summary>
            /// <param name=e></param>
            protected override void OnInit(EventArgs e)
            {
                baseOnInit(e);
                thisImageBtnLoginClick += new ImageClickEventHandler(ImageBtnLogin_Click);
            }

  /// <summary>
            /// 登錄按鈕點擊事件
            /// </summary>
            /// <param name=sender></param>
            /// <param name=e></param>
            private void ImageBtnLogin_Click(object sender ImageClickEventArgs e)
            {
                EventHandler handler = thisEvents[loginEvent] as EventHandler;

  if (handler != null)
                {
                    handler(this e);
                }
            }
        }
    }

  然後把這個用戶控件拖到頁面中相應的位置上頁面的HTML代碼就不貼了讓我們看看我們應該如何使用添加進來的事件
    using System;
    using CNLanPeiESPlusPAMSServiceFacadeInternalService;

  namespace CNLanPeiESPlusPAMSWebAppHome
    {
        /// <summary>
        /// 用戶登錄頁面
        /// </summary>
        public partial class Login : SystemWebUIPage
        {
            /// <summary>
            /// 頁面初始化函數
            /// </summary>
            /// <param name=e></param>
            protected override void OnInit(EventArgs e)
            {
                baseOnInit(e);
                thisLoginCtrlLoginEvent +=new EventHandler(LoginCtrl_LoginEvent);
            }

  /// <summary>
            /// 登錄控件的登錄事件
            /// </summary>
            /// <param name=sender></param>
            /// <param name=e></param>
            private void LoginCtrl_LoginEvent(object sender EventArgs e)
            {
                string userName = thisLoginCtrlUserName;
                string userPwd = thisLoginCtrlUserPwd;

  // 用戶登錄
                if((new Visitor())Login(userNameuserPwd))
                {
                    ResponseRedirect(Main/SuperAdmin/Defaultaspxtrue);
                }
            }
        }
    }

  好了這就是它的使用方法


From:http://tw.wingwit.com/Article/program/net/201311/12162.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.