ASP.NET菜鳥之路之登錄系統


背景

  • 我是一個ASP.NET菜鳥,暫時開始學習ASP.NET,在此記錄下我個人敲的代碼,沒有多少參考價值,請看到的盆友們為我點個贊支持我一下,多謝了。

    網站介紹

  • 根據書上的例子做了一個比較粗糙的登錄例子,里面的代碼都是自己敲出來的,而且很少使用封裝方法,就是為了讓自己能更清楚的記住做的過程。
  • 這個網站包含注冊、登錄、修改密碼三個功能。
  • 注冊介紹

  • 新建一個Web窗體,即UserManagers.aspx。不粘貼前台代碼了。然后編寫注冊方法,包括用戶名當作主鍵,SqlDataReader方式讀取數據庫,SqlCommand參數添加數據等要點
  • protected void Button1_Click(object sender, EventArgs e)
        {
            if (txtName.Text == "" || txtPwd.Text == "" || txtConfirm.Text == "")
            {
                this.Page.RegisterStartupScript("ss", "<script>alert('用戶名密碼不能為空')</script>");
                return;
            }
    
            if (txtPwd.Text.Equals(txtConfirm.Text))
            {
                //查看當前用戶是否存在
                SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
                sqlConn.Open();
                string sql = "select * from tb_user where username = '" + txtName.Text.Trim() + "'";
                SqlCommand sqlCommand = new SqlCommand(sql, sqlConn);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                if (sqlDataReader.Read())
                {
                    Page.RegisterStartupScript("", "<script>alert('用戶名已存在!')</script>");
                    return; 
                }
                sqlDataReader.Close();
    
                //新增用戶
                string strInsert = "insert into tb_user(username, pwd, marks) values (@username,@pwd, @marks)";
                sqlCommand = new SqlCommand(strInsert, sqlConn);
                sqlCommand.Parameters.Add("@username", SqlDbType.VarChar);
                sqlCommand.Parameters["@username"].Value = txtName.Text;
    
                sqlCommand.Parameters.Add("@pwd", SqlDbType.VarChar, 20);
                sqlCommand.Parameters["@pwd"].Value = txtPwd.Text;
    
                sqlCommand.Parameters.Add("@marks", SqlDbType.VarChar, 1000);
                sqlCommand.Parameters["@marks"].Value = "zbq測試";
    
                sqlCommand.ExecuteNonQuery();
                sqlConn.Close();
    
                Page.RegisterStartupScript("", "<script>alert('注冊成功!')</script>");
                Response.Redirect("Default.aspx?Name=" + txtName.Text + "");
            }
        }

    界面效果如下

    QQ拼音截圖未命名image

    登錄介紹

  • 首先添加登錄窗口ManageLogin.aspx,然后寫登錄代碼,包含驗證碼這一要點

  • protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text)|| string.IsNullOrEmpty(txtPwd.Text) || string.IsNullOrEmpty(txtValid.Text))
            {
                Page.RegisterStartupScript("", "<script>alert('信息填寫不完全!')</script>");
                return;
            }
            if (!txtValid.Text.ToUpper().Equals(Session["ValidNums"]))
            {
                Page.RegisterStartupScript("", "<script>alert('驗證碼不正確!')</script>");
                return;
            }
    
            SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
            sql.Open();
    
            string select = "select * from tb_user t where t.username = '" + txtName.Text.Trim() + "' and pwd = '" + txtPwd.Text.Trim() +
                            "'";
    
            SqlCommand command = new SqlCommand(select, sql);
    
            SqlDataReader dataReader = command.ExecuteReader();
    
            if (dataReader.Read())
            {
                //成功就跳轉
                Response.Redirect("Default.aspx?Name=" + txtName.Text + "");
            }
            else
            {
                Page.RegisterStartupScript("", "<script>alert('賬戶名或密碼錯誤!')</script>");
                dataReader.Close();
                return;
            }
  • 登錄效果如圖
  • image  

    修改密碼介紹

  • 首先建立一個EditPwd.aspx窗體
  • <table class="table" border="1px" align="center">
                    <tr>
                        <td class="firstTd">用戶名:</td>
                        <td >
                            <asp:DropDownList runat="server" ID="names" Width="200px" Height="20px" />
                        </td>
                    </tr>
                    <tr>
                        <td class="firstTd">原密碼:</td>
                        <td >
                            <asp:TextBox runat="server" ID="txtOldPwd" TextMode="Password" />
                        </td>
                    </tr>
                    <tr>
                        <td class="firstTd">新密碼:</td>
                        <td >
                            <asp:TextBox runat="server" ID="txtNewPwd" TextMode="Password"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td class="firstTd">&nbsp;</td>
                        <td align="right">
                            <span >
                                <asp:Button runat="server"  ID="btnSure" OnClick="btnSure_Click" Text="確認登錄"/>
                                <asp:Button runat="server"  ID="btnCancle" OnClick="btnCancle_Click" Text="取消"/>    
                            </span>
                        </td>
                    </tr>
                    
                </table>

    然后編寫修改方法,包含SqlDataAdapter + DataSet關鍵點

    protected void Page_Load(object sender, EventArgs e)
        {
           //初始化數據
            if (!IsPostBack)
            {
                SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
                sql.Open();
                string select = "select * from tb_user";
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(select, sql);
    
                DataSet dataSet = new DataSet();
                sqlDataAdapter.Fill(dataSet);
                sql.Close();
                if (dataSet.Tables[0].Rows.Count> 0)
                {
                    for (int index = 0; index < dataSet.Tables[0].Rows.Count; index++)
                    {
                        names.Items.Add(dataSet.Tables[0].Rows[index][1].ToString());
    
                    }
                }
            }
        }
        protected void btnSure_Click(object sender, EventArgs e)
        {
    
            if (string.IsNullOrEmpty(txtNewPwd.Text) || string.IsNullOrEmpty(txtOldPwd.Text))
            {
                Page.RegisterStartupScript("", "<script>alert('密碼不能為空或者不能不相等!')</script>");
                return;
            }
    
            SqlConnection sqlConnection = new SqlConnection("server=PC-20150424DMHQ;database=MyDatas;uid=sa;pwd=123456");
            string select = "select * from tb_user where username = '" +names.Text + "'";
            SqlCommand sqlCommand = new SqlCommand(select, sqlConnection);
            sqlConnection.Open();
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            if (sqlDataReader.Read())
            {
                if (sqlDataReader["pwd"].ToString() != txtOldPwd.Text)
                {
                    Page.RegisterStartupScript("", "<script>alert('密碼輸入錯誤!')</script>");
                    return;
                }
            }
            else
            {
                Page.RegisterStartupScript("", "<script>alert('數據庫連接錯誤!')</script>");
                return;
            }
            sqlConnection.Close();
            sqlDataReader.Close();
    
            
            //修改密碼
            sqlConnection.Open();
            string updatePwd = "update tb_user set pwd = '" + txtNewPwd.Text + "' where username = '" + names.Text + "'";
            sqlCommand = new SqlCommand(updatePwd, sqlConnection);
    
            sqlCommand.ExecuteNonQuery();
            sqlConnection.Close();
            Page.RegisterStartupScript("", "<script>alert('修改成功!')</script>");
            Page_Load(null, null);
        }

    修改密碼界面效果

    image


    免責聲明!

    本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



     
    粵ICP備18138465號   © 2018-2025 CODEPRJ.COM