Showing posts with label directory. Show all posts
Showing posts with label directory. Show all posts

Wednesday, March 28, 2012

TreeView and UpdatePanel

I create a web form to display directory structure of the disks of the server. I put a treeview on the form and fill it with the drive and directory names. Everything was fine until I put a UpdatePanel (and ScriptManager ofcourse) on the form and move the TreeView into it. Now, I have a problem with the first node of the treeview (C:\). When I expand that node I see all the directories in the root of the C: drive. But when I expand any directory, all the nodes collapses. Than, other root nodes (D:, E: etc) works properly, but C: is not. What is the thing I miss? I use Visual Studio 2005 and AJAX January CTP. My code is as follows;

protectedvoid Page_Load(object sender,EventArgs e)

{

string[] drives;int i;TreeNode node, initialChildNode;DriveInfo driveInfo;if (!Page.IsPostBack)

{

tvFileSystem.Nodes.Clear();

drives =

Environment.GetLogicalDrives();for (i = 0; i < drives.Length; i++)

{

node =

newTreeNode();

driveInfo =

newDriveInfo(drives[i]);try

{

node.Text = driveInfo.VolumeLabel +

"(" + drives[i] +")";

}

catch

{

node.Text = driveInfo.DriveType +

"(" + drives[i] +")";

}

node.Value = drives[i];

initialChildNode =

newTreeNode();

initialChildNode.Text =

".";

initialChildNode.Value =

".";

node.ChildNodes.Add(initialChildNode);

tvFileSystem.Nodes.Add(node);

node.Collapse();

}

tvFileSystem.CollapseAll();

}

}

protectedvoid tvFileSystem_TreeNodeExpanded(object sender,TreeNodeEventArgs e)

{

TreeNode node = e.Node;TreeNode initialChildNode, newNode, tempNode;DirectoryInfo dirInfo;DirectoryInfo[] subDirInfo;string path ="";string[] subDirs;int i;string dirName, js;if (node.Value ==".")return;

tempNode = node;

while (tempNode !=null)

{

path = tempNode.Value +

"\\" + path;

tempNode = tempNode.Parent;

}

try

{

dirInfo =

newDirectoryInfo(path);

subDirInfo = dirInfo.GetDirectories();

}

catch (Exception e1)

{

js =

"<script>alert('" + e1.Message.Replace("\r","").Replace("\n","") +"');</script>";

Page.ClientScript.RegisterStartupScript(

typeof(string),"expandError", js);return;

}

node.ChildNodes.Clear();

for (i = 0; i < subDirInfo.Length; i++)

{

newNode =

newTreeNode();

dirName = subDirInfo[i].Name;

newNode.Text = dirName;

newNode.Value = dirName;

try

{

subDirs =

Directory.GetDirectories(subDirInfo[i].FullName);if (subDirs.Length > 0)

{

initialChildNode =

newTreeNode();

initialChildNode.Text =

".";

initialChildNode.Value =

".";

newNode.ChildNodes.Add(initialChildNode);

}

node.ChildNodes.Add(newNode);

}

catch { };

}

}

And the aspx file :

<%

@dotnet.itags.org.PageLanguage="C#"AutoEventWireup="true"CodeFile="dirTree.aspx.cs"Inherits="dirTree" %>

<!

DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<

htmlxmlns="http://www.w3.org/1999/xhtml">

<

headrunat="server"><title>Untitled Page</title>

</

head>

<

body><formid="form1"runat="server"><div><asp:ScriptManagerID="ScriptManager1"runat="server"></asp:ScriptManager>

</div><asp:UpdatePanelID="UpdatePanel1"runat="server"><ContentTemplate> <asp:TreeViewID="tvFileSystem"runat="server"OnTreeNodeExpanded="tvFileSystem_TreeNodeExpanded"Style="position: static"><SelectedNodeStyleBackColor="NavajoWhite"/><NodeStyleForeColor="Black"/></asp:TreeView></ContentTemplate></asp:UpdatePanel></form>

</

body>

</

html>

From what I have read, Treeviews and Menus do not work inside UpdatePanels. I did, however, get the menu to work without posting back by keeping the menu out of the UpdatePanel, but calling the appropriate menu event from the updatepanel.

