標籤:
一、內建對象
(一)Response - 響應請求對象
1.定義:Response對象用於動態響應用戶端請示,控制發送給使用者的資訊,並將動態產生響應。Response對象只提供了一個資料集合cookie,它用於在用戶端寫入cookie值。若指定的cookie不存在,則建立它。若存在,則將自動進行更新。結果返回給用戶端瀏覽器。
2.方法
(1)Redirect
用法:Response.Redirect("url")
作用是在伺服器端重新導向於另一個網頁
(2)Write
功能:向用戶端發送瀏覽器能夠處理的各種資料,包括:html代碼,指令碼程式等
用法:Response.Write("")輸入字串在介面裡顯示,輸入<script>alert(‘‘)</script>彈出對話方塊
(二)Request - 擷取請求對象
Request["key"] - 預設返回的就是string類型
來擷取傳遞過來的值,Request對象的作用是與用戶端互動,收集用戶端的Form、Cookies、超連結,或者收集伺服器端的環境變數。
二、跨頁面傳值
QueryString - 地址欄資料拼接
在地址欄中拼接?key=value key為任一字元,value為要傳的值,?與=之間不能有空格
多條資料傳值 在地址欄繼續拼接&key=value
void Button1_Click(object sender, EventArgs e)
{
string path = "Default2.aspx?aaa=" + TextBox1.Text + "&bbb=" + TextBox2.Text;
Response.Redirect(path);//或使用Server.Transfer(path);
}
接收值得介面:
Label1.Text = Request["aaa"];
Label1.Text += "," + Request["bbb"];
三、Repeater刪改
在ItemTemplate中再添加一列,放修改與刪除的超連結
<td> <a id="lian" onclick="return del()" href="Delete.aspx?id=<%#Eval("Code") %>">刪除</a> <a target="_blank" href="Update.aspx?up=<%#Eval("Code") %>">修改</a> </td>
(一)刪除
1.寫好刪除方法,首頁面為Default.aspx ,刪除頁為Delete.aspx
2.添加js指令碼,判斷是否要刪除
<script> function del() { var j = document.getElementById("lian"); var ok = confirm("是否確定要刪除?"); if (ok == false) { return false; } return true; }; </script>
3.在Delete的Page_Load中執行刪除命令,並重新整理頁面
//將要刪除的主索引值取出來 string Code = Request["id"]; //將主索引值傳到方法裡進行刪除 bool b= new StudentData().delete(Code); //刪除完畢後跳轉會顯示頁面 if(b) { Response.Redirect("Default.aspx"); }
(二)修改
1.構造修改介面,並進行驗證及資料指向
2.將原資料展現到頁面中
3.執行修改操作,修改完後關閉修改頁面,並重新整理主介面
protected void Button1_Click(object sender, EventArgs e){ Student s = new Student(); s.Code = Xuehao_Text.Text; s.Name = Name_Text.Text; s.Sex =Convert.ToBoolean( RadioButtonList1.SelectedValue); string time = Year_Dr.SelectedValue + "-" + Mouth_dr.SelectedValue + "-" + Day_dr.SelectedValue; s.Birthday = Convert.ToDateTime(time); s.SubjectCode = Subject_DW.SelectedValue; s.Nation = Nation_Dw.SelectedValue; bool b = new StudentData().update(s); if (b) { Response.Write("<script>"); Response.Write("alert(‘修改成功!‘);"); Response.Write("window.opener.location.href=‘Default.aspx‘;"); Response.Write("window.close();"); Response.Write("</script>"); }}
Webform(內建對象-Response與Redirect、QueryString傳值、Repeater刪改)