问题描述:

I have this project where I want to transfer data from textbox1 with session "sam" on the about.aspx on its label. The problem is that when I input a number, it is not shown on label. My problem is that I type something on the "Αριθμός Επιβατών:" textbox (a number for example) and after that I click submit I need to get the number on the about page next to "Ari8mos epivatwn :"

Defaultaspxcs.txt:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace WebApplication4

{

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void Button1_Click2(object sender, EventArgs e)

{

string txt = TextBox1.Text;

Session["sam"] = txt;

}

}

}

Aboutaspxcs.txt:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace WebApplication4

{

public partial class About : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

Label1.Text = (string)Session["sam"];

}

}

}

Aboutaspx.txt:

<%@ Page Title="About Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"

CodeBehind="About.aspx.cs" Inherits="WebApplication4.About" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

</asp:Content>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

<p>

Ari8mos epivatwn :

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

</p>

</asp:Content>

https://www.dropbox.com/sh/4ftlddfhqo8n99p/gm-TNvol0S

网友答案:

It's likely that the content for the Label is being written twice, and not in the order your expect.

Start by familiarizing yourself with the asp.net page life-cycle

You will notice that PageLoad is called long before Render.

So, the page loads like this:

  1. PageLoad Label text set to some number from your code
  2. A bunch of other stuff
  3. Render sets the Label text to "Label" instead, since that is what you have defined in your chtml.

Review this part of your code:

...
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
...

Try this instead:

...
<asp:Label ID="Label1" runat="server"></asp:Label>
...
相关阅读:
Top