如何利用Visual studio 2010创建一个ASP网站?
【文件】-->【新建】-->【网站】-->【ASP.Net网站】--【完成】
默认页面Default.aspx
创建以上界面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserLogin.aspx.cs" Inherits="UserLogin" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <div> <h1>用户登录页面</h1> <form runat="server"> <p> <asp:Label runat="server">用户名:</asp:Label> <asp:TextBox runat="server"></asp:TextBox> </p> <p> <asp:Label runat="server">密 码:</asp:Label> <asp:TextBox runat="server" TextMode="Password"></asp:TextBox> </p> <p><asp:Button runat="server" Text="登录" /> <asp:Button runat="server" Text="取消" /> </p> </form> </div> </body> </html>
登录验证功能
/// <summary> /// 用户登录 /// </summary> /// <param></param> /// <param></param> protected void btnLogin_Click(object sender, EventArgs e) { if (tbusername.Text == "") { Response.Write(@"<script>alert('用户名不能为空!');</script>"); } if (tbpsw.Text == "") { Response.Write(@"<script>alert('密码不能为空!');</script>"); } string username = tbusername.Text; string password = tbpsw.Text; string sql = "select * from T_user where username=@username and password=@password"; SqlParameter[] parameters = { new SqlParameter("@username",username), new SqlParameter("@password", password) }; using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameters); DataSet ds = new DataSet(); SqlDataAdapter adapter = new SqlDataAdapter(cmd); adapter.Fill(ds); DataTable table = ds.Tables[0]; if(table != null) Response.Write(@"<script>alert('登录成功!');</script>"); else Response.Write(@"<script>alert('登录失败!');</script>"); } } }
注册功能
/// <summary> /// 用户注册 /// </summary> /// <param></param> /// <param></param> protected void brnRegister_Click(object sender, EventArgs e) { string username = tbusername.Text; string password = tbpsw.Text; string sql = "Insert into T_user(username, password) values(@username,@password)"; SqlParameter[] parameters = { new SqlParameter("@username", username), new SqlParameter("@password", password) }; using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameters); cmd.ExecuteNonQuery(); } } Response.Write(@"<script>alert('注册成功!');</script>"); }
知识点归纳:
1.如何创建一个ASP.Net网站
2. 使用服务器控件实现界面设计
3. 对数据库的简单操作
一个简单的用户注册、登录就完成了,通过这个案例希望对大家的学习有所帮助。
您可能感兴趣的文章: