国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

ASP.NET菜鳥之路之登錄系統(tǒng)

2019-11-14 15:56:55
字體:
來源:轉載
供稿:網(wǎng)友

背景

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

    網(wǎng)站介紹

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

  • 新建一個Web窗體,即UserManagers.aspx。不粘貼前臺代碼了。然后編寫注冊方法,包括用戶名當作主鍵,SqlDataReader方式讀取數(shù)據(jù)庫,SqlCommand參數(shù)添加數(shù)據(jù)等要點
  • 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)    {       //初始化數(shù)據(jù)        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('數(shù)據(jù)庫連接錯誤!')</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


    發(fā)表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發(fā)表
    主站蜘蛛池模板: 西华县| 镇安县| 睢宁县| 会昌县| 呼玛县| 五家渠市| 社旗县| 望都县| 繁峙县| 新田县| 玉林市| 旺苍县| 思茅市| 彩票| 比如县| 江津市| 龙岩市| 陵川县| 台南县| 津市市| 始兴县| 定襄县| 黑山县| 胶州市| 沙河市| 江北区| 西峡县| 桃源县| 东宁县| 建阳市| 辽阳市| 舒兰市| 乐陵市| 肃北| 阿勒泰市| 博爱县| 宿迁市| 吉木萨尔县| 浦东新区| 东丰县| 长治市|