0

下面的代码是一个功能齐全的 ASP.NET 计算器。即使用户关闭浏览器,如何保留输入的所有计算的历史记录?例如,如果用户输入 5+10,我需要它在我关闭并再次打开程序后显示 15。

另外,有没有办法显示过去 10 分钟内所有计算的历史记录?

<script runat="server">

sub getvalue0(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn0.Text

end sub



sub getvalue1(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn1.Text

end sub



sub getvalue2(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn2.Text

end sub



sub getvalue3(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn3.Text

end sub



sub getvalue4(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn4.Text

end sub



sub getvalue5(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn5.Text

end sub



sub getvalue6(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn6.Text

end sub



sub getvalue7(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn7.Text

end sub



sub getvalue8(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn8.Text

end sub



sub getvalue9(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn9.Text

end sub



sub check(s as Object, e as EventArgs)

val1.Text=txtinput.Text

txtinput.Text=""

end sub



sub sum(s as Object, E as EventArgs)

val2.Text=txtinput.Text

dim result=cInt(val1.Text)+cInt(val2.Text)

txtinput.Text=result

end sub



sub clear(s as Object, e as EventArgs)

txtinput.Text=""

val1.Text=""

val2.Text=""

end sub



</script>



<html>

<title> Basic Calculator</title>

<body>



<form id="Form1" runat="server">

<h1>Basic Calculator</h1>

<asp:TextBox id="txtinput" runat="server" />

<table style="background:#00ffff">

<tr>

<td><asp:Button id="btn0" Text="0" runat="server" OnClick="getvalue0" /></rd>

<td><asp:Button id="btn1" Text="1" runat="server" OnClick="getvalue1" /></rd>

<td><asp:Button id="btn2" Text="2" runat="server" OnClick="getvalue2"/></rd>



<td><asp:Button id="btnsum" Text="+" runat="server" OnClick="check"/></rd>

</tr>

<td><asp:Button id="btn3" Text="3" runat="server" OnClick="getvalue3" /></rd>

<td><asp:Button id="btn4" Text="4" runat="server" OnClick="getvalue4"/></rd>

<td><asp:Button id="btn5" Text="5" runat="server" OnClick="getvalue5"/></rd>



<td><asp:Button id="btnequal" Text="=" runat="server" OnClick="sum" /></rd>

</tr>

<td><asp:Button id="btn6" Text="6" runat="server" OnClick="getvalue6"/></rd>

<td><asp:Button id="btn7" Text="7" runat="server" OnClick="getvalue7"/></rd>

<td><asp:Button id="btn8" Text="8" runat="server" OnClick="getvalue8"/></rd>



<td><asp:Button id="btnclear" Text="Clear" runat="server" OnClick="clear"/></rd>

</tr>

<tr><td><asp:Button id="btn9" Text="9" runat="server" OnClick="getvalue9"/></td></tr>

</table>

<asp:Textbox id="val1" Visible="false" runat="server"/>

<asp:Textbox id="val2" visible="false" runat="server"/>



</form>

</body>

</html>
4

2 回答 2

0

我将创建一个名为 SetText 之类的新函数。您将从具有要设置的值的所有其他事件处理程序中调用此方法。

sub setText(s as String)

  Dim myCookie As New HttpCookie("calculatorValue")
  myCookie.Value = s
  myCookie.Expires = DateTime.Now.AddMinutes(30)
  Response.Cookies.Add(myCookie)

  txtinput.Text= s

end sub

然后,在您页面的 pageload 事件中。如有必要,您可以设置文本框

sub PageLoad()

    Dim cookie As HttpCookie = Request.Cookies.Get("calculatorValue")
    if (txtimput.Text == "" && cookie != null && cookie.Value != "")
    {
         txtinput.Text = cookie.Value;

         //Clear out cookie
    }

这可能不是 100%,因为我没有测试,但应该非常接近。

于 2013-02-14T03:38:08.883 回答
0

如果您希望在关闭浏览器后保留它,您需要将数据保留在数据库或文件之类的地方。

您可以尝试存储在会话中,但取决于您的站点设置方式,这无法保证它将存在多长时间(有关更多信息,请参阅不同类型的会话模式)。

于 2013-02-14T03:23:39.597 回答