网页用户编辑和设置cookie(asp.net和c#) [英] Webpage user editing and setting cookies (asp.net and c#)

查看:92
本文介绍了网页用户编辑和设置cookie(asp.net和c#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如您所见,网页将获得一个名为"asdf"的cookie,并将字符串值放入textbox1中.然后,我解析该字符串以取出"asdf ="并将解析后的字符串放置在textbox2中.

我希望网页用户能够编辑textbox2中的字符串,并将其保存为cookie,但是使用SetCookie过程无法正常工作.

提前谢谢.

这是c#源代码:

As you can see, the web page gets a cookie named "asdf" and puts the string value in textbox1. I then parsed the string to take out the "asdf=" and place the parsed string in textbox2.

I want the web page user to be able to edit the string in textbox2, and have that saved as the cookie but this isn''t working using the SetCookie procedure.

Thanks in advance.

Here is the c# source code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace WebApplication23
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string cookieString = GetCookie("asdf");
            TextBox2.Text = cookieString;
            TextBox3.Text = "";
            parseData(cookieString, "asdf=", TextBox3);
        }
        //-------------------------------------------------------------
        private void parseData(string cookieString, string pattern, TextBox txt)
        {
            int b = 0;
            int c;
            string[] substrings = Regex.Split(cookieString, pattern);
            foreach (string match in substrings)
            {
                if (substrings[b] != "")
                {
                    substrings[b] += "";// +Environment.NewLine;
                    }
                b++;
            }
            for (c = 0; c < b; c++)
            {
                TextBox3.Text+=substrings[c];
            }
        }
        //-------------------------------------------------------------
        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox2.Text = "";
            TextBox2.Text=GetCookie("asdf");
        }
        //-------------------------------------------------------------
        public string GetCookie(string cookiename)
        {
            string cookyval = "";
            try
            {
                cookyval = Request.Cookies[cookiename].Value;
                //cookyval = Request.Cookies[cookiename].Name;
            }
            catch (Exception e)
            {
                cookyval = "";
            }
            return cookyval;
        }
        //-------------------------------------------------------------
        protected void Button1_Click(object sender, EventArgs e)
        {
            SetCookie("asdf", TextBox3.Text, 1);
        }
        //-------------------------------------------------------------      
        
        
        
        
        //-------------------------------------------------------------
        public bool SetCookie(string cookiename, string cookievalue, int iDaysToExpire)
        {
            try
            {
                HttpCookie objCookie = new HttpCookie(cookiename);
                Response.Cookies.Clear();
                Response.Cookies.Add(objCookie);
                objCookie.Values.Add(cookiename, cookievalue);
                DateTime dtExpiry = DateTime.Now.AddDays(iDaysToExpire);
                Response.Cookies[cookiename].Expires = dtExpiry;
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        protected void TextBox3_TextChanged(object sender, EventArgs e)
        {
            SetCookie("asdf", TextBox3.Text, 1);
        }
    }
}

推荐答案

尝试以下操作:
Try this:
HttpCookie objCookie = new HttpCookie(cookiename);
objCookie.Values.Add(cookiename, cookievalue);
DateTime dtExpiry = DateTime.Now.AddDays(iDaysToExpire);
Response.Cookies.Clear();
Response.Cookies.Add(objCookie);
Response.Cookies[cookiename].Expires = dtExpiry;



我刚刚更改了将Cookie附加到响应的顺序.制作Cookie后,请向其中添加值,然后将其添加到响应中.



I have just changed the order of attaching the cookie to the response. Once you have make the Cookie, add values to it then add it to the response.


这篇关于网页用户编辑和设置cookie(asp.net和c#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