Click here to Skip to main content
11,922,416 members (30,993 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# PDF
I m trying to display the pdf files in list box and on clicking on the file (on selected index changed) i want to open that particular file.
But i m nt able to do so as i m getting the single file(Clicked first) on click of any link.
string var = txtpan.Text;
ListItem list = new ListItem();
DirectoryInfo dinfo = new DirectoryInfo(@"C:\vrishali\PDFs\");
/* get files from the directory as a files collection */
FileInfo[] finfo = dinfo.GetFiles(string.Format("{0}*.pdf",var));
for (int i = 0; i < finfo.Length; i++)
  list.Value = finfo[i].ToString();
protected void lstpdfs_SelectedIndexChanged(object sender, EventArgs e)
string filePath = @"C:\folder\PDFs\"+ lstpdfs.SelectedValue;
Session["pdfFileName"] = filePath ;
Response.Write("<script type='text/javascript'>'../PdfDisplay.aspx');</script>"); }

ON PDFDISPLAY.ASPX I have coded as
string path = Session["pdfFileName"].ToString();
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);
Response.AddHeader("content-length", buffer.Length.ToString());
This is my code. I am not able to clear the response stream as i am getting first clicked file on click of any other link.
please help
Posted 27-Jun-12 21:28pm
Edited 27-Jun-12 21:31pm
digimanus 28-Jun-12 3:06am
set the content-disposition header too

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Might be a caching issue. You could try setting the response content expires or caching settings or you can try and append a random number or Guid to the querystring e.g. ?c={Guid.NewGuid()} so that the browser will have to request for new content (might be needed for the PdfDisplay.aspx in your javascript since that url doesn't change I imagine). Not sure if that is what it is or if you have an issue with the list containing the wrong values. I imagine it is the browser caching though.

Also, looking at your selectedindex changed, I would imagine instead of response.write you might want a placeholder that you can update that javascript into instead of just sending back to the response stream. Have you looked at your html source after a few postbacks and make sure it isn't just adding the javascript again and again?
Member 8683058 10-Jul-12 4:25am
Thanks Trak4Net769 for ur soln. it helped

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 453
1 Dave Kreskowiak 335
2 Sergey Alexandrovich Kryukov 263
3 Richard MacCutchan 225
4 ppolymorphe 175
0 OriginalGriff 7,231
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,356
3 George Jonsson 2,865
4 Suvendu Shekhar Giri 2,216

Advertise | Privacy | Mobile
Web04 | 2.8.151120.1 | Last Updated 28 Jun 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100