我一直用默认的ASP.NET Web应用程序模板玩弄和下面的代码会抛出一个异常:未设置的一个实例 ASP.NET/C#混乱
对象引用目的。
点击创建的按钮。
任何人都可以提供技术解释吗?
注1:标记只是一个带有占位符的空白页 - 见下文。
注2:用Button
代替LinkButton
,代码不会抛出异常并起作用。
public partial class test : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
foo();
}
protected override void OnLoad(EventArgs e)
{
foo();
}
protected void foo()
{
placeholder1.Controls.Clear();
placeholder1.Controls.Add(new Button() { Text = "test", ID = "btn" });
}
}
标记:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WebApplication1.test" %>
<!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" runat="server">
<div>
<asp:PlaceHolder runat="server" ID="placeholder1" />
</div>
</form>
</body>
</html>
什么是例外? – Brandon 2011-03-24 14:43:30
未将对象引用设置为对象的实例。 – maxp 2011-03-24 14:45:33
什么行是抛出的异常? – Oded 2011-03-24 14:46:54