Custom event from a custom web control is fired twice

Discussion in 'ASP .Net' started by SammyBar, Jul 14, 2009.

  1. SammyBar

    SammyBar Guest

    Hi all,

    I created a custom control that fires a custom event, but when it is fired,
    the event is received twice...
    I have cooked this control from different sources on the Internet an I fear
    I'm missing something.
    What can be wrong?

    It follows a resumed version of the control. It displays a list of files.
    This is rendered as a serie of <input type="image">. When the user clicks on
    an image an event is sent back to server with the key of the Dictionary
    entry selected. This is the event that gets fired twice, I don't know why..

    Any hint is welcomed
    Thanks in advance
    Sammy
    public class DownloadsList : WebControl, IPostBackEventHandler

    {

    // this is the custom argument to the custom event. It only defines a
    property: DownloadKey

    public class DownloadClickEventArgs : EventArgs

    {

    private string downloadKey;

    public DownloadClickEventArgs() { }

    public DownloadClickEventArgs(string aDownloadKey) { downloadKey =
    aDownloadKey; }

    public string DownloadKey

    {

    get { return downloadKey; }

    set { downloadKey = value; }

    }

    }

    // Here the delegate is defined, then declared

    public delegate void DownloadClickEventHandler(object sender,
    DownloadClickEventArgs e);

    public event DownloadClickEventHandler DownloadClick;

    // This is the property through which the data is passed to the control

    public Dictionary<string, Download> DownloadsDictionary

    {

    get

    {

    return ViewState["DownloadsDictionary"] as Dictionary<string, Download>;

    }


    set

    {

    ViewState["DownloadsDictionary"] = value;

    }

    }

    // Now the rendering

    protected override void RenderContents(HtmlTextWriter output)

    {

    foreach (Download download in DownloadsDictionary.Values)

    {

    ClientScriptManager cs = Page.ClientScript;

    output.AddAttribute(HtmlTextWriterAttribute.Onclick,
    cs.GetPostBackEventReference(this, download.Key));

    output.AddAttribute(HtmlTextWriterAttribute.Type, "image");

    output.AddAttribute(HtmlTextWriterAttribute.Src,
    ResolveClientUrl(download.DownloadMetadata.IconUrl));

    output.RenderBeginTag(HtmlTextWriterTag.Input);

    }

    }

    public void RaisePostBackEvent(string eventArgument)

    {

    // Putting a breakpoint here it is fired twice

    if (this.DownloadClick != null)

    this.DownloadClick(this, new DownloadClickEventArgs(eventArgument));

    }

    }

    //-- the Page ----

    <form id="form1" runat="server">

    <div>

    <cc1:DownloadsList ID="DownloadsList1" runat="server"

    OnDownloadClick="DownloadsList1_DownloadClick"

    />

    </div>

    </form>

    //--- The Code behind ---

    protected void Page_Load(object sender, EventArgs e)

    {

    // A breakpoint here is hitted twice

    if (!IsPostBack)

    {

    ClientFilesSvc clientFilesSvc = new ClientFilesSvc();

    downloads = clientFilesSvc.getDownloads(800455);

    DownloadsList1.DownloadsDictionary = downloads;

    }

    }

    protected void DownloadsList1_DownloadClick(object sender,
    DownloadsList.DownloadClickEventArgs e)

    {

    // A breakpoint here is also hitted twice

    string key = e.DownloadKey;

    Download d = DownloadsList1.DownloadsDictionary[key];

    }
    SammyBar, Jul 14, 2009
    #1
    1. Advertising

  2. Hi Sammy,

    An event thrown twice could be caused by the AutoEventWireUp="true"
    declaration. You have the event handler already defined in the ASPX code.

    Cheers,
    Erjan

    "SammyBar" <> wrote in message
    news:Ohp%...
    > Hi all,
    >
    > I created a custom control that fires a custom event, but when it is
    > fired, the event is received twice...
    > I have cooked this control from different sources on the Internet an I
    > fear I'm missing something.
    > What can be wrong?
    >
    > It follows a resumed version of the control. It displays a list of files.
    > This is rendered as a serie of <input type="image">. When the user clicks
    > on an image an event is sent back to server with the key of the Dictionary
    > entry selected. This is the event that gets fired twice, I don't know
    > why..
    >
    > Any hint is welcomed
    > Thanks in advance
    > Sammy
    > public class DownloadsList : WebControl, IPostBackEventHandler
    >
    > {
    >
    > // this is the custom argument to the custom event. It only defines a
    > property: DownloadKey
    >
    > public class DownloadClickEventArgs : EventArgs
    >
    > {
    >
    > private string downloadKey;
    >
    > public DownloadClickEventArgs() { }
    >
    > public DownloadClickEventArgs(string aDownloadKey) { downloadKey =
    > aDownloadKey; }
    >
    > public string DownloadKey
    >
    > {
    >
    > get { return downloadKey; }
    >
    > set { downloadKey = value; }
    >
    > }
    >
    > }
    >
    > // Here the delegate is defined, then declared
    >
    > public delegate void DownloadClickEventHandler(object sender,
    > DownloadClickEventArgs e);
    >
    > public event DownloadClickEventHandler DownloadClick;
    >
    > // This is the property through which the data is passed to the control
    >
    > public Dictionary<string, Download> DownloadsDictionary
    >
    > {
    >
    > get
    >
    > {
    >
    > return ViewState["DownloadsDictionary"] as Dictionary<string, Download>;
    >
    > }
    >
    >
    > set
    >
    > {
    >
    > ViewState["DownloadsDictionary"] = value;
    >
    > }
    >
    > }
    >
    > // Now the rendering
    >
    > protected override void RenderContents(HtmlTextWriter output)
    >
    > {
    >
    > foreach (Download download in DownloadsDictionary.Values)
    >
    > {
    >
    > ClientScriptManager cs = Page.ClientScript;
    >
    > output.AddAttribute(HtmlTextWriterAttribute.Onclick,
    > cs.GetPostBackEventReference(this, download.Key));
    >
    > output.AddAttribute(HtmlTextWriterAttribute.Type, "image");
    >
    > output.AddAttribute(HtmlTextWriterAttribute.Src,
    > ResolveClientUrl(download.DownloadMetadata.IconUrl));
    >
    > output.RenderBeginTag(HtmlTextWriterTag.Input);
    >
    > }
    >
    > }
    >
    > public void RaisePostBackEvent(string eventArgument)
    >
    > {
    >
    > // Putting a breakpoint here it is fired twice
    >
    > if (this.DownloadClick != null)
    >
    > this.DownloadClick(this, new DownloadClickEventArgs(eventArgument));
    >
    > }
    >
    > }
    >
    > //-- the Page ----
    >
    > <form id="form1" runat="server">
    >
    > <div>
    >
    > <cc1:DownloadsList ID="DownloadsList1" runat="server"
    >
    > OnDownloadClick="DownloadsList1_DownloadClick"
    >
    > />
    >
    > </div>
    >
    > </form>
    >
    > //--- The Code behind ---
    >
    > protected void Page_Load(object sender, EventArgs e)
    >
    > {
    >
    > // A breakpoint here is hitted twice
    >
    > if (!IsPostBack)
    >
    > {
    >
    > ClientFilesSvc clientFilesSvc = new ClientFilesSvc();
    >
    > downloads = clientFilesSvc.getDownloads(800455);
    >
    > DownloadsList1.DownloadsDictionary = downloads;
    >
    > }
    >
    > }
    >
    > protected void DownloadsList1_DownloadClick(object sender,
    > DownloadsList.DownloadClickEventArgs e)
    >
    > {
    >
    > // A breakpoint here is also hitted twice
    >
    > string key = e.DownloadKey;
    >
    > Download d = DownloadsList1.DownloadsDictionary[key];
    >
    > }
    >
    >
    >
    >
    Erjan Gavalji, Jul 21, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Lubanovic

    onbeforeunload event is fired twice

    Rick Lubanovic, Oct 30, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    815
    Rick Lubanovic
    Oct 30, 2003
  2. Anup
    Replies:
    2
    Views:
    906
    Anup Daware
    Aug 8, 2006
  3. Daniel
    Replies:
    0
    Views:
    438
    Daniel
    Mar 4, 2007
  4. Roberto Kohler
    Replies:
    0
    Views:
    509
    Roberto Kohler
    Nov 6, 2007
  5. SammyBar

    Custom event from a custom web control is fired twice

    SammyBar, Jul 14, 2009, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    922
    Erjan Gavalji
    Jul 21, 2009
Loading...

Share This Page