asp:ImageButton 未触发 onclick 事件
我有一个页面,它使用母版页、几个RequiredFieldValidators 和Web Toolkit 自动完成扩展器。 以下代码仅显示页面的最低限度:
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Login.aspx.cs"
MasterPageFile="~/master.master"
Inherits="Login" %>
<asp:Content id="Content1"
contentplaceholderid="ContentPlaceHolder1"
runat="server">
<asp:UpdatePanel ID="pnlUpdate" runat="server">
<ContentTemplate>
<div>
<asp:ImageButton class="submitButton"
imageurl="images/button_submit.gif"
id="btnSubmit"
runat="server"
onclick="btnSubmit_ServerClick"/>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
代码隐藏:
protected void btnSubmit_ServerClick
(object sender, ImageClickEventArgs e)
{
//breakpoint here does not get hit
}
I have a page that uses a master page, several RequiredFieldValidators, and the Web Toolkit autocomplete extender. The following code only shows the bare minimum of the page:
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Login.aspx.cs"
MasterPageFile="~/master.master"
Inherits="Login" %>
<asp:Content id="Content1"
contentplaceholderid="ContentPlaceHolder1"
runat="server">
<asp:UpdatePanel ID="pnlUpdate" runat="server">
<ContentTemplate>
<div>
<asp:ImageButton class="submitButton"
imageurl="images/button_submit.gif"
id="btnSubmit"
runat="server"
onclick="btnSubmit_ServerClick"/>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Code-behind:
protected void btnSubmit_ServerClick
(object sender, ImageClickEventArgs e)
{
//breakpoint here does not get hit
}
The <form runat="server">
tag is in the master page. The code above does not fire the onclick event. If I get rid of the master page and add a form tag to the page, it works. Is the form tag in the master page not supported, or is this supposed to work somehow?
alt text http://digitalcopy.warnerbros.com/images/mainmenu.gif?provider=00079&disc=03403AAA-1D20-47F2-91FA-5EE632832659
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
您还可以检查您的 ImageButton 是否未触发验证。 如果它确实将 CausesValidation 属性设置为 false(当然,如果它有意义的话)。
You can also check if your ImageButton does not trigger validation. If it does set its CausesValidation property to false (of course if it makes sense).
我有一个类似的问题(不同的场景)。 我使用了
Page.RegisterRequiresRaiseEvent(ImageButton)
并且我的 onclick 事件开始触发。 为什么我需要这样做? 我不知道。I had a similar issue (different scenario). I used
Page.RegisterRequiresRaiseEvent(ImageButton)
and my onclick event started to fire. Why I needed to do that? I don't know.我的解决方案是将 ImageButton 的 CausesValidation 设置为 false。
My solution was to set the ImageButton's CausesValidation to false.
我对图像按钮也有类似的问题,并找到了根本原因。 您使用
“
ib.ID = i + ":" + j;
”作为
ImageButton
的 ID,“:
”是使用非法名称,因为您以编程方式创建它,ASP.NET 允许创建它。在运行时,如果您查看页面的 HTML 源代码,您将看到特殊字符要么被忽略,要么被替换为“
_
”。 因此页面无法找到正确的控件,因此事件不会触发。 尝试用纯文本更改名称,该事件将会触发。I have a similar issue with the image button and found the root cause. You are using
"
ib.ID = i + ":" + j;
"as the ID of the
ImageButton
, the ":
" is illegal name to use, as you are creating it programmatically, ASP.NET allows it to be created.At runtime, if you look at the HTML source of the page, you will see the special characters are either ignored or replaced with "
_
". So the page is unable to find the correct control, thus the event won't fire. Try changing the name with plain text, the event will fire.ib.ID = i + ":" + j;
应更改为
如果仍然不起作用,请尝试使用 StringBuilder 来构建 ID 并稍后将其分配给 ib.ID 属性
ib.ID = i + ":" + j;
should be changed to
If it still doesn't work try making use of the StringBuilder to buildup the ID and assign it later to ib.ID property
这是对我有用的解决方案
如果您通过数据绑定控件进行绑定,则使用 OnCommand attr 而不是 OnClick attr
This is solution that worked for me
If you are binding through data bound controls then use OnCommand attr instead of OnClick attr
您必须在某处以 runat=server 的形式拥有控件,它可以位于母版页或 .aspx 文件中。 仔细检查母版页表单标记是否 runat=server
AutoEventWireup 是允许您正在使用的语法的属性。 仔细检查母版页、WebForm 中的设置,也可以在 web.config 中设置。
如果这不起作用,您总是可以对其进行显式编码(我更喜欢)
UpdatePanel 也可能会引发服务器端事件,因此请尝试不使用 UpdatePanel。 我确信您在母版页中有一个 ScriptManager
You have to have the control in a form runat=server somewhere, it can be in the Master page or the .aspx file. Double check that the master page form tag is runat=server
AutoEventWireup is the property that allows the syntax you are using. Double check the setting in the Master Page, WebForm and it can also be set in the web.config.
if that doesnt work, you can always explicilty code it (which I prefer)
UpdatePanel can mess with server side events being raised also, so try it without the UpdatePanel. And I am sure you have a ScriptManager in the Master Page
从您提供的代码来看,您的页面似乎缺少
。 您必须执行以下操作之一:
,在母版页上放置
。
,但母版页上没有
。就我个人而言,我建议在母版页上使用
From the code you supplied, you seem to be missing the
<asp:scriptmanager>
from your page. You must do one of the following:<asp:scriptmanagerproxy>
on the page and the<asp:scriptmanager>
on the master page.<asp:scriptmanager>
on your page and no<asp:scriptmanager>
on the master page.Personally, I recommend having the
<form>
tag on the master page, but that's personal preference.您可以随时尝试取出 UpdatePanel 并查看它是否有效。 我通常在不使用 UpdatePanel 的情况下开始,让一切按照我想要的方式工作,然后添加到 UpdatePanel 中并调试导致的任何问题。
MasterPage 中的表单对我有用,因此@Keltex 提到的 ScriptManager/ScriptManagerProxy 可能是一个问题,尽管我有时会忘记它们并且通常会逃脱它。
使用 UpdatePanel,按钮的单击事件将通过 Javascript 进行处理,因此您可以使用 FireBug 或等效工具(取决于浏览器)并跟踪实际发生的情况。 是否在验证时出错而您没有看到它? 某处是否存在 JS 错误(控制工具包并不总是完美的)? 该页面是否实际上完全回发并且只是没有命中事件处理程序?
You can always try taking out the UpdatePanel and seeing if it works. I usually start without the UpdatePanel, get everything working the way I want and then add in the UpdatePanel and debug anything that causes.
The form in the MasterPage works for me so the ScriptManager/ScriptManagerProxy mentioned by @Keltex might be an issue, though I forget them sometimes and usually get away with it.
With the UpdatePanel the button's click event will be handled via Javascript, so you might grab FireBug or equivalent (depending on browser) and follow through what actually is happening. Is it tripping on the validation and you don't see it? Is there a JS error somewhere (the Control Toolkit isn't perfect always)? Is the page actually posting back at all and just not hitting the event handler?
在我的网页上,我正在更新面板包含的表格内动态创建图像按钮。 这些按钮是由以下代码创建的:
}
图像按钮不会触发点击事件。 但是,如果“ib.ID = ...”行被注释掉,它们就会被注释掉! 这一次的交替似乎解决了所有问题。
我不知道为什么。
如果有人能解释这一点,并告诉我如何触发事件并保持设置按钮 ID 的能力,我将非常感激
On my webpage i am creating imagebuttons dynamically inside a table that is contained by an updatepanel. The buttons are created by this code:
}
The image buttons will not trigger click events. HOWEVER, if the line 'ib.ID = ...' is commented out, they do! That single alternation seems to fix all the issues.
I have no idea why.
If anyone can explain this, and also tell me how to trigger events keeping the ability to set button id's, i'd be much thankful
我认为这可能与您在创建 & 之后重新分配 id 有关。 分配事件处理程序?
您甚至需要分配 id 吗? - 这肯定是为你做的吧? - True 删除 'ib.ID = i + ":" + j;'
I think it may have something to do with the fact your re-assigning the id's after creating & assigning the event handler?
Do you even need to assign the id's? - Surely this is done for you anyway? - True removing the 'ib.ID = i + ":" + j;'
确保使用 OnClick 而不是 onClick
您的更新面板可能会干扰回发。 在没有 UpdatePanel 的情况下尝试一下,看看这是否是罪魁祸首。
Make sure you use OnClick rather than onClick
Your update panel could be messing with the postback. Try it without the UpdatePanel and see if that is the culprit.
我遇到了同样的问题,ImageButton 的 OnClick 事件未触发。 但实际的问题是,在表单级别,我有
onSubmit="return false;"
I had the same problem that
OnClick
event of ImageButton was not firing. But the actual problem was, at form level, I hadonSubmit="return false;"
我遇到了同样的问题,我动态创建一个 ImageButton 并且单击该事件没有触发。 因此,我在 if (IsPostBack) 中创建了图像按钮。 现在一切正常。 即使页面刷新,ImageButton 也会被保留。
I was facing the same issue, where I was dynamically creating an ImageButton and click of that event was not triggering. hence, i created the Image button in if (IsPostBack) . Now it is working fine. And even if the page gets refresh, the ImageButton will be retained.
我刚刚解决了启用 OutputCache 的类似问题。 从
asp:ImageButton
更改为asp:Button
时,该事件被正确触发。 可能asp:ImageButton
在 OutputCache 方面存在一些错误。I've just solved a similar issue where OutputCache was enabled. When changing from
asp:ImageButton
toasp:Button
, the event is correctly fired. Probablyasp:ImageButton
has some bug with OutputCache.在上述建议对我不起作用之后,我通过调用 OnInit() 中的按钮创建进行了一次尝试。 这解决了我的问题,现在 OnClick 事件正在触发。
After none of the above suggestions worked for me, I did one more try by calling the button creation in OnInit(). This fixed my issue and now the OnClick event is firing.