我在 UserControl 中添加了一个动态 LinkButton。在回发时,会显示动态控件,但 Controls 集合的长度为 0。
namespace TestUC1
{
public partial class UC : System.Web.UI.UserControl
{
public event EventHandler Click;
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
foreach (Control c in Controls)
{
if (c is LinkButton)
{
((LinkButton)c).Click += new EventHandler(OnClick);
}
}
} else
{
AddNewButton();
}
}
protected void AddNewButton()
{
LinkButton lb = new LinkButton();
lb.ID = "TestLink";
lb.Text = "Test Link";
lb.Click += new EventHandler(OnClick);
Controls.Add(lb);
}
protected void OnClick(object sender, EventArgs e)
{
if (Click != null)
{
Click(this, new EventArgs());
}
}
}
}