登陸、註冊小製作

週末閒來無事...製作一個登陸和註冊的頁面,代碼頁面如下:

登陸頁面:

 protected void Button1_Click(object sender, EventArgs e)//登錄按鈕單擊事件
    {
        string str = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;//連接數據庫
        using (SqlConnection sqlcnn=new SqlConnection(str))//創建Connection對象
        {
            using (SqlCommand sqlcmm = sqlcnn.CreateCommand())//創建Command對象
            {
                sqlcmm.CommandText = "select name,password from users";//要執行的數據庫語句
                sqlcnn.Open();//打開數據庫
                sqlcmm.ExecuteNonQuery();//執行數據庫語句
            }
        }
        Response.Redirect("shoppingcad.aspx");//跳轉登陸成功以後的頁面
    }
    protected void Button3_Click(object sender, EventArgs e)//註冊按鈕單擊事件
    {
        Response.Redirect("reg.aspx");//跳轉到註冊頁面
    }

註冊頁面:

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)//註冊按鈕單擊事件
        {
            if (TextBox6.Text.ToUpper().ToString() == Session["Verification_code"].ToString())//判斷驗證碼(TextBox6中所輸內容爲驗證碼)
            {
                Myencrypt myen = new Myencrypt();
                object objs = SqlHelper.ExecuteScalar("insert into users (name,password,email,money,Points) values (@n,@p,@e,'10000','0');select @@IDENTITY",
                    new SqlParameter("@n", TextBox1.Text),
                    new SqlParameter("@p", myen.Encryptions(TextBox2.Text)),
                    new SqlParameter("@e", TextBox4.Text));//寫入數據庫中所填寫的用戶名、密碼、郵箱
                if (objs != null)//判斷OBJS
                {
                    FSMail(Convert.ToInt32(objs));//執行發送郵件方法
                    Response.Redirect("login.aspx");跳轉註冊成功以後的頁面
                }
            }
            else
            {
                TextBox6.Text = "驗證碼錯誤";
                TextBox6.Focus();
            }
        }

        private void FSMail(int num)//發送郵件的方法
        {
            try
            {
                string strSmtpServer = "smtp.163.com";
                string strFrom = "<A href="mailto:[email protected]";//">[email protected]";//發件人郵箱
                string strFromPass = "******";//發件人密碼
                string strto = this.TextBox4.Text;//收件人郵箱
                string strSubject = "您註冊了京東商城賬號";//所發郵件標題
                string strBody = "恭喜你註冊成功,您的用戶ID:" + num + ",賬號:" + this.TextBox1.Text + ",密碼:" + this.TextBox2.Text + ",請用用戶ID登錄。";//所發郵件內容
                System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
                client.UseDefaultCredentials = true;
                client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;

                System.Net.Mail.MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = true;
                client.Send(message);
            }
            catch(Exception)
            {

            }
        }

以上爲兩個頁面的後臺代碼!主要是熟練練習連接數據庫的代碼,還有要執行的數據庫語句!下邊是拓展一點小知識:發送郵件!

感覺收穫很大,你也試試吧!其實很簡單......

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章