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

Discussion in 'ASP .Net' started by Øyvind Isaksen, Feb 11, 2006.

  1. 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!!!
     
    Øyvind Isaksen, Feb 11, 2006
    #1
    1. Advertising

  2. Øyvind Isaksen

    Alec MacLean Guest

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


    "Øyvind Isaksen" <> wrote in message
    news:...
    >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!!!
    >
     
    Alec MacLean, Feb 11, 2006
    #2
    1. Advertising

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

    "Alec MacLean" <> wrote in message
    news:...
    > Hmm - how about a databind.eval(...) statement on the template column?
    >
    >
    > "Øyvind Isaksen" <> wrote in message
    > news:...
    >>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!!!
    >>

    >
    >
     
    Patrick.O.Ige, Feb 11, 2006
    #3
  4. RE: Show files in datagrid (DirectoryInfo), make button to choose a fi

    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" 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!!!
    >
    >
    >
     
    =?Utf-8?B?RWx0b24gVw==?=, Feb 11, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Cox [Microsoft MVP]

    Re: DirectoryInfo.GetFiles(Mask) finding too many files!

    Ken Cox [Microsoft MVP], Jan 26, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    829
    Ken Cox [Microsoft MVP]
    Jan 26, 2005
  2. =?Utf-8?B?SSBhbSBTYW0=?=

    Databinding to a list of files in DirectoryInfo

    =?Utf-8?B?SSBhbSBTYW0=?=, Apr 21, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    356
    =?Utf-8?B?SSBhbSBTYW0=?=
    Apr 21, 2005
  3. Larry Epn
    Replies:
    8
    Views:
    3,378
    Allen Chen [MSFT]
    Mar 16, 2009
  4. Stefano Crocco
    Replies:
    0
    Views:
    152
    Stefano Crocco
    Nov 14, 2008
  5. NotGiven
    Replies:
    6
    Views:
    145
    oeyvind toft
    Sep 5, 2004
Loading...

Share This Page