为什么没有在Page_Load事件处理程序中设置Label控件的text属性?

时间:2012-08-07 17:40:41

标签: c# asp.net .net

标记:

<%@ 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);
        }
    }
}

当我在浏览器中运行页面时,标签上没有显示文本“Hello”。

有谁知道为什么这不起作用?

3 个答案:

答案 0 :(得分: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";
    }
}

答案 1 :(得分:0)

我将您的标签和页面加载代码粘贴到我当前的应用程序中,它显示正常。

这样做:

protected void Page_Load(object sender,EventArgs e)

{
Status.Text="Hello";          
}  

然后将labe ID放在源代码中:

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

答案 2 :(得分:0)

我修好了!我刚刚将属性AutoEventWireup="false"更改为AutoEventWireup="true"。它现在工作正常