yp秋水伊人

导航

WebForm--j简单控件、简单的登录(怎么链接数据库)

一、简单控件

1、label:边框(边框的颜色、样式、粗细)  是专门显示文字的,   被编译后是    <span id="Label1">Label</span>

2、Literal:  Text属性,作用显示文字  编译后不会形成任何元素,一般被用来输出Js代码,比较灵活

       <asp:Literal ID="Literal2" runat="server" Text="mm"></asp:Literal>

3、Textbox:文字输入框, 编译后是:<input name="TextBox3" type="password" id="TextBox3" />

 属性:  wrap:自动换行

           Text Mode:可以是文本框、密码框(password)

                SingleLine 被编译为 type="text"

              --MultiLine  被编译为  type="text"

              --Password 被编译为  textarea

           Enabled:可用 或 不可用,编译后是:<input name="TextBox3" type="password" id="TextBox3" disabled="disabled" class="aspNetDisabled" />

           Readonly:只读

          Maxlength:限制长度,一般用于用户名、密码的长度。

 --

4、Button:按钮,编译后是   <input type="submit" name="Button2" value="Button" id="Button2" />提交按钮

    OnclientClick:在服务端上的点击事件,编译为click        confirm   验证判断

5、ImageButton:属性:imageurl:图片地址,——提交

6、LinkButton:超链接,

7、Hyperlink:超链接样式按钮

 

 

 

二、简单登录

.aspx页面:

   <title></title>
   <style type="text/css">       //设置button 按钮样式
       #Button1 {
       width:100px;
       height:30px;
       background-color:yellow;
       color:green;
       font-size:18px;
       font-family:黑体;
       font-weight:bold;
       }

   </style>
</head>
<body>
   
    <form id="form1" runat="server" text="xm">
    用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br/>
        密码:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />        //登录的界面
        <asp:Button ID="Button1" runat="server" Text="登录" />
        <asp:Literal ID="Literal1" runat="server"></asp:Literal>
   
    </form>
</body>
</html>

 

 

.cs页面

 

public partial class zhuce : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;//登录按钮  按两次Tab
    }

    void Button1_Click(object sender, EventArgs e)
    {
        //先把用户名和密码取出   连接数据库三个类
        string Uname = TextBox1.Text;
        string Pwd = TextBox2.Text;
        bool isok = new UsersDA().Select(Uname,Pwd);
        if (isok)
        {
            Literal1.Text = "登录成功!";
        }
        else
        {
            Literal1.Text = "用户名密码错误";
        }
    }

 

 

 链接数据库:

App_Code  把所有类放入此文件夹中

没有命名空间

1、实体类

2、数据访问类:

public class UsersDA
{
    SqlConnection conn = null;
    SqlCommand cmd = null;
    public UsersDA()
    {
        conn = new SqlConnection("server=.;database=Data0617;user=sa;pwd=100867");
        cmd = conn.CreateCommand();
    }
    /// <summary>
    /// 用户验证
    /// </summary>
    /// <param name="Uname">验证的用户名</param>
    /// <param name="Pwd">验证的密码</param>
    /// <returns></returns>
    public bool Select(string Uname,string Pwd)

    {
        bool has = false;
        cmd.CommandText = "select * from Users where UserName=@username and PassWord=@password";
        cmd.Parameters.Clear();
        cmd.Parameters.AddWithValue("@username",Uname);
        cmd.Parameters.AddWithValue("@password",Pwd);
        conn.Open();
        SqlDataReader dr= cmd.ExecuteReader();
        if (dr.HasRows)
        {
            has = true;
        }
        
        conn.Close();
        return has;
    }

 

posted on 2016-09-20 15:00  yp秋水伊人  阅读(1066)  评论(0编辑  收藏  举报