i trying open pdf file in new tab in browser, open same tab.. iam using gridview template field open pdf..
how open pdf files in new tab in browser using gridview row command in asp.net c#
asp.net
<asp:gridview id="gvwpdf" runat="server" cssclass="mgrid" cellpadding="20" cellspacing="20" autogeneratecolumns="false" emptydatatext="no files uploaded" width="100%"> <columns> <asp:boundfield datafield="text" headertext="file name" /> <asp:templatefield> <itemtemplate> <asp:linkbutton id="lnkread" runat="server" text="✉ read" commandname="read" cssclass="gvwedit" forecolor="green" onclick="readpdffile" commandargument='<%# eval("value") %>'></asp:linkbutton> </itemtemplate> </asp:templatefield> </columns> </asp:gridview>
c#
protected void page_load(object sender, eventargs e) { try { if (!ispostback) { string[] filepaths = directory.getfiles(server.mappath("~/uploads/")); list<listitem> files = new list<listitem>(); foreach (string filepath in filepaths) { files.add(new listitem(path.getfilename(filepath), filepath)); } gvwpdf.datasource = files; gvwpdf.databind(); } } catch (exception ex) { //popmsg.pop(ex.message.tostring(), bmgate.webformusercontrols.common.messagebox.iconerror, "error"); scriptmanager.registerstartupscript(this, gettype(), "error message", "alert('" + ex.message.tostring() + "')", true); } } protected void readpdffile(object sender, eventargs e) { try { string path = (sender linkbutton).commandargument; webclient client = new webclient(); byte[] buffer = client.downloaddata(path); if (buffer != null) { response.contenttype = "application/pdf"; response.addheader("content-length", buffer.length.tostring()); response.binarywrite(buffer); clientscript.registerclientscriptblock(this.gettype(), "message", "window.open('application/pdf','_blank');", true); } } catch (exception ex) { scriptmanager.registerstartupscript(this, gettype(), "error message", "alert('" + ex.message.tostring() + "')", true); } }
help solve issue..
in linkbutton, set onclientclick this:
<asp:linkbutton id="lnkread" runat="server" text="✉ read" commandname="read" cssclass="gvwedit" forecolor="green" onclientclick="window.open('newpage.aspx?filename=<%# eval("value") %>', '_newtab');"></asp:linkbutton>
this open new tab pdf file name querystring (other solutions opening new tabs here). want change in current page_load
making value of listitem
file name not passing file path on url parameter.
in page_load
of newpage.aspx
(the new tab being opened), load pdf data. if receiving byte[]
webclient
, how write pdf:
string filename = request.querystring["filename"]; string path = path.combine(server.mappath("~/uploads/"), filename); webclient client = new webclient(); byte[] buffer = client.downloaddata(path); httpcontext.current.response.clear(); httpcontext.current.response.addheader("content-type", "application/pdf"); httpcontext.current.response.addheader("content-disposition", "inline; filename=" + filename); httpcontext.current.response.binarywrite(buffer); httpcontext.current.response.end();
let me know if works you. i've tested onclientclick
in linkbutton
, opened new tab successfully. separately, i've used response
code above in few different pages without issue.
Comments
Post a Comment