Zrlhappy的博客
                               从今天起, 做一个幸福的人
                                    喂马, 劈柴, 周游世界
                                    从今天起, 关心粮食和蔬菜
                                    我有一所房子, 面朝大海, 春暖花开
                                    给每一条河每一座山取一个温暖的名字
                                    我也愿面朝大海, 春暖花开……

//用户验证

1.default.aspx设计页中,设计登陆页面,用户名、密码和登陆按钮。代码如下:

<table width="350" border="0" align="center" bgcolor="#000000" cellspacing="1" cellpadding="10">

        <form name="formlogin" runat="server">

          <tr bgcolor="#FFFFFF">

            <td width="111"> <div align="center">用户名:</div></td>

            <td width="229"> &nbsp;<asp:TextBox ID="UserNameTextBox" runat="server"></asp:TextBox></td>

          </tr>

          <tr bgcolor="#FFFFFF">

            <td width="111"> <div align="center">密 码:</div></td>

            <td width="229"> &nbsp;<asp:TextBox ID="PasswordTextBox" runat="server" TextMode="Password"></asp:TextBox> </td>

          </tr>

          <tr align="center" bgcolor="#FFFFFF">

            <td colspan="2" style="height: 42px"> <asp:Button ID="LoginButton" runat="server" OnClick="LoginButton_Click" Text="登陆" />&nbsp;

                <input id="Reset1" type="reset" value="取消" /></td>

          </tr>

        </form>

      </table>

2.default.aspx.cs页面中,加入LoginButton_Click函数,验证用户名、密码。

   protected void LoginButton_Click(object sender, EventArgs e)

    {

        SqlConnection myconn;

        myconn = new SqlConnection(ConfigurationManager.ConnectionStrings["JlyConnectionString"].ConnectionString);

        myconn.Open();

        SqlCommand myCMD = new SqlCommand("select UserName,Password,TrueName from UserTable where UserName='" + UserNameTextBox.Text.ToString() + "' and Password='" + PasswordTextBox.Text.ToString() + "'", myconn);

        SqlDataReader myReader = myCMD.ExecuteReader();

        if (myReader.Read())//如果正确,则跳转到Main.aspx页,进入系统;如果sql运行结果是空,则还在当页。

        {

            Session["TrueName"] = myReader.GetString(2).ToString();

            Response.Redirect("main.aspx");

        }

        else

        {

            Response.Redirect("default.aspx");

            Session["TrueName"] = null;

        }

    }

posted on 2006-03-10 09:47  zrlhappy  阅读(237)  评论(0编辑  收藏  举报