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

Ø

Øyvind Isaksen

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" autogeneratecolumns="False">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="lnkChooseImage" Runat="server"
CommandName="chooseImage" CommandArgument="[HERE I WANT THE FILENAME]">Use
this image</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:HyperLinkColumn DataNavigateUrlField="Name"
DataTextField="Name" HeaderText="Filename" />
<asp:BoundColumn DataField="LastWriteTime" HeaderText="Date"
DataFormatString="{0:d}" />
<asp:BoundColumn DataField="Length" HeaderText="Size"
DataFormatString="{0:#,### bytes}" />
</Columns>
</asp:datagrid>



-------------------------------------------------
Code Behind:
-------------------------------------------------


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dirInfo As New DirectoryInfo(Server.MapPath("/uploads/"))
Me.dtgFilelist.DataSource = dirInfo.GetFiles("*.*")
Me.dtgFilelist.DataBind()
End Sub


Private Sub dtgFilelist_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dtgFilelist.ItemCommand

Dim KommandoNavn As String = e.CommandName

If e.CommandName = "chooseImage" Then
session("image") = e.CommandArgument

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

End Sub


Thanks for helping me!!!
 
P

Patrick.O.Ige

Or Get it in the codebehind using e.Item.DataItem.Item("").
Patrick

Alec MacLean said:
Hmm - how about a databind.eval(...) statement on the template column?


Øyvind Isaksen said:
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"
autogeneratecolumns="False">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="lnkChooseImage" Runat="server"
CommandName="chooseImage" CommandArgument="[HERE I WANT THE
FILENAME]">Use this image</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:HyperLinkColumn DataNavigateUrlField="Name"
DataTextField="Name" HeaderText="Filename" />
<asp:BoundColumn DataField="LastWriteTime" HeaderText="Date"
DataFormatString="{0:d}" />
<asp:BoundColumn DataField="Length" HeaderText="Size"
DataFormatString="{0:#,### bytes}" />
</Columns>
</asp:datagrid>



-------------------------------------------------
Code Behind:
-------------------------------------------------


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dirInfo As New DirectoryInfo(Server.MapPath("/uploads/"))
Me.dtgFilelist.DataSource = dirInfo.GetFiles("*.*")
Me.dtgFilelist.DataBind()
End Sub


Private Sub dtgFilelist_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dtgFilelist.ItemCommand

Dim KommandoNavn As String = e.CommandName

If e.CommandName = "chooseImage" Then
session("image") = e.CommandArgument

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

End Sub


Thanks for helping me!!!
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top