如何在运行时向图像按钮添加 onclick 或 mouseover 事件

发布于 2024-10-15 08:52:46 字数 538 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

极度宠爱 2024-10-22 08:52:46

从您的评论中,我假设您正在做类似的事情:

void Page_Load(object s, EventArgs e)
{
    ImageButton img = new ImageButton();
    img.click += new ImageClickEventHandler(imageButton_Click);
    Controls.Add(img);
}

这不起作用,因为 PostBack 机制将控件的 Id 发送回 EventTarget 字段,并且未指定目标,因此它不知道在哪里处理它。

为了使其正常工作,我通常将显示包装在自定义用户控件中并重写 CreateChildControls() 方法。这可确保控件在正确的时间创建,并且在回发处理完成后可用。

From your comment I am assuming you are doing something like:

void Page_Load(object s, EventArgs e)
{
    ImageButton img = new ImageButton();
    img.click += new ImageClickEventHandler(imageButton_Click);
    Controls.Add(img);
}

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.

心凉怎暖 2024-10-22 08:52:46

您是否正在尝试启动代码隐藏事件?或者 JavaScript 事件?如果您尝试向元素添加 JavaScript 事件,则应该添加该属性。当您使用:Attributes["onclick"] 时,它当前不存在。所以它会是这样的:

imgProductImage.Attributes.Add("onclick","SetProductImage('" + imgProductImage.ID + "')");

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:

imgProductImage.Attributes.Add("onclick","SetProductImage('" + imgProductImage.ID + "')");
︶ ̄淡然 2024-10-22 08:52:46

好的。我想我发现了你想要实现的目标。您必须在 Page_Load 中显式设置控件的 ID:

ImageButton img = new ImageButton();
img.ID = "image1";
img.ImageUrl = "YOUR_IMAGE_URL";
img.OnClientClick = "SetProductImage('" + img.ID + "');return false;";
Controls.Add(img);  

只要确保您是否必须定义多个图像按钮,请为每个按钮设置唯一的 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:

ImageButton img = new ImageButton();
img.ID = "image1";
img.ImageUrl = "YOUR_IMAGE_URL";
img.OnClientClick = "SetProductImage('" + img.ID + "');return false;";
Controls.Add(img);  

Just make sure if you have to define multiple image buttones, Set a unique ID for each of them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文