我在控件的CodeBhind中创建了一个ASP按钮。这是代码:
Button SubmitButton = new Button();
protected override void CreateChildControls()
{
SubmitButton.Text = "Submit";
SubmitButton.Click += new EventHandler(SubmitButton_Click);
}
private void SubmitButton_Click(object sender, EventArgs e)
{
CustomTabs.CreateNewTab();
}
但是,Click事件不会触发。它看起来像是一个回发,从来没有击中事件。有什么想法吗?
答案 0 :(得分:2)
因此,您有一些未解释的方法,但是您是否在页面中添加了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
}
}
}