473,765 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Show files in datagrid (DirectoryInfo) , make button to choose a file and put the filename in a session-variable

I have a datagrid that displays all files in a folder. It works good, but I
need a extra column with a link for choosing image.
As shown in the code below I have made an ItemTemplate for this, and what I
need to do is to put the filename in the CommandArgument . How do I do that??
Is this the right way to do it, or is it a smarter way to put the filename
in a session-variable when clicking on the "Use this image"-LinkButton ?
-------------------------------------------------
DataGrid:
-------------------------------------------------

<asp:datagrid id="dtgFilelist " runat="server" autogeneratecol umns="False">
<Columns>
<asp:TemplateCo lumn>
<ItemTemplate >
<asp:LinkButt on ID="lnkChooseIm age" Runat="server"
CommandName="ch ooseImage" CommandArgument ="[HERE I WANT THE FILENAME]">Use
this image</asp:LinkButton>
</ItemTemplate>
</asp:TemplateCol umn>
<asp:HyperLinkC olumn DataNavigateUrl Field="Name"
DataTextField=" Name" HeaderText="Fil ename" />
<asp:BoundColum n DataField="Last WriteTime" HeaderText="Dat e"
DataFormatStrin g="{0:d}" />
<asp:BoundColum n DataField="Leng th" HeaderText="Siz e"
DataFormatStrin g="{0:#,### bytes}" />
</Columns>
</asp:datagrid>

