c# - View pdf file in another page -


here have code view pdf file , file can viewed in same page.i want display file in new page when linkbutton clicked in gridview. understood literal embed link needs changed not sure that. question how view file in new page?

gridview code:

<asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" width="100%" cellpadding="4" forecolor="#333333" gridlines="none">         <alternatingrowstyle backcolor="white" />         <columns>             <asp:boundfield datafield="comname" headertext="company name" sortexpression="comname" />             <asp:boundfield datafield="sname" headertext="stock name" sortexpression="sname" />             <asp:boundfield datafield="anndate" headertext="date announced" sortexpression="anndate" />             <asp:boundfield datafield="fyearend" headertext="financial year end" sortexpression="fyearend" />             <asp:boundfield datafield="quarterr" headertext="quarter" sortexpression="quarterr" />             <asp:boundfield datafield="qfredate" headertext="financial period ended " sortexpression="qfredate" />             <asp:boundfield datafield="figure" headertext="figure" sortexpression="figure" />             <asp:templatefield itemstyle-horizontalalign="center">         <itemtemplate>             <asp:linkbutton id="lnkview" runat="server" text="view" onclick="view" commandargument='<%# eval("id") %>'></asp:linkbutton>         </itemtemplate> 

code behind view:

 protected void view(object sender, eventargs e)     {         int id = int.parse((sender linkbutton).commandargument);         string embed = "<object data=\"{0}{1}\" type=\"application/pdf\" width=\"100%\" height=\"100%\">";         embed += "if unable view file, can download <a href = \"{0}{1}&download=1\">here</a>";         embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">adobe pdf reader</a> view file.";         embed += "</object>";         ltembed.text = string.format(embed, resolveurl("~/filecs.ashx?id="), id);     } 

filecs.ashx code:

 public void processrequest(httpcontext context) {     int id = int.parse(context.request.querystring["id"]);     byte[] bytes;     string filename, contenttype;     string constr = configurationmanager.connectionstrings["connection"].connectionstring;     using (sqlconnection con = new sqlconnection(constr))     {         using (sqlcommand cmd = new sqlcommand())         {             cmd.commandtext = "select  fname, contenttype, data announ id=@id";             cmd.parameters.addwithvalue("@id", id);             cmd.connection = con;             con.open();             using (sqldatareader sdr = cmd.executereader())             {                 sdr.read();                 bytes = (byte[])sdr["data"];                 contenttype = sdr["contenttype"].tostring();                 filename = sdr["fname"].tostring();             }             con.close();         }     }      context.response.buffer = true;     context.response.charset = "";     if (context.request.querystring["download"] == "1")     {         context.response.appendheader("content-disposition", "attachment; filename=" + filename);     }     context.response.cache.setcacheability(httpcacheability.nocache);     context.response.contenttype = "application/pdf";     context.response.binarywrite(bytes);     context.response.flush();     context.response.end(); } 

in link button add onclientclick="aspnetform.target ='_blank';"

<asp:linkbutton id="lnkview" runat="server" text="view" onclick="view" onclientclick="aspnetform.target ='_blank';" commandargument='<%# eval("id") %>'></asp:linkbutton> 

this open new tab.


Comments