将 onmouseover 属性添加到在中
我有一个包含几个面板的中继器。 我注意到
没有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
跳出框框思考,您可以在 javascript 中附加 javascript 事件侦听器,而不是使用 ASP.NET 内联指定
onmouseover
属性。为面板提供一个 CSS 类:
使用 jQuery,绑定事件处理程序的语法如下:
如果您不使用 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:
Using jQuery, the syntax to bind the event handlers would be as follows:
If you're not using jQuery, you'll need a bit more code, but I'm sure you get the idea.
面板在客户端用
div
表示,您可以使用SomePanel.ClientID
获取客户端 ID,但是如果您指的是触发面板,您始终可以使用this
不会将所有事件处理程序放在每个元素上
panel is represented with
div
on client side, you can get the client side id by usingSomePanel.ClientID
however if you are referring to the triggering panel you can always usethis
in the JavaScriptwould not put all the event handlers on each element
在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.
您可以考虑使用 ItemDataBound 事件。
You may consider using ItemDataBound event.