-------------------------------------------------
Code Behind:
-------------------------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim dirInfo As New DirectoryInfo(S erver.MapPath("/uploads/"))
Me.dtgFilelist. DataSource = dirInfo.GetFile s("*.*")
Me.dtgFilelist. DataBind()
End Sub
Private Sub dtgFilelist_Ite mCommand(ByVal source As Object, ByVal e As
System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles
dtgFilelist.Ite mCommand

Dim KommandoNavn As String = e.CommandName

If e.CommandName = "chooseImag e" Then
session("image" ) = e.CommandArgume nt

'I also tried this:
'session("image ") = e.Item.Cells(0) .Text
End If

End Sub
Thanks for helping me!!!
Feb 11 '06 #1
3 2070
Hmm - how about a databind.eval(. ..) statement on the template column?
"Øyvind Isaksen" <oy****@webress urs.no> wrote in message
news:e0******** ******@TK2MSFTN GP11.phx.gbl...
I have a datagrid that displays all files in a folder. It works good, but I
need a extra column with a link for choosing image.
As shown in the code below I have made an ItemTemplate for this, and what
I need to do is to put the filename in the CommandArgument . How do I do
that?? Is this the right way to do it, or is it a smarter way to put the
filename in a session-variable when clicking on the "Use this
image"-LinkButton ?
-------------------------------------------------
DataGrid:
-------------------------------------------------

<asp:datagrid id="dtgFilelist " runat="server" autogeneratecol umns="False">
<Columns>
<asp:TemplateCo lumn>
<ItemTemplate >
<asp:LinkButt on ID="lnkChooseIm age" Runat="server"
CommandName="ch ooseImage" CommandArgument ="[HERE I WANT THE FILENAME]">Use
this image</asp:LinkButton>
</ItemTemplate>
</asp:TemplateCol umn>
<asp:HyperLinkC olumn DataNavigateUrl Field="Name"
DataTextField=" Name" HeaderText="Fil ename" />
<asp:BoundColum n DataField="Last WriteTime" HeaderText="Dat e"
DataFormatStrin g="{0:d}" />
<asp:BoundColum n DataField="Leng th" HeaderText="Siz e"
DataFormatStrin g="{0:#,### bytes}" />
</Columns>
</asp:datagrid>

-------------------------------------------------
Code Behind:
-------------------------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim dirInfo As New DirectoryInfo(S erver.MapPath("/uploads/"))
Me.dtgFilelist. DataSource = dirInfo.GetFile s("*.*")
Me.dtgFilelist. DataBind()
End Sub
Private Sub dtgFilelist_Ite mCommand(ByVal source As Object, ByVal e As
System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles
dtgFilelist.Ite mCommand

Dim KommandoNavn As String = e.CommandName

If e.CommandName = "chooseImag e" Then
session("image" ) = e.CommandArgume nt

'I also tried this:
'session("image ") = e.Item.Cells(0) .Text
End If

End Sub
Thanks for helping me!!!

Feb 11 '06 #2
Or Get it in the codebehind using e.Item.DataItem .Item("").
Patrick

"Alec MacLean" <al**********@N O19SPAM60.copeo hs.com> wrote in message
news:e3******** ******@TK2MSFTN GP14.phx.gbl...
Hmm - how about a databind.eval(. ..) statement on the template column?
"Øyvind Isaksen" <oy****@webress urs.no> wrote in message
news:e0******** ******@TK2MSFTN GP11.phx.gbl...
I have a datagrid that displays all files in a folder. It works good, but
I need a extra column with a link for choosing image.
As shown in the code below I have made an ItemTemplate for this, and what
I need to do is to put the filename in the CommandArgument . How do I do
that?? Is this the right way to do it, or is it a smarter way to put the
filename in a session-variable when clicking on the "Use this
image"-LinkButton ?
-------------------------------------------------
DataGrid:
-------------------------------------------------

<asp:datagrid id="dtgFilelist " runat="server"
autogeneratecol umns="False">
<Columns>
<asp:TemplateCo lumn>
<ItemTemplate >
<asp:LinkButt on ID="lnkChooseIm age" Runat="server"
CommandName="ch ooseImage" CommandArgument ="[HERE I WANT THE
FILENAME]">Use this image</asp:LinkButton>
</ItemTemplate>
</asp:TemplateCol umn>
<asp:HyperLinkC olumn DataNavigateUrl Field="Name"
DataTextField=" Name" HeaderText="Fil ename" />
<asp:BoundColum n DataField="Last WriteTime" HeaderText="Dat e"
DataFormatStrin g="{0:d}" />
<asp:BoundColum n DataField="Leng th" HeaderText="Siz e"
DataFormatStrin g="{0:#,### bytes}" />
</Columns>
</asp:datagrid>

-------------------------------------------------
Code Behind:
-------------------------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim dirInfo As New DirectoryInfo(S erver.MapPath("/uploads/"))
Me.dtgFilelist. DataSource = dirInfo.GetFile s("*.*")
Me.dtgFilelist. DataBind()
End Sub
Private Sub dtgFilelist_Ite mCommand(ByVal source As Object, ByVal e As
System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles
dtgFilelist.Ite mCommand

Dim KommandoNavn As String = e.CommandName

If e.CommandName = "chooseImag e" Then
session("image" ) = e.CommandArgume nt

'I also tried this:
'session("image ") = e.Item.Cells(0) .Text
End If

End Sub
Thanks for helping me!!!


Feb 11 '06 #3
Hi Øyvind,

You can also use ButtonColumn. If you need show filename on buttons, you can
set DataTextField = "filename_Filed ". If you don't want to filename, just set
Text, e.g. Text="Open File". And store filename either in DataKeyField or an
invisible boundcloumn. In ItemCommand event, it's easy to retrieve filename.

HTH

Elton Wang

"Øyvind Isaksen" wrote:
I have a datagrid that displays all files in a folder. It works good, but I
need a extra column with a link for choosing image.
As shown in the code below I have made an ItemTemplate for this, and what I
need to do is to put the filename in the CommandArgument . How do I do that??
Is this the right way to do it, or is it a smarter way to put the filename
in a session-variable when clicking on the "Use this image"-LinkButton ?
-------------------------------------------------
DataGrid:
-------------------------------------------------

<asp:datagrid id="dtgFilelist " runat="server" autogeneratecol umns="False">
<Columns>
<asp:TemplateCo lumn>
<ItemTemplate >
<asp:LinkButt on ID="lnkChooseIm age" Runat="server"
CommandName="ch ooseImage" CommandArgument ="[HERE I WANT THE FILENAME]">Use
this image</asp:LinkButton>
</ItemTemplate>
</asp:TemplateCol umn>
<asp:HyperLinkC olumn DataNavigateUrl Field="Name"
DataTextField=" Name" HeaderText="Fil ename" />
<asp:BoundColum n DataField="Last WriteTime" HeaderText="Dat e"
DataFormatStrin g="{0:d}" />
<asp:BoundColum n DataField="Leng th" HeaderText="Siz e"
DataFormatStrin g="{0:#,### bytes}" />
</Columns>
</asp:datagrid>

-------------------------------------------------
Code Behind:
-------------------------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim dirInfo As New DirectoryInfo(S erver.MapPath("/uploads/"))
Me.dtgFilelist. DataSource = dirInfo.GetFile s("*.*")
Me.dtgFilelist. DataBind()
End Sub
Private Sub dtgFilelist_Ite mCommand(ByVal source As Object, ByVal e As
System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles
dtgFilelist.Ite mCommand

Dim KommandoNavn As String = e.CommandName

If e.CommandName = "chooseImag e" Then
session("image" ) = e.CommandArgume nt

'I also tried this:
'session("image ") = e.Item.Cells(0) .Text
End If

End Sub
Thanks for helping me!!!

Feb 11 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1473
by: Xarky | last post by:
Hi, I am trying to show files of type mp3 or wma or of type both. To show either of mp3 or wma I am doing as follows(choosing file type by a radio button), DirectoryInfo currentDir = new DirectoryInfo(path + "\\"); FileInfo allFiles; if (MP3s.Checked == true) allFiles = currentDir.GetFiles("*.mp3"); else if (WMAs.Checked == true)
4
1531
by: MikeY | last post by:
Hi everyone, In C#, is there a easy way of display files (ie. mp3 files) to a datagrid? If someone can provide me with some code, or advice I would appreciated it. Thanks in advance. MikeY
1
2557
by: TK | last post by:
I have a file upload control to allow a user to select/upload a file to the server. They need to upload x number of files in one shot because they have to confirm that they are uploading x number files for processing. So I do not want the files to be uploaded until the user presses a submit button verifying that they want to submit x number of files for processig (processing refers to me doing something to the files once uploaded). Catch...
2
6063
by: Tom Wells | last post by:
I have a little file upload page that I have been able to use to successfully upload files to the C: drive of LocalHost (my machine). I need to be able to upload to a network drive from the intranet server. On the line: dirs = Directory.GetDirectories(currentDir) I get "Access to the path "\\les-net\les\Special Projects\ATSPDF" is denied." How do I get the GetDirectories command to user my user ID and password when it tries to hit the...
1
9395
by: Vandana Rola | last post by:
Hello everyone, I am a beginner in VB.net. I am trying to write a program which will open around 300 files (which are .msg files)in a folder and extract the responses of the questions in those files. I have been able to open and extract data from one file using ...opentext(filename) method but I am not able to figure out how to loop through each file in the folder, open it, and extract data. I have been able to find a way to extract...
3
1337
by: Øyvind Isaksen | last post by:
Hi, I have made a code that list all files in a directory on the server. It works fine, but I need to modify the filename (remove the 7 first char in the name) that is displayed in the datagrid. Example: The file "123456_myfile.jpg" shoult be displayed like this: "myfile.jpg" ---------------------------
4
5255
by: maglev_now | last post by:
I'm using .net 1.1 trying to get a list of files in folder on the server. The user would select the file they want to download from a DropDownList. Can someone tell me how this should be done? I can't find the right class or object to use. Is it the File class or the FolderBrowser? Stuck like Chuck!!! Any suggestions welcome. Thanks.
1
5981
by: Riky | last post by:
hi i have a windows application. I am select the folder or file name from the folderbrowserdialog box and to delete the folder or file i am calling the following code. Public Function DeleteFile(ByVal path As String) As Boolean 'Delete a specified file Try If File.Exists(path) = True Then File.Delete(path)
0
1069
by: arugulla | last post by:
hello all, I have done C# web service for extracting xml file nodes and generate the files for the correcsponding nodes in that parsed xml. When second time i want to overwrite those files I dont understand how to do it. Could anyone please help me out how to do and here is my whole code. the parameters for this methods are sourceXml(the xml that has to be parsed, xslt file for this xml file, targetPath to store result parsed xml file,...
2
1112
by: Adela Arpitha | last post by:
Hi, i'm using asp.net 1.1 for my project. I have a written a c# code where the user can upload his files through the file upload component which gets uploaded to the specified path in a local disk and these files are viewed on the user screen application using the concept of fetching files from the local drive. I have taken a table to store these files in 1 column and in the other column i have given hyperlinks to download the files. ...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9399
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9957
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.