S
SammyBar
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>
<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];
}
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>
<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];
}