Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

Using a Treeview and Gridview to display File Directory & Date Modified column

$
0
0

Hi - I've updated this WebUserControl to display a Gridview next to the Treeview. My aim is to display the corresponding Date Modified value for any files listed in the Treeview. 

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="BrowseFilesUsrCtrl.ascx.cs" Inherits="AllstateCTSNGSrvViewingTool.Controls.BrowseFilesUsrCtrl" %><table width="100%" border="1"><tr><td style="width: 631px; position: static; top: 10px; height: 16px" valign="top"><asp:Label ID="lblDateMod" runat="server" EnableViewState="False" Font-Bold="False" Font-Names="Arial Narrow"
                Font-Size="Smaller"></asp:Label></td><td style="width: 1604px; position: static; height: 16px" valign="top" colspan="2"><asp:Label ID="lblFile" runat="server" Font-Bold="False" Font-Names="Arial Narrow" Font-Size="Smaller" EnableViewState="False"></asp:Label></td></tr></table><table width="100%" border="1"><tr><td style="width: 631px; position: static; top: 10px; height: 232px" valign="top"><asp:Panel ID="Panel1" runat="server" Height="500px" ScrollBars="Auto" Width="100%" BorderStyle="Groove"><asp:TreeView ID="MyTree" runat="server" ExpandDepth="1" ImageSet="XPFileExplorer"
                    NodeIndent="15" OnSelectedNodeChanged="MyTree_SelectedNodeChanged" OnTreeNodePopulate="PopulateNode"
                    PathSeparator="|" SkinID="Explorer"><ParentNodeStyle Font-Bold="False" /><HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" /><SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
                        VerticalPadding="0px" /><Nodes><asp:TreeNode PopulateOnDemand="True" Text="." Value="."></asp:TreeNode></Nodes><NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"
                        NodeSpacing="0px" VerticalPadding="2px" /></asp:TreeView></asp:Panel></td><td style="width: 200px; position: static; height: 100%" valign="top"><asp:GridView ID="GridView1"
                          runat="server"
                          AutoGenerateColumns="False"
                          GridLines="None" CellPadding="3" BorderStyle="Solid" Visible="true"><Columns><asp:BoundField DataField="Name" HeaderText="Name"
                         HeaderStyle-HorizontalAlign="Left"
                         HeaderStyle-Font-Bold="true" /><%--<asp:BoundField DataField="Length" HeaderText="Size"
                         ItemStyle-HorizontalAlign="Right"
                         HeaderStyle-HorizontalAlign="Right"
                         HeaderStyle-Font-Bold="true" />--%><asp:BoundField DataField="LastWriteTime"
                         HeaderText="Date Modified"
                         HeaderStyle-HorizontalAlign="Left"
                         HeaderStyle-Font-Bold="true" /></Columns></asp:GridView></td><td style="width: 100%; position: static; height: 100%" valign="top">&nbsp;<asp:TextBox ID="tbxContents" runat="server" Font-Names="Courier New" Font-Size="Small"
                Height="508px" ReadOnly="True" TextMode="MultiLine" Visible="False" Width="99%"
                Wrap="False" EnableViewState="False"></asp:TextBox></td></tr></table><asp:HiddenField ID="HiddenField1" runat="server" />

