单击按钮后,ASP.NET页面元素消失

时间:2017-07-30 23:33:15

标签: c# html asp.net webforms

我有2个页面,第一个(索引)是一个带有2个TextForm的表单,2个信息是通过POST方法发送的。 在第二页(WebForm)中,单击按钮更新时间后,其他2项(FirstName和LastName)消失,我不明白为什么? 我该怎样做才能“保留”这2个项目?

以下是所有页面:

的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)
        {

        }
    }
}

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

1 个答案:

答案 0 :(得分:1)

您拥有的是跨页回发:https://msdn.microsoft.com/en-us/library/ms178139.aspx

你想要类似的东西:

string firstName;
string lastName;
if (Page.PreviousPage != null)
{
    TextBox srcFirstName= 
        (TextBox)Page.PreviousPage.FindControl("firstName");
    if (srcFirstName!= null)
    {
        firstName = srcLastName.Text;
    }

    TextBox srcLastName= 
        (TextBox)Page.PreviousPage.FindControl("lastName");
    if (srcLastName!= null)
    {
        lastName = srcLastName.Text;
    }
}

注意这是一个粗略且未经测试的示例,但应该让您朝着正确的方向前进。