2012-08-07 17 views
-2

標記:爲什麼在Page_Load事件處理程序中沒有設置Label控件的文本屬性?

<%@ Page Language="C#" AutoEventWireup="false" CodeBehind="Files.aspx.cs" Inherits="WebModules.Web.Files" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 

</head> 
<body> 
    <form id="form1" method="post" runat="server"> 
     <asp:Label ID="Status" runat="server" /> 
    </form> 
</body> 
</html> 

代碼背後:

using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Web; 
using System.Web.SessionState; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 
using System.IO; 

namespace WebModules.Web 
{ 
    public partial class Files : System.Web.UI.Page 
    { 
     private void Page_Load(object sender, EventArgs e) 
     { 
      Status.Text="Hello"; 
     } 

     private void Page_Init(object sender, EventArgs e) 
     { 
      InitializeComponent(); 
     } 

     private void InitializeComponent() 
     { 
      this.Load += new System.EventHandler(this.Page_Load); 
     } 
    } 
} 

當我在瀏覽器中運行網頁中的文字 「你好」 是不是在標籤上顯示。

有誰知道爲什麼這不起作用?

+0

這對我來說工作得很好。 – JonH 2012-08-07 17:42:55

+2

你爲什麼改變頁面初始化週期? – 2012-08-07 17:43:03

+0

@AndreCalil請回答點 – 2012-08-07 17:44:22

回答

1

你可能要嘗試從私人保護改變你的方法訪問修飾符:

//I'm assuming that Files is the class of your page, and not just another class. Make sure that your markup inherits from this class 
public partial class Files : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     Status.Text="Hello"; 
    } 
} 
+0

其不工作方式 – 2012-08-07 17:49:21

+0

你可以將代碼發佈到你的aspx頁面嗎? – mreyeros 2012-08-07 17:50:04

+0

我已發佈。請參閱編輯的問題 – 2012-08-07 17:56:30

0

我複製粘貼的標籤和頁面加載代碼到我目前的應用程序,它顯示的罰款。

這樣做:

保護無效的Page_Load(對象發件人,EventArgs的)

{ 
Status.Text="Hello";   
} 

然後將拉貝ID在你的源代碼:

<asp:Label ID="Status" runat="server" /> 
+0

不是這樣工作 – 2012-08-07 17:58:33

+0

你把它放在你的查詢頁面嗎?如果是這樣,把它放在你的default.aspx.cs而不是 – KKP 2012-08-07 18:01:18

+0

我已經修復它!我剛將屬性AutoEventWireup =「false」更改爲AutoEventWireup =「true」。它現在工作正常 – 2012-08-07 18:05:40

0

我已經定了!我剛將屬性AutoEventWireup="false"更改爲AutoEventWireup="true"。它現在正常工作

相關問題