开发者

LinkButton in UserControl posts back but does not fire OnClick

I have a UserControl named RolloverLink that basically contains an asp:LinkButton and an <img />. I tried setting the OnClick handler like this:

<%@ 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>

I put a breakpoint in the propagate method, but it doesnt stop there. The page does post back (it flashes), but the event doesn't get called.

Here's how my code behind looks:

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);
    }
}

and this is how I use it on my master page:

<pwc:RolloverLink ID="contacts" Href="Contacts.aspx" 
    ImageRl="images/M_contacts_rl.png" ImageUp="images/M_contacts_up.png" 
    Alt="Initech Global : Laurierville, Qu&eacute;bec, Canada" Width="61" 
    Height="17" runat="server" />

Does anyone have an idea or a po开发者_如何学运维inter to an idea?


You're adding the click event, then removing it in this code

public event EventHandler Click
{
    add{click += value; }
    remove { click -= value; }
}

I think you're trying to remove a reference to a click event that could be there and then adding a reference to the click event you are defining. Just switch around the remove & add and it should work.

public event EventHandler Click
{
    remove { click -= value; }
    add{click += value; }

}


This looks like a non-standard way of adding eventhandlers to a click event:

Have you tried the standard syntax:

  _mug.Click += new EventHandler(value);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