2
2ページあり、最初の(索引)は2つのTextFormを持つフォームで、2つの情報はPOSTメソッドによって送信されます。 2番目のページ(WebForm)では、ボタンをクリックして時間を更新した後、他の2つの項目(FirstNameとLastName)が消えてしまいますが、なぜわかりませんか? 2つのアイテムを「保管」するにはどうすればよいですか?ボタンをクリックするとASP.NET要素が消える
index.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="HelloWorlds.Index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="firstName" runat="server"></asp:TextBox>
<asp:TextBox ID="secondName" runat="server"></asp:TextBox>
<asp:Button ID="btn" runat="server" Text="GO!" PostBackUrl="~/WebForm.aspx" />
</div>
</form>
</body>
</html>
WebForm.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm.aspx.cs" Inherits="HelloWorlds.WebForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="item1" runat="server"></asp:TextBox>
<asp:Button ID="b1" runat="server" Text="Click Here!" OnClick="b1_Click"></asp:Button>
<br /> <br />
<asp:Label ID="labelFirstName" runat="server"></asp:Label>
<asp:Label ID="labelLastName" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Index.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace HelloWorlds
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
Webフォーム
これらは、すべてのページです。 aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace HelloWorlds
{
public partial class WebForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DateTime actualDate = DateTime.Now;
item1.Text = actualDate.ToString();
string firstName = Request.Form["firstName"];
string lastName = Request.Form["secondName"];
if (firstName == "FirstName" && lastName == "SecondName") {
labelFirstName.Text = "Not Valid";
labelLastName.Text = "Not Valid";
}
else {
labelFirstName.Text = firstName;
labelLastName.Text = lastName;
}
}
protected void b1_Click(object sender, EventArgs e)
{
DateTime actualDate = DateTime.Now;
item1.Text = actualDate.ToString();
}
}
}
ウェブ企業はWindowsフォームと同じではないことを理解する必要があります。 Webフォームは状態が少ないので、ボタンをクリックするとページ全体が再び読み込まれ、page_loadが再び実行されます。したがって、次のペイロードは 'Request.Form [" firstName "]'と他の値を持たないでしょう。だからあなたは値が消えているのを見るのです。あなたは 'if(!IsPostBack)'ブロックの中にpage_loadのコードを入れる必要があります –