Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have binded gridview with my file system in asp.net, now when we click on any file it downloads the file, but i want to open that file in new window/teb, and when user rightclick on the file and select "save link as" then it shuld be download the file... can any one help me out??? thnx in adv...
Posted
Updated 14-Nov-11 16:43pm
v2
Comments
sriman.ch 15-Nov-11 0:25am    
Please provide your code for binding the file system in geidview....
Naikniket 16-Nov-11 4:24am    
acctuly i have binded treeview with file system & with treeview i hv binded gridview... m posting my code below...


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim rootFolder As String = Me.Server.MapPath("~\")

' load the treeview based on the folder strucutre
Dim rootNode As New TreeNode("Root", rootFolder)
rootNode.Expanded = True
rootNode.[Select]()
' add the root node
Me.tvFolders.Nodes.Add(rootNode)

' bind sub directories to the treeview
BindDirs(rootFolder, rootNode)

' bind the gridview to the datasource using the root node
BindDirsContents(rootNode, Me.gvFolderItems)
End If
End Sub

'''
'''
'''

''' <param name="sender"></param>
''' <param name="args"></param>
Protected Sub GvFolderItems_RowDataBound(ByVal sender As Object, ByVal args As GridViewRowEventArgs)
If args.Row.RowType = DataControlRowType.DataRow Then
Dim item As System.IO.FileSystemInfo = DirectCast(args.Row.DataItem, System.IO.FileSystemInfo)
Dim imageButton As ImageButton = DirectCast(args.Row.FindControl("btnItemIcon"), ImageButton)

If TypeOf item Is System.IO.DirectoryInfo Then
imageButton.ImageUrl = "Images/folder.gif"
Else
imageButton.ImageUrl = "Images/File.gif"

Dim fileInfo As System.IO.FileInfo = DirectCast(item, System.IO.FileInfo)
Dim lblSize As Label = DirectCast(args.Row.FindControl("lblSize"), Label)
lblSize.Text = String.Format("{0:N0} KB", fileInfo.Length \ 1000)
End If
End If
End Sub

'''
'''
'''

''' <param name="sender"></param>
''' <param name="args"></param>
Protected Sub GvFolderItems_RowCommand(ByVal sender As Object, ByVal args As GridViewCommandEventArgs)
' handle either opening the item or rebinding the grid
If args.CommandName = "ItemClick" Then
Dim name As String = DirectCast(args.CommandArgument, String)
Dim dinfo As New System.IO.DirectoryInfo(Me.tvFolders.SelectedNode.Value)

If System.IO.File.Exists(System.IO.Path.Combine(dinfo.FullName, name)) Then
Dim fileInfo As New System.IO.FileInfo(System.IO.Path.Combine(dinfo.FullName, name))
' they clicked on a file, download it
' to there PC
Me.Response.Clear()
Me.Response.AddHeader("Content-Disposition", "attachment; filename=" & fileInfo.Name)
Me.Response.AddHeader("Content-Length", fileInfo.Length.ToString())
Me.Response.ContentType = "application/octet-stream"
Me.Response.WriteFile(fileInfo.FullName)
Me.Response.[End]()
Else
For Each node As TreeNode In Me.tvFolders.SelectedNode.ChildNodes
If node.Text = name Then
node.Selected = True
node.Expanded = False

' expand the parents
Dim parentNode As TreeNode = node.Parent
While parentNode IsNot Nothing
parentNode.Expanded = False
parentNode = parentNode.Parent
End While

' bind the gridview to the datasource
BindDirsContents(node, Me.gvFolderItems)
Exit For
End If
Next
End If
End If
End Sub

'''
'''
'''

''' <param name="sender"></param>
''' <param name="args"></param>
Protected Sub TvFolders_SelectedNodeChanged(ByVal
eparadise 24-Mar-12 6:29am    
It's ok..........
Naikniket 16-Nov-11 4:26am    
Protected Sub TvFolders_SelectedNodeChanged(ByVal sender As Object, ByVal args As EventArgs) Handles tvFolders.SelectedNodeChanged
BindDirsContents(Me.tvFolders.SelectedNode, Me.gvFolderItems)

End Sub

'''
'''
'''

''' <param name="node"></param>
Private Shared Sub BindDirsContents(ByVal node As TreeNode, ByVal gridView As System.Web.UI.WebControls.GridView)
' bind the gridview to the datasource
Try
gridView.DataSource = New System.IO.DirectoryInfo(node.Value).GetFileSystemInfos()

gridView.DataBind()
Catch ex As Exception

End Try
End Sub

'''
'''
'''

''' <param name="path"></param>
''' <param name="treeNode"></param>
Private Shared Sub BindDirs(ByVal path As String, ByVal treeNode As TreeNode)
If Not String.IsNullOrEmpty(path) Then
For Each directoryPath As String In System.IO.Directory.GetDirectories(path)
Dim directory As New System.IO.DirectoryInfo(directoryPath)
Dim subNode As New TreeNode(directory.Name, directory.FullName)
treeNode.ChildNodes.Add(subNode)
' bind sub directories
BindDirs(directoryPath, subNode)
''Dim Files As System.IO.FileInfo() = directory.GetFiles()
''For FileCount As Integer = 0 To Files.Length - 1
'' treeNode.ChildNodes.Add(New TreeNode(Files(FileCount).Name))
''Next
' if the parent node is null, return this node
Next
End If
End Sub

Try this,
HTML
<a href="/downloads/file.docx" target="_blank">Download File</a>


All you have added is a "target" attribute that tells the browser to open in new window. Other target valid values include: self, top, parent, search. You can try them all and decide what you would like to use.

Happy coding,
Morgs
 
Share this answer
 
v2
hi

use Hyper Link Fild

<asp:hyperlinkfield text="Name" runat="Server" headertext="Column Header Text " target="_blank" datanavigateurlfields="ID" datanavigateurlformatstring="~/Page.apsx?Id={0}" xmlns:asp="#unknown">

during DataBinding {0} is get replace with ID in DataNavigateUrlFormatString

you can give the file page in DataNavigateUrlFormatString so your file get open in next tab
 
Share this answer
 
Comments
Naikniket 16-Nov-11 8:59am    
it's not working... it generates error "file page not found"... n ya i have binded my gridview with filesystem, n i want to open file directly.. for example there is a picture in folder, and it is displying in gridview, so when i click on it or open button in gridview the picture should be open in new tab... :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900