如何在运行时向图像按钮添加 onclick 或 mouseover 事件
ive imagebuttons 是在运行时从数据库创建的,我需要在单击或鼠标悬停事件上添加它们,但我
imagebutton.click += new ImageClickEventHandler(imageButton_Click);
在创建 imagebuttons 时添加失败,我还需要什么,
我也尝试了 javascript 但它也没有触发;
<script type="text/javascript">
function SetProductImage(imgID) {
document.getElementById('imgProduct').src=document.getElementById(imgID).src;
}
</script>
imgProductImage.Attributes["onclick"] = "javascript:SetProductImage('" + imgProductImage.ID + "')";
谢谢...
ive imagebuttons which created in runtime from database and i need to add them onclick or mouseover event but i failed
imagebutton.click += new ImageClickEventHandler(imageButton_Click);
im adding this when i created imagebuttons, what i need else
also i tried javascript but its not firing too;
<script type="text/javascript">
function SetProductImage(imgID) {
document.getElementById('imgProduct').src=document.getElementById(imgID).src;
}
</script>
imgProductImage.Attributes["onclick"] = "javascript:SetProductImage('" + imgProductImage.ID + "')";
thank you...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从您的评论中,我假设您正在做类似的事情:
这不起作用,因为 PostBack 机制将控件的 Id 发送回 EventTarget 字段,并且未指定目标,因此它不知道在哪里处理它。
为了使其正常工作,我通常将显示包装在自定义用户控件中并重写
CreateChildControls()
方法。这可确保控件在正确的时间创建,并且在回发处理完成后可用。From your comment I am assuming you are doing something like:
This won't work because the PostBack mechanism sends the Id of the control back in the EventTarget field and the target isn't specified so it doesn't know where to handle it.
To get this to work properly I usually wrap the display in a custom user control and override the
CreateChildControls()
method. This ensures that the control is created at the correct time and is available when the postback handling is done.您是否正在尝试启动代码隐藏事件?或者 JavaScript 事件?如果您尝试向元素添加 JavaScript 事件,则应该添加该属性。当您使用:Attributes["onclick"] 时,它当前不存在。所以它会是这样的:
Are you trying to fire up a code behind event? or a javascript event? If you are trying to add a javascript event to your element, you should Add the attribute. It's not currently there when you use: Attributes["onclick"]. So It'd be like:
好的。我想我发现了你想要实现的目标。您必须在 Page_Load 中显式设置控件的 ID:
只要确保您是否必须定义多个图像按钮,请为每个按钮设置唯一的 ID。
Ok. I think I found out what you are trying to achieve. You have to explicitly set the ID of the control in Page_Load:
Just make sure if you have to define multiple image buttones, Set a unique ID for each of them.