Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Custom event from a custom web control is fired twice

Reply
Thread Tools

Custom event from a custom web control is fired twice

 
 
SammyBar
Guest
Posts: n/a
 
      07-14-2009
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.Onclic k,
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>

<cc1ownloadsList 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];

}




 
Reply With Quote
 
 
 
 
Erjan Gavalji
Guest
Posts: n/a
 
      07-21-2009
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" <(E-Mail Removed)> wrote in message
news:Ohp%(E-Mail Removed)...
> 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.Onclic k,
> 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>
>
> <cc1ownloadsList 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];
>
> }
>
>
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom event from a custom web control is fired twice SammyBar ASP .Net Web Controls 1 07-21-2009 07:16 PM
Embedded <divs> with events: How to prevent the parent div's eventfrom being fired when the embedded div's event is fired? Num GG Javascript 2 11-17-2008 08:56 PM
click event is fired twice on an enhanced Button Control Roberto Kohler ASP .Net 0 11-06-2007 10:22 PM
Event fired twice with custom control Daniel ASP .Net 0 03-04-2007 05:51 AM
onbeforeunload event is fired twice Rick Lubanovic ASP .Net 0 10-30-2003 08:55 PM



Advertisments