c# - To download selected filename in grid view -
in below code attachment file names in session have bind documnetid,attachmentname , download.but when select 1st attachmnet filename downloading second attchmt filename.my aim download select in grid view. eg
document id attchfilename download 1 xxx view 2 yyy view markup
<asp:gridview id="attchdwnld" runat="server" allowpaging="true" allowsorting="true" alternatingrowstyle-cssclass="alt" autogeneratecolumns="false" cellpadding="4" cssclass="mgrid" forecolor="#333333" pagesize="10" pagesize-mode="numericpages" pagerstyle-cssclass="pgr" pagerstyle-visible="true" showfooter="false" width="100%" onrowcommand="attchdwnld_rowcommand" > <columns> <asp:templatefield headertext="documentid" itemstyle-width="200px"> <itemtemplate> <asp:label id="documentid" runat="server" text='<%#eval("documentid") %>'> </asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="attachmentfilename" itemstyle-width="200px"> <itemtemplate> <asp:label id="attachmentfilename" runat="server" text='<%#eval("attachmentfilename") %>'> </asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="download" itemstyle-width="150px"> <itemtemplate> <asp:linkbutton id="lnlbtnattch" runat="server" text="view" commandname="edit"> </asp:linkbutton> </itemtemplate> </asp:templatefield> </columns> <headerstyle font-bold="true" forecolor="white" /> </asp:gridview> code-behind
protected void attchdwnld_rowcommand(object sender, gridviewcommandeventargs e) { var searchdoc = (searchdoc)session["documentname"]; lblmessage.text = searchdoc.attachmentfilename; string fileurl = "c:\\search\\" + stname + "\\" + strtfolder + "\\" + lblmessage.text; string filename = fileurl; if (filename != "") { string path = filename; system.io.fileinfo file = new system.io.fileinfo(path); if (file.exists) { response.clear(); response.addheader("content-disposition", "attachment; filename=" + file.name); response.addheader("content-length", file.length.tostring()); response.contenttype = "application/octet-stream"; response.writefile(file.fullname); response.end(); } else { response.write("this file not exist."); } } }
i don't see why should store session["documentname"] in code @ all. omitting part.
try code
protected void attchdwnld_rowcommand(object sender, gridviewcommandeventargs e) { // gets clicked row of gridview var row = ((linkbutton)e.commandsource).namingcontainer gridviewrow; //gets filename in cliked row var attachmentnamelabel = row.findcontrol("attachmentfilename") label; //store lblmessage's text lblmessage.text = attachmentnamelabel.text; var filepath = string.format("c:\\search\\{0}\\{1}\\{2}", stname, strtfolder, lblmessage.text); var file = new system.io.fileinfo(filepath); if (file.exists) { response.clear(); response.addheader("content-disposition", "attachment; filename=" + file.name); response.addheader("content-length", file.length.tostring()); response.contenttype = "application/octet-stream"; response.writefile(file.fullname); response.end(); } else { response.write("this file not exist."); } }
Comments
Post a Comment