Response.Cookies.Add()和方法Response.AppendCookie()有什么区别?

落叶知秋秋 2008-09-20 09:05:30
Response.Cookies.Add()和方法Response.AppendCookie()有什么区别?
...全文
487 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
limfungsuen 2011-03-09
  • 打赏
  • 举报
回复
Add 方法
创建新对象并将其添加到 Cookies 集合。

AppendCookie
此方法支持 .NET 基础结构,但不适合在代码中直接使用。
将一个 HTTP Cookie 添加到内部 Cookie 集合。

没什么大的区别,不过add的有更多的重载方法如Cookies.Add(strPath, strName, strValue, [bPersist])

网上找的,希望能帮到你
cdxrandy 2009-03-17
  • 打赏
  • 举报
回复
期待中
zoroz 2008-09-20
  • 打赏
  • 举报
回复
帮顶
特别说明请注意: 根许多网友反应, using Maticsoft.Functions; 这些代码看不懂 其实Functions 这个dll是我定义常用的函数类,如果需要跟我联系索取http://sql8.net 下面有我的群号, 其中 ArtsShop.Model.Arts_Product _p = new ArtsShop.Model.Arts_Product(); ArtsShop.BLL.Arts_Product p = new ArtsShop.BLL.Arts_Product(); _p = p.GetModel(id); 这是我的商品信息的类,三层结构,这个在用时你们只能换成你们自己的,这些代码完全可以删除, 比如 MyDr[1] = _p.Title; 用来读取商品名的,你们可以改MyDr[1] = dr["productname"].ToString();就行了, AddToCart.aspx页面代码 无标题页 保存 取消 编辑 继续购物 清空购物车 下订单 AddToCart.aspx.cs页面代码 using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using Maticsoft.Functions;public partial class AddToCart : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { int ProID; HttpCookie cookie; bool Tempbl = false; string Tempstr; if (!Page.IsPostBack) { if (!object.Equals(Request.QueryString["id"], null)) { ProID = int.Parse(Request.QueryString["id"]); //创购物车cookie yxy .//sql8.net if (object.Equals(Request.Cookies["ztbscart"], null)) cookie = new HttpCookie("ztbscart"); else cookie = Request.Cookies["ztbscart"]; //判断是否已存在于购物车内 yxy // sql8.net for (int i = 0; i < cookie.Values.Keys.Count; i++) { if (!object.Equals(cookie.Values.Keys[i], null)) { Tempstr = cookie.Values.AllKeys[i].ToString(); if (Tempstr.Trim() != "") { if (ProID == int.Parse(cookie.Values.AllKeys[i])) { Tempbl = true; break; } } } } //不未购买过则加入购物车 yxy //sql8.net if (!Tempbl) cookie.Values.Add(ProID.ToString(), "1"); else { } TimeSpan ts = new TimeSpan(0, 0, 10, 0); cookie.Expires = DateTime.Now + ts; Response.AppendCookie(cookie); } BindGrid(); } } //绑定数据 yxy //sql8.net private void BindGrid() { DataTable MyDt; DataRow MyDr; string str = ""; MyDt = new DataTable(); MyDt.Columns.Add(new DataColumn("id", str.GetType())); MyDt.Columns.Add(new DataColumn("Title", str.GetType())); MyDt.Columns.Add(new DataColumn("Num", str.GetType())); MyDt.Columns.Add(new DataColumn("Price", str.GetType())); MyDt.Columns.Add(new DataColumn("Discount", str.GetType())); MyDt.Columns.Add(new DataColumn("Vipprice", str.GetType())); MyDt.Columns.Add(new DataColumn("Totle", str.GetType())); if (!object.Equals(Request.Cookies["ztbscart"], null)) { HttpCookie cookie = Request.Cookies["ztbscart"]; double Totle; //Response.Write("|" + Request.Cookies["ztbscart"].Values.Keys[1].ToString() + "|"); //Response.End(); for (int i = 0; i < cookie.Values.Keys.Count; i++) { int id; MyDr = MyDt.NewRow(); if (cookie.Values.AllKeys[i] != "" && cookie.Values[i] != "") { id = int.Parse(cookie.Values.AllKeys[i].ToString()); ArtsShop.Model.Arts_Product _p = new ArtsShop.Model.Arts_Product(); ArtsShop.BLL.Arts_Product p = new ArtsShop.BLL.Arts_Product(); _p = p.GetModel(id); MyDr[0] = id; MyDr[1] = _p.Title; MyDr[2] = cookie.Values[i]; MyDr[3] = _p.Price; MyDr[4] = _p.Discount; MyDr[5] = _p.Vipprice1; Totle = double.Parse(MyDr[2].ToString()) * double.Parse(MyDr[5].ToString()); MyDr[6] = Totle; MyDt.Rows.Add(MyDr); } } GridView1.DataSource = MyDt.DefaultView; GridView1.DataBind(); } } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { //编辑某行数量 yxy //sql8.net GridView1.EditIndex = e.NewEditIndex; BindGrid(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { //取消更新 yxy //sql8.net GridView1.EditIndex = -1; BindGrid(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { //更新数量 yxy //sql8.net string num; TextBox tempbx = new TextBox(); Label templb = new Label(); tempbx = (TextBox)(GridView1.Rows[e.RowIndex].Cells[6]).Controls[1]; num = tempbx.Text.ToString(); HttpCookie cookie = new HttpCookie("ztbscart"); for (int i = 0; i < GridView1.Rows.Count; i++) { string id; string tempnum; id = GridView1.Rows[i].Cells[1].Text.ToString(); if (e.RowIndex == i) tempnum = num; else { templb = (Label)(GridView1.Rows[i].Cells[6]).Controls[1]; tempnum = templb.Text.ToString(); } if (tempnum.Trim() == "") tempnum = "0"; //Response.Write("ID:"+id.ToString() + "Num:"+tempnum+":"+i+"");//测试用途 yxy//sql8.net cookie.Values.Add(id, tempnum); } //Response.End(); TimeSpan ts = new TimeSpan(0, 0, 10, 0); cookie.Expires = DateTime.Now + ts; Response.AppendCookie(cookie); GridView1.EditIndex = -1; Message.GoTo("AddToCart.aspx"); } protected void LinkButton4_Click(object sender, EventArgs e) { //继续购物 yxy //sql8.net Message.WebClose(); } protected void LinkButton3_Click(object sender, EventArgs e) { //清空购物车 yxy //sql8.net CheckBox tempcb = new CheckBox(); HttpCookie cookie = new HttpCookie("ztbscart"); Label templb = new Label(); for (int i = 0; i < GridView1.Rows.Count; i++) { tempcb = (CheckBox)(GridView1.Rows[i].Cells[0]).Controls[1]; if (!tempcb.Checked) { string id; string tempnum; id = GridView1.Rows[i].Cells[1].Text.ToString(); templb = (Label)(GridView1.Rows[i].Cells[6]).Controls[1]; tempnum = templb.Text.ToString(); if (tempnum.Trim() == "") tempnum = "0"; //Response.Write("ID:"+id.ToString() + "Num:"+tempnum+":"+i+"");//测试用途 yxy//sql8.net cookie.Values.Add(id, tempnum); } } TimeSpan ts = new TimeSpan(0, 0, 10, 0); cookie.Expires = DateTime.Now + ts; Response.AppendCookie(cookie); Message.GoTo("AddToCart.aspx"); } protected void CheckAll_CheckedChanged(object sender, EventArgs e) { //全选事件 yxy //sql8.net CheckBox tempcb = new CheckBox(); bool tempbl; tempcb = (CheckBox)(GridView1.HeaderRow.Cells[0]).Controls[1]; tempbl = tempcb.Checked; for (int i = 0; i < GridView1.Rows.Count; i++) { tempcb = (CheckBox)(GridView1.Rows[i].Cells[0]).Controls[1]; tempcb.Checked = tempbl; } } } _________________________________________________________________________ 如转载请注明原出处 www.sql8.net
namespace ioffice { using iOfficeData; using iofficeservice; using Microsoft.VisualBasic; using Microsoft.VisualBasic.CompilerServices; using System; using System.Diagnostics; using System.Resources; using System.Runtime.CompilerServices; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; public partial class login2 : Page { [AccessedThroughProperty("chpwd")] private ImageButton _chpwd; [AccessedThroughProperty("dropBranch")] private DropDownList _dropBranch; [AccessedThroughProperty("frm")] private HtmlForm _frm; [AccessedThroughProperty("help")] private HtmlImage _help; [AccessedThroughProperty("imgLogo")] private HtmlImage _imgLogo; [AccessedThroughProperty("Label1")] private Label _Label1; [AccessedThroughProperty("Label2")] private Label _Label2; [AccessedThroughProperty("Label3")] private Label _Label3; [AccessedThroughProperty("lblLoginID")] private Label _lblLoginID; [AccessedThroughProperty("lblPwd")] private Label _lblPwd; [AccessedThroughProperty("lblSimpleCode")] private Label _lblSimpleCode; [AccessedThroughProperty("login1")] private HtmlImage _login1; [AccessedThroughProperty("login3")] private HtmlImage _login3; [AccessedThroughProperty("ok")] private ImageButton _ok; [AccessedThroughProperty("RequiredFieldValidator1")] private RequiredFieldValidator _RequiredFieldValidator1; [AccessedThroughProperty("txtloginid")] private TextBox _txtloginid; [AccessedThroughProperty("txtpwd")] private TextBox _txtpwd; [AccessedThroughProperty("valSum")] private ValidationSummary _valSum; private HttpCookie cookie; private int i; protected ResourceManager LocRM; public string logintitle; private Mr mr; private iOfficeData.Res Res; private string[] sErrMessage; private iofficeservice.sysSet sysSet; public login2() { base.Init += new EventHandler(this.Page_Init); base.Load += new EventHandler(this.Page_Load); this.mr = new Mr(); this.sysSet = new iofficeservice.sysSet(); this.Res = new iOfficeData.Res(); this.sErrMessage = new string[10]; } private void AccessIni() { this.Session["iSecureLogin"] = 0; this.Session["SerialNumber"] = ""; if (this.mr.InsSession(this.Session.SessionID, this.Request.UserHostAddress) == 1) { this.Response.Redirect("errdog.htm"); } this.imgLogo.Src = StringType.FromObject(this.Application["corplogo"]); //if (DateTime.Compare(DateTime.Today, DateType.FromString("2004-12-20")) >= 0) //{ // Page pgeParent = this; // DbConfig.ShowMessage(ref pgeParent, iOfficeData.Res.GetResource(this.sErrMessage[9], "你的试用期限已到,系统无法启动!请与程序供应商联系!"), "location.replace('http://www.ioffice.cn')", true); //} } private void BindBranch() { this.dropBranch.DataSource = this.mr.GetBranch(0).Tables["branchsel"]; this.dropBranch.DataBind(); } private void chpwd_Click(object sender, ImageClickEventArgs e) { this.UserLogin(1); } private void GotoNext(int iType) { if (iType != 0) { FormsAuthentication.SetAuthCookie(this.txtloginid.Text, false); this.Response.Redirect("chpwd.aspx"); } else { Page page; if (StringType.StrCmp(this.txtpwd.Text, "", false) == 0) { FormsAuthentication.SetAuthCookie(this.txtloginid.Text, false); page = this; DbConfig.ShowMessageAndRedirect(ref page, iOfficeData.Res.GetResource(this.sErrMessage[6], "你的密码为空!为了确保安全,请及时更改你的密码!"), "chpwd.aspx", true); } else { switch (this.mr.NeedIKey()) { case -1: { FormsAuthentication.SetAuthCookie(this.txtloginid.Text, false); string userAgent = this.Request.UserAgent; userAgent = Strings.Mid(userAgent, Strings.InStr(userAgent, "MSIE", CompareMethod.Binary) + 5, 3); if (!Information.IsNumeric(userAgent)) { page = this; DbConfig.ShowMessage(ref page, iOfficeData.Res.GetResource(this.sErrMessage[8], "请选取Microsoft Internet Explorer 6.0或以上版本作为浏览器。否则,将会有部份的iOffice.net功能不能正常使用!"), "/ioffice/default.aspx", true); return; } if (DoubleType.FromString(userAgent) <= 5.0) { page = this; DbConfig.ShowMessage(ref page, iOfficeData.Res.GetResource(this.sErrMessage[7], "你的Microsoft Internet Explorer 版本太低,请及时升级到6.0或以上版本。否则,将会有部份的iOffice.net功能不能正常使用!"), "/ioffice/default.aspx", true); return; } this.Response.Redirect("/ioffice/default.aspx"); return; } case 0: this.Response.Redirect("ikey/userlogin.aspx"); return; case 1: this.Response.Redirect("/ioffice/etoken/useretokenlogin.aspx"); return; } } } } [DebuggerStepThrough] private void InitializeComponent() { } private void LoadIngCookieLoginID() { if (this.Request.Cookies["iofficelogin"] != null) { this.cookie = this.Request.Cookies["iofficelogin"]; this.txtloginid.Text = this.cookie.Values["loginid"]; int num = this.dropBranch.Items.Count - 1; this.i = 0; while (this.i <= num) { if (StringType.StrCmp(this.dropBranch.Items[this.i].Text, this.cookie.Values["branch"], false) == 0) { this.dropBranch.Items[this.i].Selected = true; } this.i++; } this.txtpwd.Text = this.cookie.Values["pwd"]; if (StringType.StrCmp(this.txtpwd.Text, "", false) != 0) { this.cookie.Values["pwd"].Remove(1, this.cookie.Values["pwd"].Length - 1); this.UserLogin(0); } } } private void LocalRes() { ResourceManager manager=null; iOfficeData.Res res = this.Res; if (res.PrepareResource(typeof(login).Assembly, "ioffice.root", ref manager)) { Label lblLoginID = this.lblLoginID; string text = lblLoginID.Text; res.GetResourceEx(ref manager, ref text, "loginid"); lblLoginID.Text = text; lblLoginID = this.lblPwd; text = lblLoginID.Text; res.GetResourceEx(ref manager, ref text, "pwd"); lblLoginID.Text = text; lblLoginID = this.lblSimpleCode; text = lblLoginID.Text; res.GetResourceEx(ref manager, ref text, "simplecode"); lblLoginID.Text = text; res.GetResourceEx(ref manager, ref this.logintitle, "logintitle"); res.GetResourceEx(ref manager, ref this.sErrMessage[1], "sErrMessage1"); res.GetResourceEx(ref manager, ref this.sErrMessage[2], "sErrMessage2"); res.GetResourceEx(ref manager, ref this.sErrMessage[3], "sErrMessage3"); res.GetResourceEx(ref manager, ref this.sErrMessage[4], "sErrMessage4"); res.GetResourceEx(ref manager, ref this.sErrMessage[5], "sErrMessage5"); res.GetResourceEx(ref manager, ref this.sErrMessage[6], "sErrMessage62"); res.GetResourceEx(ref manager, ref this.sErrMessage[7], "sErrMessage7"); res.GetResourceEx(ref manager, ref this.sErrMessage[8], "sErrMessage8"); res.GetResourceEx(ref manager, ref this.sErrMessage[9], "sErrMessage9"); if (res.IsNotSimpChn()) { ImageButton ok = this.ok; text = ok.ImageUrl; res.GetResourceButtonImageURL(ref text); ok.ImageUrl = text; ok = this.chpwd; text = ok.ImageUrl; res.GetResourceButtonImageURL(ref text); ok.ImageUrl = text; HtmlImage help = this.help; text = help.Src; res.GetResourceButtonImageURL(ref text); help.Src = text; help = this.login1; text = help.Src; res.GetResourceButtonImageURL(ref text); help.Src = text; } } res = null; } private void ok_Click(object sender, ImageClickEventArgs e) { this.UserLogin(0); } protected void Page_Init(object sender, EventArgs e) { this.InitializeComponent(); } protected void Page_Load(object sender, EventArgs e) { this.AccessIni(); this.LocalRes(); if (!this.IsPostBack) { this.BindBranch(); this.LoadIngCookieLoginID(); } } private void PwdCookieSet() { this.cookie = new HttpCookie("iofficelogin"); this.cookie.Values.Add("loginid", this.txtloginid.Text); this.cookie.Values.Add("branch", this.dropBranch.SelectedItem.Value); this.cookie.Expires = DateTime.MaxValue; this.Response.AppendCookie(this.cookie); this.cookie = new HttpCookie("iofficeErr"); this.cookie.Values.Add("iofficeneterrset", this.txtpwd.Text + ":ioffice.net"); this.Response.AppendCookie(this.cookie); this.cookie = new HttpCookie("iofficeloginnum"); this.cookie.Values.Add("loginnum", this.mr.GetLoginInf()); //添加 Session["loginnum"] = this.mr.GetLoginInf(); this.Response.AppendCookie(this.cookie); } private void UserLogin(object iType) { int index = this.mr.LoginiOffice(this.sysSet.SafeSql(this.txtloginid.Text), this.txtpwd.Text, this.dropBranch.SelectedItem.Value, this.dropBranch.Visible); if (index == 0) { this.PwdCookieSet(); this.mr.UpLoginInfo(this.Request.UserHostAddress, this.Request.UserHostName); this.sysSet.GetUserStylesSet(); this.GotoNext(IntegerType.FromObject(iType)); } else { Page pgeParent = this; DbConfig.ShowMessage(ref pgeParent, this.sErrMessage[index], "", true); } } protected virtual ImageButton chpwd { get { return this._chpwd; } [MethodImpl(MethodImplOptions.Synchronized)] set { if (this._chpwd != null) { this._chpwd.Click -= new ImageClickEventHandler(this.chpwd_Click); } this._chpwd = value; if (this._chpwd != null) { this._chpwd.Click += new ImageClickEventHandler(this.chpwd_Click); } } } protected virtual DropDownList dropBranch { get { return this._dropBranch; } [MethodImpl(MethodImplOptions.Synchronized)] set { if (this._dropBranch != null) { } this._dropBranch = value; if (this._dropBranch != null) { } } } protected virtual HtmlForm frm { get { return this._frm; } [MethodImpl(MethodImplOptions.Synchronized)] set { if (this._frm != null) { } this._frm = value; if (this._frm != null) { } } } protected virtual HtmlImage help { get { return this._help; } [MethodImpl(MethodImplOptions.Synchronized)] set { if (this._help != null) { } this._help = value; if (this._help != null) { } } } protected virtual HtmlImage imgLogo { get { return this._imgLogo; } [MethodImpl(MethodImplOptions.Synchronized)] set { if (this._imgLogo != null) { } this._imgLogo = value; if (this._imgLogo != null) { } } } protected virtual Label Label1 { get { return this._Label1; } [MethodImpl(MethodImplOptions.Synchronized)] set { if (this._Label1 != null) { } this._Label1 = value; if (this._Label1 != null) { } } } protected virtual Label Label2 { get { return this._Label2; } [MethodImpl(MethodImplOptions.Synchronized)] set { if (this._Label2 != null) { } this._Label2 = value; if (this._Label2 != null) { } } } protected virtual Label Label3 { get { return this._Label3; } [MethodImpl(MethodImplOptions.Synchronized)] set { if (this._Label3 != null) { } this._Label3 = value; if (this._Label3 != null) { } } } protected virtual Label lblLoginID { get { return this._lblLoginID; } [MethodImpl(MethodImplOptions.Synchronized)] set { if (this._lblLoginID != null) { } this._lblLoginID = value; if (this._lblLoginID != null) { } } } protected virtual Label lblPwd { get { return this._lblPwd; } [MethodImpl(MethodImplOptions.Synchronized)] set { if (this._lblPwd != null) { } this._lblPwd = value; if (this._lblPwd != null) { } } } protected virtual Label lblSimpleCode { get { return this._lblSimpleCode; } [MethodImpl(MethodImplOptions.Synchronized)] set { if (this._lblSimpleCode != null) { } this._lblSimpleCode = value; if (this._lblSimpleCode != null) { } } } protected virtual HtmlImage login1 { get { return this._login1; } [MethodImpl(MethodImplOptions.Synchronized)] set { if (this._login1 != null) { } this._login1 = value; if (this._login1 != null) { } } } protected virtual HtmlImage login3 { get { return this._login3; } [MethodImpl(MethodImplOptions.Synchronized)] set { if (this._login3 != null) { } this._login3 = value; if (this._login3 != null) { } } } protected virtual ImageButton ok { get { return this._ok; } [MethodImpl(MethodImplOptions.Synchronized)] set { if (this._ok != null) { this._ok.Click -= new ImageClickEventHandler(this.ok_Click); } this._ok = value; if (this._ok != null) { this._ok.Click += new ImageClickEventHandler(this.ok_Click); } } } protected virtual RequiredFieldValidator RequiredFieldValidator1 { get { return this._RequiredFieldValidator1; } [MethodImpl(MethodImplOptions.Synchronized)] set { if (this._RequiredFieldValidator1 != null) { } this._RequiredFieldValidator1 = value; if (this._RequiredFieldValidator1 != null) { } } } protected virtual TextBox txtloginid { get { return this._txtloginid; } [MethodImpl(MethodImplOptions.Synchronized)] set { if (this._txtloginid != null) { } this._txtloginid = value; if (this._txtloginid != null) { } } } protected virtual TextBox txtpwd { get { return this._txtpwd; } [MethodImpl(MethodImplOptions.Synchronized)] set { if (this._txtpwd != null) { } this._txtpwd = value; if (this._txtpwd != null) { } } } protected virtual ValidationSummary valSum { get { return this._valSum; } [MethodImpl(MethodImplOptions.Synchronized)] set { if (this._valSum != null) { } this._valSum = value; if (this._valSum != null) { } } } } }

110,555

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