=============================Cookie=========================================
用这个类吧HttpCookie 。
HttpCookie c = new HttpCookie("userid", "admin");
HttpContext.Current.Response.Cookies.Add(c);
string userid = HttpContext.Current.Request.Cookies.Get("userid").Value.ToString();
=========================Session=========================================
一、定义Session:
Session["ZTID"] = DropDownList1.SelectedValue.ToString();
二、获取Session:(都在同一个解决方案下)
1、同页面:string ztid = Session["ZTID"]
2、同项目下:this.Context.Session["ZTID"].ToString();
3、不能项目下:HttpContext.Current.Session["ZTID"].ToString()
三、如果想在ashx页面获取Session值:
1、引入 命名空间:
using System.Web.SessionState;
2、实现IRequiresSessionState接口,具体如下
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class AddUserInfo : IHttpHandler,IRequiresSessionState//就是这样显示的实现一下,不用实现什么方法
{
public void ProcessRequest(HttpContext context)
{
//...
//这样你就可以如下 操作了
if(context.Session["userAccount"] != null)
{
string account = context.Session["userAccount"].ToString();
}
//...继续下面的代码
}
}