我有一个简单的webpart,代码如下,但是当我点击“UnAcceptClick”中的按钮代码时无效。
我错过了什么或做错了什么?
public class simple_wp : WebPart
{
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
CreateControlHierarchy();
}
private void CreateControlHierarchy()
{
unAccept.Text = "Cancel";
unAccept.Click += new EventHandler(UnAcceptClick);
... some other code ... }
private void UnAcceptClick(object sender, EventArgs e)
{
... some code ...
}
protected override void CreateChildControls()
{
try
{
Controls.Add(unAccept); // button
}
catch (Exception ex)
{
Controls.Add(new LiteralControl(ex.ToString()));
}
} // # CreateChildControls #
}
答案 0 :(得分:1)
在CreateChildControls
中添加此内容,您在init方法中关联而不是在呈现应用程序
protected override void CreateChildControls()
{
try
{
unAccept.Click += new EventHandler(UnAcceptClick);
Controls.Add(unAccept); // button
}
catch (Exception ex)
{
Controls.Add(new LiteralControl(ex.ToString()));
}
} //