I am facing a challenge with a gridview that contains a list of pdf files. Whenever a user clicks on a pdf, it should display the file inline on the page. I have been trying to execute some javascript after the pdf has finished loading, but I'm encountering difficulties. The main issue seems to be that the pdf loads last, causing the load event to trigger before the pdf even begins loading.
Initially, I attempted to use an iframe solution where the inner page would fetch the file and write the data to the response. However, this approach also led to the same problem of the load event occurring prematurely. Currently, my code utilizes a generic handler (ashx) to load the pdf inline from the server side. How can I ensure that an event executes javascript only after the pdf data has been successfully loaded through the ashx generic handler?
Aspx page:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "View")
{
int index = Convert.ToInt32(e.CommandArgument.ToString());
string Id = GridView1.DataKeys[index].Value.ToString();
HtmlGenericControl myObject = new HtmlGenericControl();
myObject.TagName = "object";
Panel1.Controls.Add(myObject);
myObject.Attributes.Add("data", "GetPdf.ashx?Id=" + Id);
}
}
Generic handler ashx:
public void ProcessRequest(HttpContext context)
{
System.Diagnostics.Debug.WriteLine("GetPdf.ashx started");
string Id = context.Request.QueryString["Id"];
byte[] data = GetPdf(Id);
context.Response.ClearContent();
context.Response.ContentType = "application/pdf";
context.Response.AppendHeader("Content-disposition", "inline");
context.Response.AddHeader("Content-Length", data.Length.ToString());
context.Response.BinaryWrite(data);
System.Diagnostics.Debug.WriteLine("GetPdf.ashx is done");
context.Response.End();
}