活动不开火?

时间:2012-08-18 07:25:04

标签: c# asp.net

我想在代码后面创建一个图像按钮,如下所示:

ImageButton img1 = new ImageButton();
img1.CssClass = "stylImage";
img1.ImageUrl = @"~/images/Workflow/esign.jpg";
img1.AlternateText = "Signature";
img1.CommandName = "edit";
img1.Click += new ImageClickEventHandler(Image_OnClientClick);
img1.Command += new CommandEventHandler(Image_OnCommand);
workdesk.Controls.Add(img1);

按钮在页面上创建并显示,但没有点击或命令事件触发 也没有错误或异常。

有什么想法吗?!?

1 个答案:

答案 0 :(得分:1)

您的代码工作正常,并且在我测试时,两个事件都会完美地发生。

您可能没有正确指定Event Definition

请重新检查下面的代码,它就像魅力...... !!

protected void Page_Load(object sender, EventArgs e)
{
    ImageButton img1 = new ImageButton();
    img1.CssClass = "stylImage";
    img1.ImageUrl = @"~/images/Workflow/esign.jpg";
    img1.AlternateText = "Signature";
    img1.CommandName = "edit";
    img1.Click += new ImageClickEventHandler(Image_OnClientClick);
    img1.Command += new CommandEventHandler(Image_OnCommand);
    form1.Controls.Add(img1);

}

protected void Image_OnClientClick(object sender, ImageClickEventArgs e)
{
    //some code...
}

protected void Image_OnCommand(object sender, CommandEventArgs e)
{
    //some code...
}

这可能对你有帮助.. !!