asp.net - How to download image/pdf file from database using Gridview's linkbutton? -


here problem. have user control download binary file (image, pdf etc.) using link button in gridview.

<asp:gridview visible="true" id="gridview1" runat="server" headerstyle-backcolor="#3ac0f2" headerstyle-forecolor="white" rowstyle-backcolor="#a1dcf2" alternatingrowstyle-backcolor="white" alternatingrowstyle-forecolor="#000" autogeneratecolumns="false"> <columns>             <asp:boundfield datafield="id" headertext="id"/>     <asp:boundfield datafield="filename" headertext="file name"/>     <asp:templatefield headertext="action" itemstyle-horizontalalign = "center">         <itemtemplate>          <asp:linkbutton id="lnkdownload" runat="server" text="download" onclick="downloadfile" commandargument='<%# eval("id") %>'></asp:linkbutton>          <asp:linkbutton id="lnkdelete" runat="server" text="delete" onclick="deletefile"  commandargument='<%# eval("id") %>'></asp:linkbutton>         </itemtemplate>     </asp:templatefield> </columns> </asp:gridview> 

code behind:

protected sub downloadfile(sender object, e eventargs)     dim id integer = integer.parse(trycast(sender, linkbutton).commandargument)     dim bytes byte()     dim filename string, contenttype string      using con new sqlconnection(datasource.connectionstring)         using cmd new sqlcommand()             cmd.commandtext = "select filename, patimage, filetype db id=@id"             cmd.parameters.addwithvalue("@id", id)             cmd.connection = con             con.open()             using sdr sqldatareader = cmd.executereader()                 sdr.read()                 bytes = directcast(sdr("patimage"), byte())                 contenttype = sdr("filetype").tostring()                 filename = sdr("filename").tostring()             end using             con.close()         end using     end using     response.clear()     response.buffer = true     response.charset = ""     response.cache.setcacheability(httpcacheability.nocache)     response.contenttype = contenttype     response.appendheader("content-disposition", "attachment; filename=" + filename)     response.binarywrite(bytes)     response.flush()     response.end() end sub 

but not working if have updatepanel in parent aspx page. googled , found answer; place code in rowdatabound:

private sub gridview1_rowdatabound(sender object, e gridviewroweventargs) handles gridview1.rowdatabound     dim lb linkbutton = trycast(e.row.findcontrol("lnkdownload"), linkbutton )     scriptmanager.getcurrent(me.page).registerasyncpostbackcontrol(lb) end sub 

and new error occurred. can't find linkbutton inside gridview using code in rowdatabound.

so googled again, , found out should add property aspx page clientidmode="autoid". working on framework 4.x. , couldn't 'cause im using 3.5. there remedies in current situation?

a found answer, changed rowdatabound content, refer code:

private sub gridview1_rowdatabound(sender object, e gridviewroweventargs) handles gridview1.rowdatabound     if e.row.rowtype = datacontrolrowtype.datarow         dim lbutton = ctype(e.row.findcontrol("lnkdownload"), system.web.ui.webcontrols.linkbutton)         dim scriptmanager__1 = toolkitscriptmanager.getcurrent(me.page)         if scriptmanager__1 isnot nothing             scriptmanager__1.registerpostbackcontrol(lbutton)         end if     end if end sub 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -