I have a Datalist control which display thumbnails and a download icon (ImageButton) under it, Datalist is wrapped under UpdatePanel, When user clicks on download icon I call the following function to allow file to be downloaded at user end
protected void dtlSearchDetails_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "dtlImgDownload")
{
downloadFile(e.CommandArgument.ToString(), "~/uploads/primary/");
}
}
function :
public void downloadFile(string fileName, string filePath)
{
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", fileName));
Response.WriteFile(filePath + fileName);
}
Now since the ImageButton
/ Datalist
is wrapped inside UpdatePanel
, I have registered it as a postback control with the ScriptManager
on Page_Load
if (dtlSearchDetails.Items.Count > 0)
{
foreach (DataListItem li in dtlSearchDetails.Items)
{
ImageButton img = (ImageButton)li.FindControl("dtlImgDownload");
ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(img);
}
}
Now when I click the download icon nothing happens and I get this following error in console :
Uncaught Sys.WebForms.PageRequestManagerParserErrorException: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
But,
There are other controls on the same page like search button, which are outside this UpdatePanel but I have registered them as
<trigger>
<asp:AsyncPostBackTrigger ControlID="btnKeySearch" />
</trigger>
inside the same updatePanel.
When I click on this search button and then click the download Icon it behaves perfectly fine as expected (download's the file at client) without and error in console.
I am not able to find out the cause behind this workaround.
Kindly help me out.
your ImageButton
is inside a DataList
so you have to do the RegisterPostBackControl
inside your DataList
's ItemDataBound
event.
protected void dtlSearchDetails_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var imgBtn = e.Item.FindControl("dtlImgDownload") as ImageButton;
ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(imgBtn);
// your other code goes here.
}
}