我有一个名为RolloverLink的UserControl,基本上包含asp:LinkButton
和<img />
。我尝试像这样设置OnClick处理程序:
<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="RolloverLink.ascx.cs" Inherits="Controls.RolloverLink" %>
<asp:LinkButton runat="server" ID="mug" OnClick="propagate"
CausesValidation="false" onmouseout="MM_swapImgRestore()" >
<img runat="server" ID="pug" name="<%# pug.ClientID %>" border="0" />
</asp:LinkButton>
我在propagate
方法中放了一个断点,但它并没有就此止步。页面会回发(闪烁),但事件不会被调用。
以下是我背后的代码:
public partial class RolloverLink : System.Web.UI.UserControl
{
private string _imageRl;
public string Href { get; set; }
public string ImageUp { get { return pug.Src; } set { pug.Src = value; } }
public string ImageRl {
get
{
return _imageRl;
}
set
{
_imageRl = value;
mug.Attributes["onmouseover"] = "MM_swapImage('"+pug.ClientID+"','','"+_imageRl+"',1)";
}
}
public string Alt { get { return pug.Alt; } set { pug.Alt = value; } }
public int Width { get { return pug.Width; } set { pug.Width = value; } }
public int Height { get { return pug.Height; } set { pug.Height = value; } }
public event EventHandler Click
{
add{click += value; }
remove { click -= value; }
}
private event EventHandler click;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Url.AbsolutePath.EndsWith(Href))
ImageUp = ImageRl;
}
protected void propagate(object sender, EventArgs e)
{
EventHandler copy = click;
if (copy != null)
copy(this, EventArgs.Empty);
else
Response.Redirect(Href);
}
}
这就是我在母版页上使用它的方式:
<pwc:RolloverLink ID="contacts" Href="Contacts.aspx"
ImageRl="images/M_contacts_rl.png" ImageUp="images/M_contacts_up.png"
Alt="Initech Global : Laurierville, Québec, Canada" Width="61"
Height="17" runat="server" />
有没有人有想法或指向想法?
答案 0 :(得分:0)
您正在添加点击事件,然后在此代码中将其删除
public event EventHandler Click
{
add{click += value; }
remove { click -= value; }
}
我认为您正在尝试删除对可能存在的点击事件的引用,然后添加对您定义的Click事件的引用。只需切换移除&amp;添加,它应该工作。
public event EventHandler Click
{
remove { click -= value; }
add{click += value; }
}
答案 1 :(得分:0)
这看起来像是将事件处理程序添加到click事件的非标准方法:
您是否尝试过标准语法:
_mug.Click += new EventHandler(value);