开发者

Adding an onmouseover attribute to an <asp:Panel> in a <asp:Repeater>

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开发者_如何学C 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.


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.


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


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.


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()");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