<asp:UpdatePanel runat="Server" id="upSomeUpdatePanel" UpdateMode="Conditional">
<contentTemplate>
</contentTemplate>
<Triggers>
<AutoSyncPostBackTrigger ControlID="TreeViewID" Event="treevieweventname" />
</triggers>
</asp:UpdatePanel>

Don't copy and paste my code above because I did it all by memory and some of the control parameters might have slightly different names, but I hope you get the drift. The "treevieweventname" would be the name of the event you want to do without posting back - like "TreeNodeExpanded".

Hope this helps.


I have a treeview that works fine inside of an update panel.


What I'm doing differently is I'm not using the Expanded event,I'm using the TreeNodePopulate event and setting each node thathas children's "PopulateOnDemand" property to true. MyTreeView is inside an update panel and it expands, collapses,dynamically populates, etc. just fine.


By the way, I created a thread in this forum with a working example of a TreeView in an update panel using the technique I mentioned. I used the FileSystem since it's easy and doesn't requrie a database, it isn't exactly like yours but it works. It should show up soon upon moderation. Let me know if that helps!


Thank you aaron,

Your example is very helpful for me. But, what if I want user to select a node. I need to hold the selected node value, for example in a HiddenField. I modified your example by removing "Selected.Action = TreeNodeSelectAction.None" lines. And I add SelectedNodeChanged Event which includes following code:

hdSelectedPath.Value = tvFiles.SelectedNode.Value;

Now, it works except some thing. If I selects a node, then expands another node and select another node, the selected nodes changes in a wrong way. I mean, any other node is selected which I did not select.

What is the point I miss?

Thanks.


Thank you aaron,

Your example is very helpful for me. But, what if I want user to select a node. I need to hold the selected node value, for example in a HiddenField. I modified your example by removing "Selected.Action = TreeNodeSelectAction.None" lines. And I add SelectedNodeChanged Event which includes following code:

hdSelectedPath.Value = tvFiles.SelectedNode.Value;

Now, it works except some thing. If I selects a node, then expands another node and select another node, the selected nodes changes in a wrong way. I mean, any other node is selected which I did not select.

What is the point I miss?

Thanks.

Monday, March 26, 2012

Treeview TreeNodePopulate event.

I have recently installed the AJAX Beta 2 Extensions having used the ATLAS CTP. Having installed it I removed the DLLs from the Bin directory. On two of my pages in the application I am using the Treeview controls which only populate child nodes when each of the parent nodes are clicked on. On one page this the treeview works fine and populates on demand -

1protected void ATreeview_TreeNodePopulate(object sender, TreeNodeEventArgs e)
2 {
3switch (e.Node.Depth)
4 {
5case 0:
6break;
7case 1:
8 PopulateTreeview(e.Node);
9break;
10 }
11 }


The PopulateTreeview method takes the node that should be populated (which for e.g. is the Category) and creates all the product nodes. At the moment the TreenodePopulate event does nothing as I just want to see whether it actually raises the event. When I discovered the problem I took out all the code in this event. The code at the moment is as follows:

1protected void ATreeview_TreeNodePopulate(object sender, TreeNodeEventArgs e)
2 {
3 lblMessage.Text ="inside the treenode populate " + e.Node.Depth.ToString();
4 lblMessage.Visible =true;
5 }

When I debug this and expand a node I get the following error:


"Microsoft JScript runtime error: 'ctl00_ContentPlaceHolder1_tvUsersNoGroup_Data' is undefined" - I have no choice but to press Break and stop the application from running. In the IDE it just highlights this line TreeView_PopulateNode(ctl00_ContentPlaceHolder1_ATreeview_Data,1,ctl00_ContentPlaceHolder1_ATreeviewn1,ctl00_ContentPlaceHolder1_ATreeviewt1,null,'t','A Category','TopNode\\3016','f','','tf')

The design source code is as follows:

