还是老师惹的祸~~~
设计和实现一个简单的网上注册系统和投票系统。要求注册页面中用户可以输入用户名、密码、性别、年龄、等信息;登录成功后利用application对象实现优秀班干部评选的功能。
新建web应用程序:
右键单击解决方案添加新项 web窗体:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; namespace _8_1 { public partial class login : System.Web.UI.Page { string str = @"Data Source=localhost\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"; protected void Button2_Click(object sender, EventArgs e) { //跳转页面 Response.Redirect("register.aspx"); } protected void Button1_Click(object sender, EventArgs e) { try { SqlConnection conn = new SqlConnection(str); SqlCommand com = new SqlCommand(); com.Connection = conn; com.CommandText = string.Format("select * from users where name='{0}' and password='{1}'", TextBox1.Text, TextBox2.Text); conn.Open(); int n = com.ExecuteNonQuery(); if (n > 0) { MsgBox("OK", "login.aspx"); conn.Close(); Response.Redirect("vote.aspx"); } else { MsgBox("不存在记录", "login.aspx"); conn.Close(); //Response.Redirect("vote.aspx"); } } catch (Exception ex) { MsgBox(ex.ToString(), "login.aspx"); } } //消息提示函数 public void MsgBox(string strMsg, string URL) { System.Web.HttpContext.Current.Response.Write(strMsg); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; namespace _8_1 { public partial class register : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } string str = @"Data Source=localhost\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"; protected void Button1_Click(object sender, EventArgs e) { try{ SqlConnection conn = new SqlConnection(str); SqlCommand com = new SqlCommand(); com.Connection = conn; com.CommandText = string.Format("insert into users (name,password,sex,age)values('{0}','{1}','{2}',{3})", TextBox1.Text, TextBox2.Text, TextBox3.Text, int.Parse(TextBox4.Text)); conn.Open(); int n = com.ExecuteNonQuery(); if (n > 0) { MsgBox("注册成功。", "register.aspx"); } conn.Close(); Response.Redirect("login.aspx"); //暂时不知道怎么设置跳转时间 } catch(Exception ex){ MsgBox(ex.ToString(), "register.aspx"); } } //构造web提示框 public void MsgBox(string strMsg, string URL) { System.Web.HttpContext.Current.Response.Write(strMsg); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace _8_1 { public partial class vote : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { //使用application int n=(int)Application["Elena"]; Application["Elena"] = n + 1; Button1.Text = Application["Elena"].ToString(); } protected void Button2_Click(object sender, EventArgs e) { int n = (int)Application["Demon"]; Application["Demon"] = n + 1; Button2.Text = Application["Demon"].ToString(); } protected void Button3_Click(object sender, EventArgs e) { int n = (int)Application["Stephen"]; Application["Stephen"] = n + 1; Button3.Text = Application["Stephen"].ToString(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; using System.Web.SessionState; namespace _8_1 { public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { Application["Elena"] = 0; Application["Demon"] = 0; Application["Stephen"] = 0; } protected void Session_Start(object sender, EventArgs e) { } protected void Application_BeginRequest(object sender, EventArgs e) { } protected void Application_AuthenticateRequest(object sender, EventArgs e) { } protected void Application_Error(object sender, EventArgs e) { } protected void Session_End(object sender, EventArgs e) { } protected void Application_End(object sender, EventArgs e) { } } }
注册页面:
投票界面: