ASP.Net ImageButton未触发onclick事件

3

我正在测试两个ImageButton控件,一个是在GridView_RowDataBound函数中以编程方式设置的,另一个是在客户端硬编码如下:

静态ImageButton(可用且触发ImageButton_Click):

<asp:ImageButton OnClick="ImageButton_Click" runat="server" ID="Foo" ImageUrl="images/edit-icon.png" />

动态 ImageButton(不起作用,不会触发 ImageButton_Click):

 protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
.
.
.

    ImageButton i = new ImageButton();
                        i.ImageUrl = "/images/edit-icon.png";
                        i.ID = "icon_" + testID.ToString();
                        i.ToolTip = "Click to add a new comment";
                        i.Click += new ImageClickEventHandler(ImageButton_Click);
                        e.Row.Cells[5].Controls.Add(i);
.
.
.

    }

动态 ImageButton 的呈现如下所示。
<input type="image" name="GridView1$ctl24$DGV1$ctl02$icon_1234" id="GridView1_ctl24_DGV1_ctl02icon_1234" title="Click to add a new comment" src="/images/edit-icon.png" onclick="ImageButton_Click;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;GridView1$ctl24$DGV1$ctl02$icon_1234&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" style="border-width:0px;">

我发现动态图像按钮中包含了"onclick",而静态图像按钮中有"OnClick",不确定是否有区别。我已经被这个问题卡了几个小时,不知道为什么动态ImageButton没有反应。请帮忙。
更新:
谢谢大家的反馈,我只需要处理ImageButton的ID,不必在服务器端进行操作。有没有办法可以在客户端使用JavaScript来实现这一点,以避免postback?我尝试重新配置ImageButton如下:
                      ImageButton i = new ImageButton();
                    i.ImageUrl = "/images/edit-icon.png";
                    i.ID = "icon_" + testID.ToString();
                    i.ToolTip = "Click to add a new comment";
                    i.OnClientClick = "submitComment(i.ID);return false;";
                    e.Row.Cells[5].Controls.Add(i);

我在我的网页表单的标题中指定了这个函数。

       <head runat="server">
            <script type="text/javascript">
             submitComment(i.ID){
             //Call WebMethod and pass comment along with this button's testID
}
    </script>
        </head>

但是当我点击编辑图标 ImageBtn 时,出现以下错误:
Uncaught Reference Error: icon_12345 is not defined.

这是怎么回事?如果编辑图标已经被呈现出来,而我只是使用客户端的Javascript,那么它们不应该已经被定义并能够被正确引用了吗?这对我来说似乎非常违反直觉。我有什么遗漏吗?除了ImageButton之外,还有其他控件可以用于渲染图像而无需担心PostBack,并且仍然可以调用JavaScript并传递其ID吗?使用普通的Image控件,将其onclick属性连接到submitComment函数会怎样?

1
我也遇到了这个问题。似乎在调用page_load函数之后,您无法为按钮添加EventHandler,因为.NET不会正确注册它们。您可以在Page_Init中添加它,但是然后您无法在RowDataBound函数中动态绑定它,这也是我想要做的。 - onit
Jay Douglass说得对,使用这种技术会带来痛苦和折磨。这可能并不是必要的。如果您在gv_RowDataBound()中提供更多代码,以显示为什么需要动态创建ImageButton,我可能会提出一种替代方法,在每次回发时都无需动态创建控件即可完成相同的任务。 - pseudocoder
4个回答

4
问题在于,在回传(postback)时,ASP.NET不会记住您在上一次请求中动态添加的控件。这个控件在控件树中根本不存在,因此事件也不会触发。
尝试动态添加控件时,您可能会遇到许多问题和烦恼。如果您希望该事件触发,则必须在 Page_Load 之前重新创建该控件。有关更多详细信息,请参见 http://forums.asp.net/t/1186195.aspx/1
我建议仅对不需要 ImageButton 的行进行隐藏。

在所有方面都同意,除了你假设 OP 动态创建控件是因为他只想让它出现在某些行中。可能还有其他原因。但基本概念是相同的,解决这个问题的最佳方法通常是使用模板化的 ImageButton、绑定表达式,并结合 GridView.RowCommand 中的 CommandNameCommandArgument 属性来使用。 - pseudocoder

1

每次回传都必须进行 DataBind 操作,以便将控件重新加入 ControlTree


0
也许你可以尝试这样做?只需使用数据绑定表达式为onclick属性创建普通的img元素。 "ID"必须是gridview数据源中的字段。
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <img src="/images/edit-icon.png" onclick="submitComment('<%# Eval("ID", "icon_{0}") %>');"></img>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

虽然未经测试,但我之前使用过类似的东西。ASP.NET在解析数据绑定表达式时可能会有点挑剔。


0
如果您的页面被路由,只需将此代码添加到masterpage的on_load或具有表单的页面上即可。
form1.Action = Request.Url.PathAndQuery;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接