一、內置對象
(一)Response對象
1、簡介:response 對象在ASP中負責將信息傳遞給用戶.Response對象用於動態響應客戶端請求,並將動態生成的響應結果返回到客戶端浏覽器中,使用Response對象可以直接發送信息給浏覽器,重定向浏覽器到另一個URL或設置cookie的值等.
2、方法:①、write方法:response.write **
功能:向客戶端發送浏覽器能夠處理的各種數據,包括:html代碼,腳本程序等. 實例:response.write "I LOVE YOU !!" ②、redirect方法:response.redirect("url")的作用是在服務器端重定向於另一個網頁。主頁後台代碼:
protected void Page_Load(object sender, EventArgs e) { if (Request.Cookies["user"] != null) { Users u = new UsersDA().Select(Request.Cookies["user"].Value); Labdl.Text = u.NickName; Literal1.Text = ",歡迎你!"; } if (!IsPostBack) { Repeater1.DataSource = new UsersDA().Select(); Repeater1.DataBind(); } Btntc.Click += Btntc_Click; btn1.Click += btn1_Click; } void btn1_Click(object sender, EventArgs e) { if (Request.Cookies["user"] != null) { Response.Redirect("Add.aspx"); } else { Response.Redirect("Login.aspx"); } } void Btntc_Click(object sender, EventArgs e) { //1清除cookies Response.Cookies["user"].Expires = DateTime.Now.AddDays(-5); //2刷新頁面/跳到登陸頁面 Response.Redirect("Login.aspx"); } public void Del(object sender, EventArgs e) { if (Request.Cookies["user"] != null) { Response.Redirect("Delete.aspx"); } else { Response.Redirect("Login.aspx"); } } public void Update(object sender, EventArgs e) { if (Request.Cookies["user"] != null) { Response.Redirect("Update.aspx"); } else { Response.Redirect("Login.aspx"); } } 主頁後台點擊主頁“”增加用戶“按鈕”,跳轉到Add(添加)頁面。
(一)增加
Add頁面前台代碼:
<title></title> <%--判斷兩次密碼是否一致--%> <script type="text/javascript"> window.onload = function () { document.getElementById("Button1").onclick = function () { var pwd1 = document.getElementById("TextBox2").value; var pwd2 = document.getElementById("TextBox3").value; if (pwd1 != pwd2) { document.getElementById("Label1").innerText = "兩次密碼不一致!"; return false; } }; }; </script> <style type="text/css"> #Label1 { color: red; } </style> </head> <body> <form id="form1" runat="server"> <h1>用戶添加</h1> 用戶名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> <br /> 密碼:<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox><br /> <br /> 重復密碼:<asp:TextBox ID="TextBox3" runat="server" TextMode="Password"></asp:TextBox><asp:Label ID="Label1" runat="server" Text=""></asp:Label><br /> <br /> 昵稱:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br /> <br /> 性別:<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow"> <asp:ListItem Value="True" Selected="True">男</asp:ListItem> <asp:ListItem Value="False">女</asp:ListItem> </asp:RadioButtonList><br /> <br /> 生日:<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>年 <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>月 <asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList>日 <br /> <br /> 民族:<asp:DropDownList ID="DropDownList4" runat="server"></asp:DropDownList><br /> <br /> <asp:Button ID="Button1" runat="server" Text="添加" /> </form> </body> </html> Add前台Add頁面後台代碼:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { for (int i = DateTime.Now.Year; i >= 1900; i--) { ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList1.Items.Add(li); } for (int i = 1; i <= 12; i++) { ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList2.Items.Add(li); } for (int i = 1; i <= 31; i++) { ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList3.Items.Add(li); } DropDownList4.DataSource = new NationData().Select(); DropDownList4.DataTextField = "NationName"; DropDownList4.DataValueField = "NationCode"; DropDownList4.DataBind(); } Button1.Click += Button1_Click; } void Button1_Click(object sender, EventArgs e) { Users u = new Users(); u.UserName = TextBox1.Text; u.PassWord = TextBox3.Text; u.NickName = TextBox4.Text; u.Sex = Convert.ToBoolean(RadioButtonList1.SelectedItem.Value); string date = DropDownList1.SelectedValue + "-" + DropDownList2.SelectedValue + "-" + DropDownList3.SelectedValue; u.Birthday = Convert.ToDateTime(date); u.Nation = DropDownList4.SelectedItem.Value; bool ok = new UsersDA().Insert(u); //3、提示添加成功 if (ok) { Response.Write("<script>alert('添加成功!')</script>"); Response.Write("<script>this.opener.location.href='Main.aspx';this.close();</script>"); } else { Response.Write("<script>alert('添加失敗!')</script>"); } } Add後台注:在Add頁面中點擊“”“添加”按鈕,會提示添加是否成功,若添加成功,關閉Add頁面,刷新Main主頁面。在這裡用到了Response對象中的write方法!
(二)、刪除
Delete頁面前台代碼:無
Delete頁面後台代碼:
protected void Page_Load(object sender, EventArgs e) { //1、獲得要刪除的主鍵值,username string Uname = Request["un"].ToString(); //2、刪除 new UsersDA().Delete(Uname); //3、調回顯示頁面 Response.Redirect("Main.aspx"); } Delete後台注:在這裡用到了Response對象中的Redirect方法和Request對象中的QueryString集合,當然,在用QueryString集合時,需要Main主頁裡面寫傳遞,這個可以見上面的Main主頁前台代碼。
(三)、修改
Update前台代碼:
<form id="form1" runat="server"> <h1>用戶修改</h1> 用戶名:<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br /> <br /> 密碼:<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox><br /> <br /> 重復密碼:<asp:TextBox ID="TextBox3" runat="server" TextMode="Password"></asp:TextBox><asp:Label ID="Label1" runat="server" Text=""></asp:Label><br /> <br /> 昵稱:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br /> <br /> 性別:<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow"> <asp:ListItem Value="True">男</asp:ListItem> <asp:ListItem Value="False">女</asp:ListItem> </asp:RadioButtonList><br /> <br /> 生日:<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>年 <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>月 <asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList>日 <br /> <br /> 民族:<asp:DropDownList ID="DropDownList4" runat="server"></asp:DropDownList><br /> <br /> <asp:Button ID="Button1" runat="server" Text="修改" /> </form> View CodeUpdate後台代碼:
//建一個變量來存儲原密碼 string pwd = ""; protected void Page_Load(object sender, EventArgs e) { //1、將傳過來的主鍵值接收 string uname = Request["un"].ToString(); //2、通過主鍵值將對象查出來 Users u = new UsersDA().Select(uname); pwd = u.PassWord; if (!IsPostBack) { for (int i = DateTime.Now.Year; i >= 1900; i--) { ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList1.Items.Add(li); } for (int i = 1; i <= 12; i++) { ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList2.Items.Add(li); } for (int i = 1; i <= 31; i++) { ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList3.Items.Add(li); } DropDownList4.DataSource = new NationData().Select(); DropDownList4.DataTextField = "NationName"; DropDownList4.DataValueField = "NationCode"; DropDownList4.DataBind(); //3、將對象中的數據綁定到每一個控件上去 Label2.Text = u.UserName; TextBox4.Text = u.NickName; foreach (ListItem li in RadioButtonList1.Items) { if (u.Sex) { if (li.Value == "True") { li.Selected = true; } } else { if (li.Value == "False") { li.Selected = true; } } } DropDownList1.SelectedValue = u.Birthday.Year.ToString(); DropDownList2.SelectedValue = u.Birthday.Month.ToString(); DropDownList3.SelectedValue = u.Birthday.Day.ToString(); DropDownList4.SelectedValue = u.Nation; } Button1.Click += Button1_Click; } void Button1_Click(object sender, EventArgs e) { //1、構建一個Users對象 Users u = new Users(); u.UserName = Label2.Text; //獲取密碼 if (TextBox2.Text == "" && TextBox3.Text == "") { u.PassWord = pwd; } else { u.PassWord = TextBox3.Text; } u.NickName = TextBox4.Text; u.Sex = Convert.ToBoolean(RadioButtonList1.SelectedItem.Value); string date = DropDownList1.SelectedValue + "-" + DropDownList2.SelectedValue + "-" + DropDownList3.SelectedValue; u.Birthday = Convert.ToDateTime(date); u.Nation = DropDownList4.SelectedItem.Value; //2、將此對象添加到數據庫去 bool ok = new UsersDA().Update(u); //3、提示添加成功 if (ok) { Response.Write("<script>alert('修改成功!')</script>"); Response.Write("<script>this.opener.location.href='Main.aspx';this.close();</script>"); } else { Response.Write("<script>alert('修改失敗!')</script>"); } View Code 注:●在這裡用到了Response對象中的write方法和Request對象中的QueryString集合,當然,在用QueryString集合時,需要Main主頁裡面寫傳遞,這個可以見上面的Main主頁前台代碼。
●點擊“”修改“”按鈕,彈提示框,若添加成功,關閉Update頁面,刷新Main主頁面。
後注:部分重要代碼
1、js代碼判斷兩次密碼是否一致
<script type="text/javascript"> window.onload = function () { document.getElementById("Button1").onclick = function () { var pwd1 = document.getElementById("TextBox2").value; var pwd2 = document.getElementById("TextBox3").value; if (pwd1 != pwd2) { document.getElementById("Label1").innerText = "兩次密碼不一致!"; return false; } }; }; </script> js密碼2、彈框顯示添加/修改是否成功,成功則刷新主頁面reparter數據
※this.opener.location.href='Main.aspx':跨界面刷新主頁面
if (ok) { Response.Write("<script>alert('添加成功!')</script>"); Response.Write("<script>this.opener.location.href='Main.aspx';this.close();</script>"); } else { Response.Write("<script>alert('添加失敗!')</script>"); } 添加 if (ok) { Response.Write("<script>alert('修改成功!')</script>"); Response.Write("<script>this.opener.location.href='Main.aspx';this.close();</script>"); } else { Response.Write("<script>alert('修改失敗!')</script>"); } 修改