[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.buildingcontrols

Custom event from a custom web control is fired twice

SammyBar

7/14/2009 5:16:00 PM

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

}




1 Answer

Erjan Gavalji

7/21/2009 7:17:00 PM

0

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" <sammybar@gmail.com> wrote in message
news:Ohp%233rKBKHA.5092@TK2MSFTNGP03.phx.gbl...
> 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];
>
> }
>
>
>
>