Here is my codebehind for populating the Treeview directory:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                ShowEdit(false);        
        }

        void Page_PreRender(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                if(DirPath!="None")
                    Populate();   
            }
        }

        public override void DataBind()
        {
            if (DirPath != "None")
                Populate();
        }

        public void PopulateNode(Object source, TreeNodeEventArgs e)
        {
            TreeNode node = e.Node;

            if (HiddenField1.Value == null || HiddenField1.Value.Length == 0)
            {
                MyTree.Visible = false;
                return;
            }
            MyTree.Visible = true; ;
            

            try
            {
                String[] dirs = Directory.GetDirectories(node.Value);

                // Enumerate directories
                foreach (String dir in dirs)
                {
                    String virtualDir = node.Value.TrimEnd(_slashArray) + @"\" + Path.GetFileName(dir);

                    TreeNode newNode = new TreeNode(Path.GetFileName(dir), virtualDir);
                    try
                    {
                        if (Directory.GetFiles(dir).Length > 0
                            || Directory.GetDirectories(dir).Length > 0)
                        {
                            newNode.PopulateOnDemand = true;                            
                        }
                    }
                    catch (UnauthorizedAccessException)
                    {
                        newNode.Text += " [AccessDenied]";
                    }
                    node.ChildNodes.Add(newNode);                    
                }
                // Enumerate files
                String[] files = Directory.GetFiles(node.Value);
                foreach (String file in files)
                {
                    TreeNode newNode = new TreeNode(Path.GetFileName(file), file);
                    node.ChildNodes.Add(newNode);                    
                }

                //PMC
                GridView1.Visible = true;
                this.GridView1.DataSource = Directory.GetFiles(DirPath);
                this.GridView1.DataBind();
            }
            catch (Exception exx)
            {

                tbxContents.Text = exx.Message;
                ShowEdit(true);

            }
        }
        protected void Populate()
        {
            try
            {
                HiddenField1.Value = DirPath;
                MyTree.Nodes.Clear();
                TreeNode newnode = new TreeNode(HiddenField1.Value, HiddenField1.Value);
                newnode.PopulateOnDemand = true;
                MyTree.Nodes.Add(newnode);
                if (HiddenField1.Value == null || HiddenField1.Value.Length == 0)
                {
                    MyTree.Visible = false;
                }
                else
                    MyTree.Visible = true; ;
                    
                MyTree.ExpandDepth = 1;
            }
            catch (Exception ex)
            {
                tbxContents.Text = ex.Message;
                ShowEdit(true);
              
            }
        }

        protected void MyTree_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeNode node = MyTree.SelectedNode;

            if (node.ChildNodes.Count == 0)
            {
                FileInfo fi = new FileInfo(node.Value);
                System.DateTime dt = fi.LastWriteTimeUtc;

                //this is to compensate .Net flaw - looks like it is fixed now
                //if (!System.TimeZone.CurrentTimeZone.IsDaylightSavingTime(dt))
                //    dt = dt.AddHours(-1);

                bool canView = false;
                try
                {
                    string ext = ConfigurationManager.AppSettings["ViewExt"];
                    string[] extensions = ext.Split(';');
                    foreach (string extension in extensions)
                    {
                        if (String.Compare(fi.Extension, extension, true) == 0)
                        {
                            canView = true;
                            break;
                        }
                    }
                }
                catch (Exception)
                {
                }

                if (canView)
                {
                    tbxContents.Text = CleanseConfigs(File.ReadAllText(node.Value));
                    string ver = GetConfigVersion(fi);
                    lblFile.Text = 
                        string.IsNullOrEmpty(ver) ?
                         string.Format("{0}  [Date Modified: <b>{1}</b>]",
                            node.Value,dt.ToLocalTime().ToString()):
                        string.Format("{0}  [Ver.:<b> {2} </b>] [Date Modified: <b>{1}</b>]",
                            node.Value, dt.ToLocalTime().ToString(), ver);
                    ShowEdit(true);
                }
                else if (String.Compare(fi.Extension, ".exe", true) == 0 ||
                        String.Compare(fi.Extension, ".dll", true) == 0)
                {

                    tbxContents.Text = String.Format("Version:\t{0}\nDateModified:\t{1}\nSize(bytes):\t{2}",
                        System.Diagnostics.FileVersionInfo.GetVersionInfo(node.Value).ProductVersion,
                        dt.ToLocalTime().ToString(), fi.Length);
                    lblFile.Text = node.Value + "  [Date Modified: " + dt.ToLocalTime().ToString() + "]";
                    ShowEdit(true);
                }
                else
                {
                    lblDateMod.Text = "Current File/Dir Last Modified on: " + dt.ToLocalTime().ToString();
                    ShowEdit(false);
                }
            }
            else
            {
                FileInfo fi = new FileInfo(node.Value);
                System.DateTime dt = fi.LastWriteTimeUtc;
                if (!System.TimeZone.CurrentTimeZone.IsDaylightSavingTime(dt))
                    dt = dt.AddHours(-1);
                lblDateMod.Text = "Current File/Dir Last Modified on: " + dt.ToLocalTime().ToString();
                ShowEdit(false);

            }
        }

Is what I am trying to do possible i.e. align a gridview with Date Modified for Treeview files? When I run the application I am not getting any data returned to the Gridview. At which point should I be filling the GV.datasource?


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>