将 onmouseover 属性添加到

发布于 2024-10-17 18:50:43 字数 651 浏览 4 评论 0原文

我有一个包含几个面板的中继器。 我注意到 没有 onmouseover 属性。 我读过,我可以通过调用 page_load 将属性添加到面板中:

PanelID.Attributes.Add("onmouseover", "Script to Run");

问题是我在转发器中,PanelID 是由 asp.net 生成的,如 ContentPlaceHolder_ctl01_myID_0 所以不仅很难弄清楚,而且VS2010 无法将其识别为正确的对象并引发错误,而且我必须将其附加到每个项目上,因此我需要使用 for 或 foreach,但我不知道要迭代什么。 有没有类似

foreach(childcontrol in Repeater.ChildControlsISpecificallyNeedPossiblyIdentifiedBySomeID)
{
 childcontrol.Attributes.Add("onmouseover", "Script to Run");
}

在 C# 中的 Page_Load 事件处理程序中执行此操作的方法? 我也想在面板上运行客户端 onload、onmouseclick 和 onmouseout,所以我也想附加这些属性。

I have a repeater containing several several panels.
I have noticed that there is no onmouseover attribute of the <asp:Panel>.
I have read that I can add the attribute to the panel by calling in the page_load:

PanelID.Attributes.Add("onmouseover", "Script to Run");

The problem is that I am in a repeater, and the PanelID is generated by asp.net like ContentPlaceHolder_ctl01_myID_0 so not only it is hard to figure out, but VS2010 does not recognise it as a proper object and throws an error on it, plus I have to attach it on every item, so I need to use a for or foreach, but I don't know what to iterate on.
Is there a way like

foreach(childcontrol in Repeater.ChildControlsISpecificallyNeedPossiblyIdentifiedBySomeID)
{
 childcontrol.Attributes.Add("onmouseover", "Script to Run");
}

to do this in C# in the Page_Load eventhandler?
I want to run a client side onload, onmouseclick and onmouseout on the panels too, so I want to attach those attributes too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

网白 2024-10-24 18:50:43

跳出框框思考,您可以在 javascript 中附加 javascript 事件侦听器,而不是使用 ASP.NET 内联指定 onmouseover 属性。

为面板提供一个 CSS 类:

<asp:Panel CssClass="panel"></asp:Panel>

使用 jQuery,绑定事件处理程序的语法如下:

$(".panel").mouseover(functionToRun);

如果您不使用 jQuery,则需要更多代码,但我相信您已经明白了。

Thinking outside the box, instead of specifying the onmouseover-attribute inline using ASP.NET, you can attach the javascript event listeners in javascript.

Give the panel a CSS class:

<asp:Panel CssClass="panel"></asp:Panel>

Using jQuery, the syntax to bind the event handlers would be as follows:

$(".panel").mouseover(functionToRun);

If you're not using jQuery, you'll need a bit more code, but I'm sure you get the idea.

堇色安年 2024-10-24 18:50:43

面板在客户端用 div 表示,您可以使用 SomePanel.ClientID 获取客户端 ID,但是如果您指的是触发面板,您始终可以使用 this

不会将所有事件处理程序放在每个元素上

panel is represented with div on client side, you can get the client side id by using SomePanel.ClientID however if you are referring to the triggering panel you can always use this in the JavaScript

would not put all the event handlers on each element

轻拂→两袖风尘 2024-10-24 18:50:43

在repeater onitemdatabound 事件中将属性添加到面板,并在其中找到控件,因为每次绑定行时都会调用此事件,这样您就不必为each 调用它。

Add the attribute to the panel in the event of repeater onitemdatabound and find the control in that, as this event will called for each and every time the row is bound so that you dont have to call it foreach.

瘫痪情歌 2024-10-24 18:50:43

您可以考虑使用 ItemDataBound 事件。

protected void myRepeater_ItemDataBound(object source, RepeaterCommandEventArgs e) {   
    if(e.Item.ItemType == ListItemType.Item){
       Panel pnl = (Panel)e.Item.FindControl("myPanel");   
       pnl.Attributes.Add"onMouseOver", "yourFnctionName()");
    }
}

You may consider using ItemDataBound event.

protected void myRepeater_ItemDataBound(object source, RepeaterCommandEventArgs e) {   
    if(e.Item.ItemType == ListItemType.Item){
       Panel pnl = (Panel)e.Item.FindControl("myPanel");   
       pnl.Attributes.Add"onMouseOver", "yourFnctionName()");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文