本文介绍了从gridview下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
先生,我在表格ID和文件名中使用了两个字段.我成功上传了文件,并且还在网格中显示了该文件名,我想在我单击网格中的文件名时执行该操作,请在新窗口中打开该文件.问候dnyaneshwar
我的代码在这里.cs代码
sir i have taken two field in table id and filename .i uploaded file successfully and also show that file name in grid,i want to do when i click on filename in grid that file open in new window please help. regards dnyaneshwar
my code is here .cs code
protected void btnSaveFile_Click(object sender, EventArgs e)
{
string uploadFolder = Server.MapPath ("FileStore\\");
FileUpload1.SaveAs(uploadFolder + FileUpload1.FileName);
Label1.Text = "File uploaded successfully: " + FileUpload1.PostedFile.FileName;
string filename=FileUpload1.PostedFile.FileName.ToString();
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
SqlCommand cmd = new SqlCommand("sp_SaveFile", sqlcon);
SqlParameter[] sqlp = new SqlParameter[] { new SqlParameter("@filename",filename)};
cmd.Parameters.AddRange(sqlp);
cmd.CommandType = CommandType.StoredProcedure;
sqlcon.Open();
int ret =cmd.ExecuteNonQuery();
sqlcon.Close();
if (ret == 1)
{
lblmsg.Text = "Filename inserted.";
}
BindGrid();
}
.aspx代码
.aspx code
<table width="100%">
<tr>
<td colspan="2" class="heading">
<asp:Label ID="lblFileTitle" runat="server" Text="File Upload" Font-Size="XX-Large"> </asp:Label>
</td>
</tr>
<tr>
<td style="width: 10%" align="right">
<asp:Label ID="lblchosefile" runat="server" Text="Choose File : "></asp:Label>
</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="FileUpload1"
ErrorMessage="Choose a file!">
</asp:RequiredFieldValidator>
<asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="OrangeRed"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2" style="width: 100%; padding-left: 200px;">
<asp:Button ID="btnSaveFile" runat="server" Text="Save" Font-Bold="true" OnClick="btnSaveFile_Click" />
<asp:Label ID="lblmsg" runat="server" ForeColor="Blue"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2" style="padding-left: 50px">
<asp:GridView ID="GridViewFile" runat="server" AllowPaging="true" AllowSorting="true"
AutoGenerateColumns="false" PageSize="3" DataKeyNames="FileID" OnRowDataBound="GridViewFile_RowDataBound"
OnRowCreated="GridViewFile_RowCreated">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="HeaderChk" runat="server" ToolTip="Select All" onclick="javascript:CheckAll(this)" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="ChildChk" runat="server" ToolTip="Select" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Serial No.</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblSRNO" runat="server" Text='<%# Container.DataItemIndex+1 %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="NameFile" HeaderText="File Name" SortExpression="NameFile" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
推荐答案
<asp:HyperLinkField DataNavigateUrlFields="NameFile"
DataNavigateUrlFormatString="(a location here)"
DataTextField="NameFile" HeaderText="File Name"
NavigateUrl="(a location)" />
并使
的可见属性< asp:boundfield datafield ="NameFile" headertext =文件名" sortexpression ="NameFile" xmlns:asp =#unknown"/>
错误.
谢谢
and make the visible property of
<asp:boundfield datafield="NameFile" headertext="File Name" sortexpression="NameFile" xmlns:asp="#unknown" />
false.
Thanks
这篇关于从gridview下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!