我一直在浏览这里所有与cookie相关的线程以及其余的精彩网络世界,除了我遗漏了什么之外,没有发现任何相关信息。
我有一个aspx页面,我可以让用户输入数据,如下所示:
testform.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testform.aspx.cs" Inherits="assets_testform" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Incident Druation Monitor</title>
<link rel="icon" type="image/ico" href="assets/images/favicon.ico" />
<link rel="apple-touch-icon" href="assets/images/favicon.png" />
<meta name="msapplication-TileImage" content="assets/images/favicon.png" />
<link rel="stylesheet" type="text/css" href="assets/css/site.css" />
</head>
<body>
<div id="formpage">
<div class="header">
</div><!-- end header -->
<div class="form-style-10">
<h1>Incident Duration<span>Input the details below and start the timer!</span></h1>
<form id="frmIdm" name="frmIdm" runat="server">
<div class="section">Incident Details:</div>
<div class="inner-wrap">
<label>Incident Name: <asp:TextBox runat="server" id="txtIncnam" /></label>
<label>Incident Location: <asp:TextBox runat="server" id="txtIncloc" /></label>
<label>Incident Type: <asp:TextBox runat="server" id="txtInctype" /></label>
<label>Date and Time Incident Occured: </label>
<label class="sublable">Month: <asp:DropDownList name="month" runat="server" ID="ddlMonth" width="150">
<asp:ListItem Text="Select the Month" Value="0" Selected="True"></asp:ListItem>
<asp:ListItem Text="January" Value="Jan"></asp:ListItem>
<asp:ListItem Text="Februray" Value="Feb"></asp:ListItem>
<asp:ListItem Text="March" Value="Mar"></asp:ListItem>
<asp:ListItem Text="April" Value="Apr"></asp:ListItem>
<asp:ListItem Text="May" Value="May"></asp:ListItem>
<asp:ListItem Text="June" Value="Jun"></asp:ListItem>
<asp:ListItem Text="July" Value="Jul"></asp:ListItem>
<asp:ListItem Text="August" Value="Aug"></asp:ListItem>
<asp:ListItem Text="September" Value="Sep"></asp:ListItem>
<asp:ListItem Text="October" Value="Oct"></asp:ListItem>
<asp:ListItem Text="November" Value="Nov"></asp:ListItem>
<asp:ListItem Text="December" Value="Dec"></asp:ListItem>
</asp:DropDownList></label>
<label class="sublable">Day: <asp:DropDownList name="day" runat="server" ID="ddlDay" Width="150">
<asp:ListItem Text="Select the Day" Value="0" Selected="True"></asp:ListItem>
<asp:ListItem Text="1" Value="01"></asp:ListItem>
<asp:ListItem Text="2" Value="02"></asp:ListItem>
<asp:ListItem Text="3" Value="03"></asp:ListItem>
<asp:ListItem Text="4" Value="04"></asp:ListItem>
<asp:ListItem Text="5" Value="05"></asp:ListItem>
<asp:ListItem Text="6" Value="06"></asp:ListItem>
<asp:ListItem Text="7" Value="07"></asp:ListItem>
<asp:ListItem Text="8" Value="08"></asp:ListItem>
<asp:ListItem Text="9" Value="09"></asp:ListItem>
<asp:ListItem Text="10" Value="10"></asp:ListItem>
<asp:ListItem Text="11" Value="11"></asp:ListItem>
<asp:ListItem Text="12" Value="12"></asp:ListItem>
<asp:ListItem Text="13" Value="13"></asp:ListItem>
<asp:ListItem Text="14" Value="14"></asp:ListItem>
<asp:ListItem Text="15" Value="15"></asp:ListItem>
<asp:ListItem Text="16" Value="16"></asp:ListItem>
<asp:ListItem Text="17" Value="17"></asp:ListItem>
<asp:ListItem Text="18" Value="18"></asp:ListItem>
<asp:ListItem Text="19" Value="19"></asp:ListItem>
<asp:ListItem Text="20" Value="20"></asp:ListItem>
<asp:ListItem Text="21" Value="21"></asp:ListItem>
<asp:ListItem Text="22" Value="22"></asp:ListItem>
<asp:ListItem Text="23" Value="23"></asp:ListItem>
<asp:ListItem Text="24" Value="24"></asp:ListItem>
<asp:ListItem Text="25" Value="25"></asp:ListItem>
<asp:ListItem Text="26" Value="26"></asp:ListItem>
<asp:ListItem Text="27" Value="27"></asp:ListItem>
<asp:ListItem Text="28" Value="28"></asp:ListItem>
<asp:ListItem Text="29" Value="29"></asp:ListItem>
<asp:ListItem Text="30" Value="30"></asp:ListItem>
<asp:ListItem Text="31" Value="31"></asp:ListItem>
</asp:DropDownList></label>
<label class="sublable">Year: <asp:TextBox name="year" ID="txtyear" runat="server" placeholder="2016" Width="150" /></label>
<label class="sublable">Time (24hr format): <asp:TextBox name="timestart" ID="txtTimestart" runat="server" placeholder="00:00:00" Width="150" /></label>
<label><asp:Button Text="Start Timer" runat="server" ID="btnStart_timer" OnClick="btnSubmit_Click" OnClientClick="window.open('timer.aspx')" /></label>
</div>
<script type="text/javascript">
function fixform() {
if (opener.document.getElementById("frmIdmt").target != "_blank") return;
opener.document.getElementById("frmIdmt").target = "";
opener.document.getElementById("frmIdmt").action = opener.location.href;
}
</script>
</form>
</div>
</div>
</body>
</html>
当用户点击“Start Timer”时,它应该将该数据存储到CodeFile中的cookie中:
testform.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class assets_testform : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("PreviousPage");
CookieData cd = new CookieData();
cd.IncidentLocation = txtIncloc.Text;
cd.IncidentDay = ddlDay.SelectedValue;
cd.IncidentMonth = ddlMonth.SelectedValue;
cd.IncidentName = txtIncnam.Text;
cd.IncidentType = txtInctype.Text;
cd.StartTime = txtTimestart.Text;
cd.Year = txtyear.Text;
cookie.Value = cd.ToXmlString();
Response.Cookies.Add(cookie);
}
}
然后将数据传递到CookieData.cs文件,如下所示:
CookieData.cs
using System;
using System.IO;
using System.Xml.Serialization;
/// <summary>
/// Summary description for CookieData
/// </summary>
public class CookieData
{
public String ToXmlString()
{
XmlSerializer xs = new XmlSerializer(typeof(CookieData));
MemoryStream ms = new MemoryStream();
xs.Serialize(ms, this);
return Convert.ToBase64String(ms.ToArray());
}
public static CookieData FromXmlString(String XML)
{
XmlSerializer xs = new XmlSerializer(typeof(CookieData));
byte[] data = Convert.FromBase64String(XML);
MemoryStream ms = new MemoryStream(data);
return (CookieData)xs.Deserialize(ms);
}
public CookieData()
{
//
// TODO: Add constructor logic here
//
}
public String IncidentName
{
get;
set;
}
public String IncidentType
{
get;
set;
}
public String IncidentLocation
{
get;
set;
}
public String IncidentDay
{
get;
set;
}
public String IncidentMonth
{
get;
set;
}
public String Year
{
get;
set;
}
public String StartTime
{
get;
set;
}
}
然后它应该将这些数据传递到目标页面timer.aspx,如下:
timer.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="timer.aspx.cs" Inherits="timer" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Incident Druation Monitor - Timer</title>
<link rel="icon" type="image/ico" href="idt_files/images/favicon.ico" />
<link rel="apple-touch-icon" href="idt_files/images/favicon.png" />
<meta name="msapplication-TileImage" content="idt_files/images/favicon.png" />
<script src="idt_files/scripts/js/jquery-1.12.2.js"></script>
<link rel="stylesheet" type="text/css" href="idt_files/css/site.css" />
</head>
<body>
<div id="formpage">
<div class="header">
</div><!-- end header -->
<div class="form-style-10">
<h1>Incident Duration<span>The timer has started below.</span></h1>
<form id="frmIdmt" name="frmIdm" runat="server">
<script type="text/javascript">
/*
* Basic Count Up from Date and Time
* Author: @mrwigster / trulycode.com
*/
$(document).ready(function () {
var month = ("<%=month %>");
var day = ("<%=day %>");
var year = ("<%=year %>");
var datestart = month + "," + day + "," + year;
var timestart = ("<%=start %>");
// Month,Day,Year,Hour,Minute,Second
upTime(datestart + " " + timestart); // ****** Change this line!
});
function upTime(countTo) {
now = new Date();
countTo = new Date(countTo);
difference = (now - countTo);
days = Math.floor(difference / (60 * 60 * 1000 * 24) * 1);
hours = Math.floor((difference % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1);
mins = Math.floor(((difference % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1);
secs = Math.floor((((difference % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1);
document.getElementById('countup').setAttribute("uptime", days + ':' + hours + ':' + mins + ':' + secs)
document.getElementById('days').firstChild.nodeValue = days;
document.getElementById('hours').firstChild.nodeValue = hours;
document.getElementById('minutes').firstChild.nodeValue = mins;
document.getElementById('seconds').firstChild.nodeValue = secs;
clearTimeout(upTime.to);
upTime.to = setTimeout(function () { upTime(countTo); }, 1000);
}
function stop_timer() {
clearTimeout(upTime.to);
var endtime = 'Days: ' + days + '. Time: ' + hours + ':' + mins + ':' + secs;
document.getElementById('incendtime').value = endtime;
/*return;*/
}
</script>
<!-- Start Incident Details -->
<div runat="server" id="idmstart" style="/*display:none;*/">
<div class="section">Incident Duration Timer!</div>
<div class="inner-wrap">
<label>Incident Name: <asp:Label runat="server" id="lblName"></asp:Label></label>
<label>Incident Location: <asp:Label runat="server" id="lblLocation"></asp:Label></label>
<label>Incident Type: <asp:Label runat="server" id="lblType"></asp:Label></label>
<label>Incident Started at: <asp:Label runat="server" id="lblTime"></asp:Label></label>
<div id="countup">
<h3>Duration Time</h3>
<div class="container">
<div id="clockdiv">
<div>
<span id="days">00</span>
<div class="timeRefDays">Days</div>
</div>
<div>
<span id="hours">00</span>
<div class="timeRefHours">Hrs.</div>
</div>
<div>
<span id="minutes">00</span>
<div class="timeRefMinutes">Min.</div>
</div>
<div>
<span id="seconds">00</span>
<div class="timeRefSeconds">Sec.</div>
</div>
</div>
</div>
</div>
<br /><br /><br /><br />
<!-- <asp:Button runat="server" Text="Stop Timer" ID="btnStop_timer" OnClientClick="stop_timer()" /> --> <asp:Button runat="server" ID="btnSave" Text="Save Log" OnClick="btnSave_Click" OnClientClick="stop_timer()" />
<asp:HiddenField runat="server" ID="incendtime" />
</div>
</div>
<!-- End Incident Details -->
</form>
</div>
</div>
</body>
</html>
timer.aspx.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class timer : System.Web.UI.Page
{
protected string month;
protected string day;
protected string year;
protected string start;
public void Page_Load(object sender, EventArgs e)
{
CookieData cd = CookieData.FromXmlString(Request.Cookies["PreviousPage"].Value);
month = cd.IncidentMonth;
day = cd.IncidentDay;
year = cd.Year;
start = cd.StartTime;
var datestart = month + "," + day + " " + year;
var tod = "";
DateTime starttime = DateTime.Parse(start);
tod = starttime.ToString("T");
lblName.Text = cd.IncidentName;
lblLocation.Text = cd.IncidentLocation;
lblType.Text = cd.IncidentType;
lblTime.Text = " " + tod;
}
public void btnSave_Click(object sender, EventArgs e)
{
string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var name = lblName.Text.Replace("_", string.Empty);
using (StreamWriter objWriter = new StreamWriter("C:/Users/agilbert/Desktop/tmp/Test_" + name + ".txt"))
{
var tod = "";
var itimestart = start;
var datestart = month + "/" + day + "/" + year;
string endtime = incendtime.Value;
DateTime starttime = DateTime.Parse(itimestart);
tod = starttime.ToString("T");
objWriter.WriteLine("Incident log created: " + DateTime.Now.ToString());
objWriter.WriteLine("Incident Name: " + lblName.Text);
objWriter.WriteLine("Incident Location: " + lblLocation.Text);
objWriter.WriteLine("Incident Type: " + lblType.Text);
objWriter.WriteLine("Incident happend on: " + datestart);
objWriter.WriteLine("Incident Started at: " + lblTime.Text);
objWriter.WriteLine("Duration of incident: " + endtime);
objWriter.Flush();
objWriter.Close();
}
ClientScript.RegisterStartupScript(typeof(Page), "closePage", "window.close();", true);
//Response.Redirect("~/Default.aspx");
}
}
我不知道我是否在我的代码中弄乱了一些东西,但是当我第一次完全运行时,这是清除浏览器当前存储的cookie,我得到一个空值的异常错误加载到timer.aspx页面时的cookie。
然后我可以在testform.aspx页面上再次点击启动计时器,没有输入任何值,我将从上一次运行中获取cookie值。
这里缺少什么?我更确定这是一件简单的事情,但我无法在任何地方发现它,它慢慢地让我疯狂。
所以很清楚我在这里设置cookie过程时遇到了什么问题?
还有更多,但我会看到,在我提出一个可能多余的问题之前,是否有任何解决办法可以一石二鸟杀死第二只鸟。
感谢您的帮助。
编辑:感谢@khlr的建议,我已经解决了上述问题。感谢所有回复和提供的选项让我尝试。
编辑2:此处的问题是,是否可以随机生成Cookie名称,然后在下一页访问所述Cookie?这个问题的原因是我希望能够立即运行timer.aspx页面的多个并保存各个页面显示的数据。或者饼干不是最适合的吗?
答案 0 :(得分:1)
Cookies存储字符串,但不存储二进制对象。因此,您可以将对象序列化为字符串。为此,在类中编写ToXmlString方法并将obj作为参数发送给方法,该方法将对象转换为字符串并设置为cookie值
protected void btnSubmit_Click(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("PreviousPage");
CookieData cd = new CookieData();
cd.IncidentLocation = txtIncloc.Text;
cd.IncidentDay = ddlDay.SelectedValue;
cd.IncidentMonth = ddlMonth.SelectedValue;
cd.IncidentName = txtIncnam.Text;
cd.IncidentType = txtInctype.Text;
cd.StartTime = txtTimestart.Text;
cd.Year = txtyear.Text;
cookie.Value = ToXmlString(cd);
Response.Cookies.Add(cookie);
}
public String ToXmlString(CookieData cdObj)
{
XmlSerializer xs = new XmlSerializer(typeof(List<List<string>>));
StringWriter outSWriter = new StringWriter();
xs.Serialize(outSWriter, cdObj);
return outSWriter.ToString();
}
然后使用你的cookie。感谢