master.FindControl的问题,菜鸟求助!!
我创建了一个网站,里面一个母版页,一个内容页,现在我在母版页添加了一个label,显示系统时间,在内容页中也添加一个label,用于显示母版页label的文本,但是抛出了异常。具体如下:
母版页:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MainMaster.master.cs" Inherits="MainMaster" %>
<!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">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
<asp:Label ID="lbM" runat="server"></asp:Label>
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class MainMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
lbM.Text = "今天是" + DateTime.Today.Year + "年" + DateTime.Today.Month + "月" + DateTime.Today.Day + "日";
}
}
内容页:
<%@ Page Title="" Language="C#" MasterPageFile="~/MainMaster.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<p>
<asp:Label ID="lbD" runat="server" Text="Label"></asp:Label>
</p>
</asp:Content>
cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_LoadComplete(object sender, EventArgs e)
{
[color=#FF0000]Label lb = (Label)this.Master.FindControl("lbM");
lbD.Text = lb.Text;
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
抛出异常:
lbM.Text = "今天是" + DateTime.Today.Year + "年" + DateTime.Today.Month + "月" + DateTime.Today.Day + "日";---
Gridview模板列的有关问题。