Vanessa says:
Dim
filePaths
As
String
() = Directory.GetFiles((
"C:\Assets docs"
& ITCode))
Request.QueryString("id") is not connected with database.
The id is the key of query stringparameter.
Based on the query string value the files are retched from folder.
Example:
You are redirecting from another page with QueryString Id 152 then in page load it will be checked and all the file from the folder Assets docs152 will be displayed in the GridView.
Actual path generated from query string C:\Assets docs152.
If you want directly show from folder refer below code.
HTML
C#
<asp:GridView runat="server" ID="grvDocuments" Width="100%" CssClass="table table-bordered" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="S.No">
<ItemTemplate>
<asp:Label ID="lblRowNumber" Text='<%# Container.DataItemIndex + 1 %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Text" HeaderText="Document Name" />
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:HyperLink ID="lnkView" Text="View" runat="server" Target="_blank"
NavigateUrl='<%# string.Format("~/Detail.aspx?fileName={0}", HttpUtility.UrlEncode(Eval("Value").ToString())) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
VB.Net
<asp:GridView runat="server" ID="grvDocuments" Width="100%" CssClass="table table-bordered" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="S.No">
<ItemTemplate>
<asp:Label ID="lblRowNumber" Text='<%# Container.DataItemIndex + 1 %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Text" HeaderText="Document Name" />
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:HyperLink ID="lnkView" Text="View" runat="server" Target="_blank"
NavigateUrl='<%# Eval(HttpUtility.UrlEncode("Value"), "~/Detail.aspx?fileName={0}") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code
Default
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetDocuments();
}
}
protected void GetDocuments()
{
string[] filePaths = System.IO.Directory.GetFiles(@"C:\Assets docs");
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(System.IO.Path.GetFileName(filePath), filePath));
}
grvDocuments.DataSource = files;
grvDocuments.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
GetDocuments()
End If
End Sub
Protected Sub GetDocuments()
Dim filePaths As String() = IO.Directory.GetFiles("C:\Assets docs")
Dim files As List(Of ListItem) = New List(Of ListItem)()
For Each filePath As String In filePaths
files.Add(New ListItem(IO.Path.GetFileName(filePath), filePath))
Next
grvDocuments.DataSource = files
grvDocuments.DataBind()
End Sub
Detail
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string filePath = Request.QueryString["fileName"];
Response.ContentType = "Application/pdf";
Response.WriteFile(filePath);
Response.End();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim filePath As String = Request.QueryString("fileName")
Response.ContentType = "Application/pdf"
Response.WriteFile(filePath)
Response.End()
End If
End Sub