(一)内置对象
1.Request --- 获取请求对象
获取通过地址栏传值过来的对象
string aaa = Request["key"]; key必须和传值的对象名称相同
2.Response --- 响应请求对象
Response.Redirect("新打开的网址?aa=key");/ Response.Redirect("新打开的网址?aa="+key)
地址栏传值/URL传值/QueryString传值优势:不占用服务器内存,保密性查,传递的长度有限
?aa=key 要传递的对象 只能在原有页面上进行刷新,无法打开新的选项卡
Response.Write("string"); --- 将字符串输出到页面的最顶部,string可以书写js代码,打开新的网址
3.Session
优势:1.存贮在服务端,占用服务器内存2.传输速度快3.不要滥用,容易溢出
生命周期:20分钟,每一次的请求都会刷新这个时间,如果浏览器关闭,就会断开此次Session的链接
Session可以存贮Object类型
Session页面传值:
string aa = TextBox1.Text; Session["zz"] = aa; Response.Redirect("bb.aspx");
接收的页面:
Label1.Text = Session["zz"].ToString();
传递实例化的对象:
protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; } void Button1_Click(object sender, EventArgs e) { Users u = new Users(); u.username = "小明"; u.sex = "男"; Session["zz"] = u; Response.Redirect("bb.aspx"); }
接收的页面:
Users u = new Users(); u = Session["zz"] as Users; Label1.Text = u.username + u.sex;
4.Cookie
cookie和session一模一样,不一样的地方是:Cookies是存储在客户端的
两者的优缺点:
Session很安全,但是消耗服务器内存,Cookies,不消耗服务器内存,存在客户端上,但是有可能被访问
cookie判断为不为空:
if(Request.Cookies["aa"]!=null){} --- 直接判断接收的值是不是为空
设置持久cookie:
String u = TextBox1.Text;
Response.Cookies["aa"].Value =u;
Response.Cookies["aa"].Expires = DateTime.Now.AddDays(3);---- 设置cooki持续的时间3天,不同的浏览器都有自己的cooki,与保存的时间无关,只是在当前浏览器下保存
页面传值:
protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; } void Button1_Click(object sender, EventArgs e) { string u = TextBox1.Text; Response.Cookies["zz"].Value = u; Response.Redirect("bb.aspx"); }
接收的页面:
protected void Page_Load(object sender, EventArgs e) { string u = Request.Cookies["zz"].Value; Label1.Text = u; }
(二)时间年月日的三级联动
页面代码:
<asp:DropDownList Style="font-size: 15px; 70px;" ID="DropDownList1" runat="server"></asp:DropDownList>年 <asp:DropDownList Style="font-size: 15px; 70px;" ID="DropDownList2" runat="server"></asp:DropDownList>月 <asp:DropDownList Style="font-size: 15px; 70px;" ID="DropDownList3" runat="server"></asp:DropDownList>日
js代码:
//年的改变事件 document.getElementById("DropDownList1").onchange = function () { year(); } //判断选择年是否位闰月 function year() { var year1 = document.getElementById("DropDownList1"); var mon = document.getElementById("DropDownList2"); var day = document.getElementById("DropDownList3"); //2月的判断 if (mon.value == "2") { if (year1.value % 100 == 0 && year1.value % 4 == 0 || year1.value % 400 == 0) { day.options.length = null; for (var i = 1; i < 30; i++) { var op = document.createElement("option"); op.value = i; op.innerText = i; day.appendChild(op); } } else { day.options.length = null; for (var i = 1; i < 29; i++) { var op = document.createElement("option"); op.value = i; op.innerText = i; day.appendChild(op); } } } } //月的改变事件 document.getElementById("DropDownList2").onchange = function () { var year1 = document.getElementById("DropDownList1"); var mon = document.getElementById("DropDownList2"); var day = document.getElementById("DropDownList3"); if (mon.value == "1" || mon.value == "3" || mon.value == "5" || mon.value == "7" || mon.value == "9" || mon.value == "10" || mon.value == "12") { day.options.length = null; for (var i = 1; i < 32; i++) { var op = document.createElement("option"); op.value = i; op.innerText = i; day.appendChild(op); } } else if (mon.value == "4" || mon.value == "6" || mon.value == "8" || mon.value == "11") { day.options.length = null; for (var i = 1; i < 31; i++) { var op = document.createElement("option"); op.value = i; op.innerText = i; day.appendChild(op); } } else if(mon.value=="2") { year(); } }