<asp:UpdatePanel ID="upUsers" runat="server"> <ContentTemplate> <table border="0" class="report_treeview"> <tr style="background-color:#6178BE; color: #FFFFFF;font-weight:bold; font-size:80%;text-align:center;"> <td>Users not in selected security group</td> <td><asp:DropDownList ID="ddSecurity" runat="server" AppendDataBoundItems="True" AutoPostBack="true" OnSelectedIndexChanged="ddSecurity_SelectedIndexChanged"> <asp:ListItem Selected="True" Text="Please select a security group" Value="0"></asp:ListItem> </asp:DropDownList></td> <td>Users in selected security group</td> </tr> <tr> <td style="vertical-align: top; width: 300px; text-align:left;"> <asp:TreeView id="ATreeview" runat="server" showlines="true" visible="false" OnTreeNodePopulate="tvUsersNoGroup_TreeNodePopulate"></asp:TreeView> </td> <td style="border-right: solid 1px #DDDDDD;border-left: solid 1px #DDDDDD;text-align:center;"> <asp:Button ID="btnAddUser" runat="server" Text=">>" CssClass="option_button" OnClick="btnAddUser_Click" ToolTip="Add selected users." /> <p /> <p /> <asp:Button ID="btnRemoveUser" runat="server" Text="<<" CssClass="option_button" OnClick="btnRemoveUser_Click" ToolTip="Remove selected users." /> </td> <td style="vertical-align: top; width: 300px; text-align:left;"> <asp:Label ID="lblStatus" runat="server"></asp:Label></td> </tr> </table> <p /><asp:Label ID="lblMessage" runat="server" CssClass="labelStatus"></asp:Label> </ContentTemplate> </asp:UpdatePanel>
Anyone else come across this problem? Why is my other page fine and this page is throwing this error?

I did have something like this. I would get an error on the source code saying it the object was undifined when it was trying to put the tooltip on the treenode. To 'fix' it I turned off script debuging on my browser, and it went away.

It may be something to try

Eric Wild


Thanks for the suggestion but I don't think that the problem can be solved by suppressing the errors from the browser.

What I would like to know is if other people have had the same problems as myself.

Is this a problem with the AJAX Beta 2? or my silly code?


I am encountering this issue right now and know it is related to the Visible="false" on the TreeView.

I'm using RC1 and have a Button in an UpdatePanel and a TreeView in another UpdatePanel. The TreeView has it's Visible property = "false".

When the Button is clicked, it sets TreeView's Visible="true".

At this point, if you try to Expand a TreeView Node, it will come back with that same debugging trap you encountered. I tried using Fiddler to see if it passed anything, but it didn't.

You could either keep the TreeView's Visible property = "true" from the start or set the TreeView's EnableClientScript="false".

I think this deserves a bump and hope someone can further explain why and perhaps offer a workaround because the concept that the TreeView only populates its expanding Node is very nice.

Nathan

Wednesday, March 21, 2012

Trouble getting Value out of DynamicPopulate Populated Control

Hi:

I'm using a combination of AutoComplete and DynamicPopulate out of the AJAX Toolkit. The AutoComplete polls my Active Directory, then sends the selected name to the DynamicPopulate control. The DP control then sends me back an email address. Since the DP control will not populate a textbox's "Text" value, I decided to use a label. The label populates properly on the page, however, when I try to pull the value through my code, the label's text value is "". All population using the DP is done before the call for the values. Does anyone have any suggestions?

The label is being populated after the page loads; ASP.NET is a server side language, since the page hasnt posted back to the server yet, your code still thinks the value is empty.

Thanks for your reply. I understand why I can't get the value out, and I'm trying to prevent a postback, however, is there any possible other way to get these values out? I've tried a javascript "hacky" way to get the value into an HTML control, then finding the control to get it's value, but that's proving to be quite a feat in itself. Any suggestions?

Here's the set up:

I have a web service with two methods. One goes to my Active Directory and does AutoComplete on the name. Onblur, it calls the other method which takes the text in the AutoComplete text box and sends it back through the directory, which then returns the email address. A DynamicPopulate control populates a label (which is going to be hidden) in order for me to grab the email address. My structure is lenient, so any suggestions would be greatly appreciated!

Thanks

John


I'm having somewhat the same problem!

I use the DynamicPopulateextender to populate at Panel with information from a webservice. It works just fine but now i want to fetch the info and save it. If u get it to work, please tell me :)