我有一个名为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" />
有人有想法或想法的指针吗?
最佳答案
您要添加click事件,然后在此代码中将其删除
public event EventHandler Click
{
add{click += value; }
remove { click -= value; }
}
我认为您正在尝试删除对可能存在的click事件的引用,然后添加对您定义的click事件的引用。只需切换删除和添加,它应该可以工作。
public event EventHandler Click
{
remove { click -= value; }
add{click += value; }
}
关于c# - UserControl中的LinkButton回发但不会触发OnClick,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3514105/