Webform(五)——內置對象(Response、Request)和Repeater中的增刪改,webformrepeater
一、內置對象
(一)Response對象
1、簡介:response 對象在ASP中負責將信息傳遞給用戶.Response對象用於動態響應客戶端請求,並將動態生成的響應結果返回到客戶端浏覽器中,使用Response對象可以直接發送信息給浏覽器,重定向浏覽器到另一個URL或設置cookie的值等.
2、方法:①、write方法:response.write **
功能:向客戶端發送浏覽器能夠處理的各種數據,包括:html代碼,腳本程序等.
實例:response.write "I LOVE YOU !!"
②、redirect方法:
response.redirect("url")的作用是在服務器端重定向於另一個網頁。
(二)Request對象
1、簡介:Request對象的作用是與客戶端交互,收集客戶端的Form、Cookies、超鏈接,或者收集服務器端的環境變量。
request對象是從客戶端向服務器發出請求,包括用戶提交的信息以及客戶端的一些信息。客戶端可通過HTML表單或在網頁地址後面提供參數的方法提交數據,然後通過request對象的相關方法來獲取這些數據。request的各種方法主要用來處理客戶端浏覽器提交的請求中的各項參數和選項。
2、Request對象的五個集合:①、QueryString:用以獲取客戶端附在url地址後的查詢字符串中的信息。
例如:stra=Request.QueryString ["strUserld"]
前台傳遞寫法:地址 ?key=value&key=value
注意事項:●不需要保密的東西可以傳,在地址欄中是可見的,可更改的。 ●不要傳過長東西,因為長度有限,過長會造成數據丟失。
②、Form:用以獲取客戶端在FORM表單中所輸入的信息。(表單的method屬性值需要為POST)
例如:stra=Request.Form["strUserld"]
③、Cookies:用以獲取客戶端的Cookie信息。
例如:stra=Request.Cookies["strUserld"]
④、ServerVariables:用以獲取客戶端發出的HTTP請求信息中的頭信息及服務器端環境變量信息。
例如:stra=Request.ServerVariables["REMOTE_ADDR"],返回客戶端IP地址
⑤、ClientCertificate:用以獲取客戶端的身份驗證信息
例如:stra=Request.ClientCertificate["VALIDFORM"],對於要求安全驗證的網站,返回有效起始日期。
二、利用Response對象和Request對象對Reparter中數據進行增刪改
主頁前台代碼:

![]()
</style>
<%--光棒效果--%>
<script type="text/javascript">
window.onload = function () {
var items = document.getElementsByClassName("tr_Item");
var oldColor = "";
for (var i = 0; i < items.length; i++) {
items[i].onmouseover = function () {
oldColor = this.style.backgroundColor;
this.style.backgroundColor = "yellow";
};
items[i].onmouseout = function () {
this.style.backgroundColor = oldColor;
};
}
};
</script>
</head>
<body>
<form id="form1" runat="server">
<div >
<a href ="Login.aspx"><asp:Label ID="Labdl" runat="server" Text="[請登錄]"></asp:Label></a>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
<asp:Button ID="Btntc" runat="server" Text="退出登陸" />
</div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table id="tb1">
<tr id="tr_head">
<td>用戶名</td>
<td>密碼</td>
<td>昵稱</td>
<td>性別</td>
<td>生日</td>
<td>年齡</td>
<td>民族</td>
<td>操作</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="tr_Item" Red")%>">
<td><%#Eval("UserName") %></td>
<td><%#Eval("PassWord") %></td>
<td><%#Eval("NickName") %></td>
<td><%#Eval("SexStr") %></td>
<td><%#Eval("BirthdayStr") %></td>
<td><%#Eval("Age") %></td>
<td><%#Eval("NationName") %></td>
<td> <a href="Delete.aspx?un=<%#Eval("UserName") %>" onclick="Del" >刪除</a>
<a href="Update.aspx?un=<%#Eval("UserName") %>" target="_blank" onclick="Update">修改</a>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Button ID="btn1" runat="server" Text="添加用戶" />
<%--<input id="btn1" type="button" value="添加用戶" /><br />--%>
<%--<script>
document.getElementById("btn1").onclick = function () {
window.open("Add.aspx", "_blank");
};
</script>--%>
</form>
</body>
</html>
View Code
主頁後台代碼:

![]()
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 Code
Update後台代碼:

![]()
//建一個變量來存儲原密碼
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>");
}
修改