asp.net按钮Onclick事件没有激活它的代码隐藏功能

时间:2015-01-10 10:27:46

标签: c# asp.net c#-4.0 c#-3.0

在asp.net页面中使用asp按钮。在代码后面将此按钮渲染为html字符串并分配给div。

<asp:ImageButton ID="ProImg" runat="server" CssClass="Photo" ImageUrl="~/images/ProfilePage/pic.jpg"
                    ImageAlign="Middle" OnClick="ProImg_Click" />

渲染场景是

Control MyApps = tabsUL.FindControl("MyApp");
string MyAppshtml = RenderControl(MyApps);
outerTabInfo.InnerHtml =  MyAppTxt;

并且rendercontrol函数是

private string RenderControl(Control control)
{
    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    HtmlTextWriter writer = new HtmlTextWriter(sw);

    control.RenderControl(writer);
    return sb.ToString();
}

但它实际上没有激发 ProgImg_Click 功能。功能是

protected void ProImg_Click(object sender, EventArgs e)
{
    //code process
}

我该如何制作....提前致谢

1 个答案:

答案 0 :(得分:0)

您需要附加programaticaly事件

private string RenderControl(Control control)
{
    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    HtmlTextWriter writer = new HtmlTextWriter(sw);
    if(control.Id=="ProImg"){
      control.Click += new System.EventHandler(this.ProImg_Click);

     }
    control.RenderControl(writer);
    return sb.ToString();
}