We help IT Professionals succeed at work.

File upload button breaks expansion of treeview on second file upload

ameek
ameek asked
on
Background:  I have an aspx page that references a master template.  On my work.aspx page, there are items like sqldatasource, listviews, datapager, fileupload, and treeview.  

Problem:  The treeview node link breaks when clicking the "upload file" button the 2nd time.  Note there are other edit/update buttons on the page and none of them break the treeview node expansion after they are clicked and the page is refreshed -- only the update button breaks it.  And the update button breaks it always after the upload is clicked the 2nd time and only breaks it randomly the 1st time it is clicked.

I have tried changing the SelectAction on the treenode to Select, Expand, and SelectExpand, but that does not change the behavior.  I keep thinking maybe I need to repopulate the tree node in the on_click somhow (maybe a call to the TreeView1_TreeNodePopulate, but I don't know how to pass it the right parameters to do that (I'm pretty new at VB).  

Here is my related code that is in the aspx page:

 
<div class="attachment">
        
     <table>
        <tr><td colspan="3"><span class="bigger">Files associated with this job number.</span>  You may upload up to three files at time. </td></tr>
        <tr> 
        <td valign="top"><br />

        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <asp:FileUpload ID="FileUpload2" runat="server" /><br />
        <asp:FileUpload ID="FileUpload3" runat="server" /><br />
    
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" 
         Text="Upload File(s)" /><br />
       
        <asp:Label ID="AttLabel1" runat="server"></asp:Label></div><br />
        
        </td>
        
        <td  width="100px"> &nbsp; &nbsp; </td>


        <td valign="top">
            <asp:TreeView ID="TreeView1" runat="server" OnTreeNodePopulate="TreeView1_TreeNodePopulate" AutoGenerateDataBindings="False" ExpandDepth="0">
            <Nodes> <asp:TreeNode Value="C:" Text="Expand to see Attachments" SelectAction="Expand" PopulateOnDemand="true"></asp:TreeNode> </Nodes>
            </asp:TreeView>
        </td>  
        </tr>
    </table>

</div>

Open in new window



And here is the related code in my code behind:
 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ' This takes login name with domain name and strips out domain name
        '  Therefore ACSDATALINE\John_Doe becomes just John_Doe
        Dim Parts As String() = HttpContext.Current.User.Identity.Name.Split("\")

        If Parts.Length = 2 Then
            LoginName.Text = Parts(1)
        Else
            LoginName.Text = HttpContext.Current.User.Identity.Name
        End If


        Dim mode As String = Request.QueryString("mode")

        If mode = "Edit" Then Base.EditIndex = 0

        'populate attachments block
        Dim jobnum As String = Request.QueryString("Job_Number")
        TreeView1.Nodes(0).Value = Server.MapPath("~/media/" & jobnum)

    End Sub

Open in new window

Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, ByVal e As TreeNodeEventArgs)
        If IsCallback = True Then
            If e.Node.ChildNodes.Count = 0 Then
                LoadChildNode(e.Node)
            End If
        End If
    End Sub

    Protected Sub LoadChildNode(ByVal node As TreeNode)
        Dim directory As DirectoryInfo = Nothing
        directory = New DirectoryInfo(node.Value)

        For Each subtree As DirectoryInfo In directory.GetDirectories()
            Dim subNode As New TreeNode(subtree.Name)
            subNode.Value = subtree.FullName
            Try
                If subtree.GetDirectories().Length > 0 Or subtree.GetFiles().Length > 0 Then
                    subNode.SelectAction = TreeNodeSelectAction.SelectExpand
                    subNode.PopulateOnDemand = True
                    subNode.NavigateUrl = "#"
                End If
            Catch ex As Exception

            End Try
            node.ChildNodes.Add(subNode)
        Next subtree
        For Each fi As FileInfo In directory.GetFiles()
            Dim subNode As New TreeNode(fi.Name)
            Dim jobnum As String = Request.QueryString("Job_Number")
            node.ChildNodes.Add(subNode)
            subNode.NavigateUrl = "~/media/" & jobnum & "/" & fi.Name.ToString()
        Next fi
    End Sub


    Protected Sub Button1_Click(ByVal sender As Object, _
       ByVal e As System.EventArgs)

        Dim jobnum As String = Request.QueryString("Job_Number")
        Dim filepath As String = Server.MapPath("~/media/" & jobnum)
        Dim uploadedFiles As HttpFileCollection = Request.Files
        Dim i As Integer = 0

        If Not Directory.Exists(Server.MapPath("~/media/" & jobnum)) Then
            Directory.CreateDirectory(Server.MapPath("~/media/" & jobnum))
        End If


        Do Until i = uploadedFiles.Count
            Dim userPostedFile As HttpPostedFile = uploadedFiles(i)

            Try

                If (userPostedFile.ContentLength > 0) Then
                    AttLabel1.Text += "<u>Successfully loaded file #" & (i + 1) & "</u><br>"
                    AttLabel1.Text += "File Content Type: " & _
                       userPostedFile.ContentType & "<br>"
                    AttLabel1.Text += "File Size: " & _
                       userPostedFile.ContentLength & "kb<br>"

                    AttLabel1.Text += "File Name: " & _
                    userPostedFile.FileName & "<br>"

                    userPostedFile.SaveAs(filepath & "\" & _
                       System.IO.Path.GetFileName(userPostedFile.FileName))

                    'AttLabel1.Text += "Location where saved: " & _
                    'filepath & "/" & _
                    ' System.IO.Path.GetFileName(userPostedFile.FileName) & _
                    '"<p>"

                End If
            Catch ex As Exception
                AttLabel1.Text += "Error:<br>" & ex.Message
            End Try
            i += 1
        Loop



    End Sub

Open in new window



Here's a visual of what I mean:
 screenshot of issue
Thanks for any help you can give!
Comment
Watch Question

Most Valuable Expert 2012
Top Expert 2014
Commented:
Try changing the page load



Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 If Not Page.IsPostBack Then
        ' This takes login name with domain name and strips out domain name
        '  Therefore ACSDATALINE\John_Doe becomes just John_Doe
        Dim Parts As String() = HttpContext.Current.User.Identity.Name.Split("\")

        If Parts.Length = 2 Then
            LoginName.Text = Parts(1)
        Else
            LoginName.Text = HttpContext.Current.User.Identity.Name
        End If


        Dim mode As String = Request.QueryString("mode")

        If mode = "Edit" Then Base.EditIndex = 0

        'populate attachments block
        Dim jobnum As String = Request.QueryString("Job_Number")
        TreeView1.Nodes(0).Value = Server.MapPath("~/media/" & jobnum)
End If
    End Sub

Open in new window

Author

Commented:
Brilliantly simple and works perfectly.  Thank you!!!
Most Valuable Expert 2012
Top Expert 2014

Commented:
Glad to help :-)