因此,您有一些方法没有解释,但您是否将SubmitButton
添加到页面?某处应该是:
SomeServerControl.Controls.Add(SubmitButton);
下,我(显然改变命名空间)作品:
ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="VS2010WEBFORMS.WebForm1" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>
ASPX.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace VS2010WEBFORMS
{
public partial class WebForm1 : System.Web.UI.Page
{
Button SubmitButton = new Button();
protected void Page_Load(object sender, EventArgs e)
{
SubmitButton.Text = "Submit";
SubmitButton.Click += new EventHandler(SubmitButton_Click);
form1.Controls.Add(SubmitButton);
}
private void SubmitButton_Click(object sender, EventArgs e)
{
//Something
}
}
}
是的,我我将它添加到CreateChildControls中的页面)方法,我发布后的一行(假定它不重要)。我将控件移入OnLoad(),现在它可以工作。但是不正确的地方CreateChildControls()? – 2010-09-25 00:23:32
您是否正在像文档状态那样添加,将控件添加到页面的代码对于保留在您的文章中至关重要。 http://msdn.microsoft.com/en-us/library/system.web.ui.control.createchildcontrols.aspx。 – 2010-09-25 15:52:28