<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><asp:Button
<a href="javascript:TestPostValue('test')">Test Post By HiddenField</a><br/>
<asp:HiddenField ID="hidden" runat="server" />
...
<script type="text/javascript"> function TestPostValue(value) { document.getElementById("hidden").value=value; document.getElementById("Button1").click(); } </script>
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(hidden.Value);
}
二、利用Cookies
Cookie的用法也和ASP中差不多。
比如我们建立一个名为aspcn,值为飞刀的cookie HttpCookie cookie = new HttpCookie["aspcn"]; cookie.Value = "飞刀"; Response.AppendCookie(cookie); 我们取出Cookie值也很简单 HttpCookie cookie = Request.Cookies["aspcn"]; cookieValue = cookie.Value; 有时候我们想在一个Cookie中储存多个信息,那也没有问题。比如我们在名为aspcn的cookie下加多个信息 HttpCookie cookie = new HttpCookie("aspcn"); cookie.Values.Add("webmaster","飞刀"); cookie.Values.Add("writer","beige"); cookie.Values.Add("LinkColor","blue"); Response.AppendCookie(cookie); 取出信息也一样简单 HttpCookie cookie = Request.Cookies["aspcn"]; value1 = cookies.Values["webmaster"]; value2 = cookies.Values["writer"];
View State
这是一个新出来的东东,用法和Session一样,他的主要用途是记录Web Control的状态。虽然是新出来的,但是和Application、Session的用法没有什么区别,所以也不想详细讲解了。 State["DropLoadIndex"] = 0 ; 基本用法如上:),但是请记住,他保存在的信息只能在一个aspx文件中使用。出去后,这个就没有用了,因为他的用途只是保存WEB控件的状态。
我的实现代码:
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
<a href="javascript:TestPostByCookies('Test')">Test Post By Cookies</a>
function TestPostByCookies(value) { SetCookie("myCookie",value); document.getElementById("Button2").click(); } //写cookies函数 作者:翟振凯 function SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值 { var Days = 30; //此 cookie 将被保存 30 天 var exp = new Date(); //new Date("December 31, 9998"); exp.setTime(exp.getTime() + Days*24*60*60*1000); document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); } function getCookie(name)//取cookies函数 { var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)")); if(arr != null) return unescape(arr[2]); return null; } function delCookie(name)//删除cookie { var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval=getCookie(name); if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString(); }
protected void Button2_Click(object sender, EventArgs e)
{
HttpCookie cook = Request.Cookies["myCookie"];
Response.Write(cook.Value);
}