2016-04-18 5 views
-1

私はここで経験しているものに何かを見逃していなければ、ウェブの素晴らしい世界のすべてのクッキー関連のスレッドとそれ以外のものを探していました。次に、ユーザは、「タイマ開始」をクリックすると、その後、保存することになっているクッキーについての不満

<%@ 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> 

testform.aspx:

は、私は以下のようにデータを入力するユーザーを取得イムaspxページを持っていますなどCodeFileでクッキーにそのデータ:

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にこのデータを渡す必要があります。

タイマーを.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が保存されているブラウザをクリアしているため、null値timer.aspxページに読み込まれるときのクッキー。

testform.aspxページで値を入力せずにもう一度start timerをクリックすると、以前の実行からクッキー値が取得されます。

ここには何がありますか?それは何かシンプルですが、私はそれをどこにでも見つけられず、ゆっくりと私を狂わせてしまいます。

ここでクッキープロセスのセットアップで何が間違っているのかよく分かりますか?

私は冗長かもしれない質問をする前に、何か解決策が1つの石で2番目の鳥を殺すことができるかどうかを確認します。

ありがとうございました。

編集: @khlrのアドバイスのおかげで、私は上記の質問を解決しました。お返事ありがとうございます。

編集2:ここでは、クッキー名をランダムに生成し、次のページでそのクッキーにアクセスすることは可能ですか?この側面の質問の理由は、timer.aspxページの複数回を一度に実行し、個々のページが表示しているデータを保存できるようにすることです。または、クッキーはそれに最も適していませんか?

+1

'OnClick =" btnSubmit_Click "と' OnClientClick = "window.open( 'timer.aspx')は同時に実行されますか? – khlr

+1

作成されるクッキーに特定のパスを追加してみてください。このようなもの: 'cookie.Path ="/";' – user1429080

+0

@khlr、それは良い質問です。私は彼らがそうだと言うでしょうが、私は自分のコードを調べてテストします。 –

答えて

1

Cookieは文字列を格納しますが、バイナリオブジェクトは格納しません。したがって、オブジェクトを文字列にシリアライズすることができます。そのためには、クラスであなたのToXmlStringメソッドを記述し、オブジェクトを文字列に変換しますパラメータとしてメソッドにOBJを送信し、

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(); 
    } 

は、その後、あなたのクッキーと連携クッキー値として設定します。ありがとうございました

+0

お返事ありがとうございました。私はテストしたり、あなたの方法を使用することはありませんでしたが、それは私と同じ状況で誰かを助けることを確信しています。 –

関連する問題