在下面的代碼中,我在會話中獲得附件文件名我已經綁定了documnetid,attachmentname和download.but當我選擇第一個attachmnet文件名時,它正在下載第二個attchmt文件名。我的目標是下載我在網格視圖中選擇的內容。 如在網格視圖下載選定的文件名
document id attchfilename download
1 xxx view
2 yyy view
標記
<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>
代碼隱藏
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 does not exist.");
}
}
}
你在哪裏設置'會話[ 「Documentname」]'的價值? –