Accessing textbox data from within a datalist template

kco1122

Member
Joined
Mar 29, 2006
Messages
21
Programming Experience
5-10
Alrighty.. so for those of you who read:

My last post

Here's more... I've got everything working so far, I can display a file listing in my datalist as well as let them delete files based off that list but I'm also trying to allow people to rename files on the server and I can't get that to work.. here's my problem in detail...

So far I have the display looking something like:

File1.txt
...misc info...
[button to delete]
[text box] [button to rename]

File2.txt
...
...

********************
Here is the aspx code for the datalist.. the datasource for this is a array list built from a directory listing of files:

VB.NET:
    <asp:DataList id="xxx" Runat="Server" RepeatColumns="1" CellPadding="3"
       OnItemCommand="xxx_ItemCommand" RepeatLayout="Flow" ShowFooter="False" ShowHeader="False">

        <ItemTemplate>
            <asp:Label ID="FileName" Text='<%# Container.Dataitem %>' Runat="Server"/><br />
            <asp:Label ID="FileSize" Text='<%# FileInfo(Container.Dataitem,"filesize") %>' Runat="Server"/><br />
            <asp:Label ID="FileURL" Text='<%# FileInfo(Container.Dataitem,"fileurl") %>' Runat="Server"/><br />
            <asp:TextBox ID="NewFilename" runat="server"></asp:TextBox>
            <asp:
            <asp:linkButton id="Rename"  Text="Rename" Runat="Server" Font-Size="10pt" CommandName='<%# "Rename_" & Container.DataItem %>'/><br />
            <asp:linkButton id="Delete"  Text="Delete" Runat="Server" Font-Size="10pt" CommandName='<%# "Delete_" & Container.DataItem %>'/>
            <hr />
        </ItemTemplate>
    </asp:DataList>

And here is the vb code behind it all:

VB.NET:
   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        GetFilesInfoInFolder()
    End Sub

    Sub GetFilesInfoInFolder()
        Dim FileArray() As String = Directory.GetFiles(Server.MapPath("Podcasts"), "*.*")

        Dim i As Integer
        For i = 0 To UBound(FileArray)
            FileArray(i) = Replace(FileArray(i), Server.MapPath("Podcasts/"), "")
        Next

        YUploadFiles.DataSource = FileArray
        YUploadFiles.DataBind()
    End Sub

    Function FileInfo(ByVal sFilename As String, ByVal sInfo As String)
        Dim FileURL As String
        Dim file As FileInfo

        Select Case UCase(sInfo)
            Case "FILEURL"
                FileURL = My.Request.ServerVariables("URL")
                FileURL = "http://" & My.Request.ServerVariables("SERVER_NAME") & "/" & _
                    FileURL.Substring(0, FileURL.LastIndexOf("/") + 1) & "Podcasts/" & sFilename

                FileInfo = FileURL
            Case "FILESIZE"
                file = New FileInfo(Server.MapPath("Podcasts") & "\" & sFilename)

                FileInfo = "File Size: " & file.Length
            Case Else
                FileInfo = ""
        End Select
    End Function

    Protected Sub xxx_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles xxx.ItemCommand
        Dim I As Integer

        Select Case e.CommandName.Substring(0, 7).ToUpper
            Case "DELETE_"
                File.Delete(Server.MapPath("Podcasts") & "\" & e.CommandName.Substring(7, e.CommandName.Length - 7))
                GetFilesInfoInFolder()
            Case "RENAME_"
                For I = 0 To YUploadFiles.Items.Count - 1
                    Dim NewFilename As TextBox
                    NewFilename = YUploadFiles.Items(I).FindControl("NewFileName")
                    Response.Write(NewFilename.Text)
                    If NewFilename.Text <> "" Then
                        Rename(Server.MapPath("Podcasts") & "\" & e.CommandName.Substring(7, e.CommandName.Length - 7), _
                               Server.MapPath("Podcasts") & "\" & NewFilename.Text)
                        GetFilesInfoInFolder()
                    End If
                Next
        End Select
    End Sub

What I'm stuck on is the renaming part, the last sub (XXX_ITEMCOMMAND) ... for some reason as I iterate through the controls within the datalist, even though I can FIND the control named 'NewFileName', when I try to retrieve the text from the textbox, there is NOTHING there.

Can anyone give a little help on this? It's driving me nuts... plz!!!
 
Can you please provide example data as to what the values are for each in the following:

Rename(Server.MapPath("Podcasts") & "\" & e.CommandName.Substring(7, e.CommandName.Length - 7), Server.MapPath("Podcasts") & "\" & NewFilename.Text)

** What actual value is here -> Server.MapPath("Podcasts") & "\" & e.CommandName.Substring(7, e.CommandName.Length - 7)

I have found that when using the substring I have to use the indexof also to get the correct position.

Thx,
 
Back
Top