Maintaining Treeview State in Asp.Net 2 with a Sitemap
For a couple of days, I was looking around for a way to expand a treeview menu to the node you are currently on. On a site with nodes that go several levels deep, it is a pain to have to open all the nodes every time a postback occurs.
I found code samples online and followed so many goosechases. Some people put a marker in the session to let them know where their node was. Others tried to do some clever javascript which didn't seem to work for me. It seems to be some code that is heavily in demand.
Finally, I found a blog post by Walt Ritscher which helped me get what I wanted in a few lines of code. It doesn't maintain the state of all open and closed nodes on the tree, but it does open the tree to the node with the URL of the page you are on. It took me so long to find his blog post from last year, that I thought I would add another link here:
http://waltritscher.com/blog/ramblings/archive/2006/05/18/843.aspx
His examples are in VB.Net, but a conversion to c# is not difficult. Here are my code examples:
In the .aspx page:
<asp:TreeView ID="TreeView1" ExpandDepth="0" OnTreeNodeDataBound="treeMainMenu_TreeNodeDataBound" PopulateNodesFromClient="false" NodeWrap="false" HoverNodeStyle-ForeColor="green" HoverNodeStyle-Font-Underline="true" NodeIndent="10" Height="100%" runat="server" DataSourceID="SiteMapDataSource1">
</asp:TreeView>
Notice the "OnTreeNodeDataBound" and "PopulateNodesFromClient" attributes.
In the code behind:
protected void treeMainMenu_TreeNodeDataBound (
Object sender,
System.Web.UI.WebControls.TreeNodeEventArgs e )
{
if (Request.Url.PathAndQuery == e.Node.NavigateUrl) {
e.Node.ExpandAll();
}
if (e.Node.NavigateUrl == "") {
e.Node.SelectAction = TreeNodeSelectAction.Expand;
}
}
Anyway, thanks to Walt for his blog post. I looked over some other posts in his blog and found it very interested. You might want to have a look too.
Microsoft Learning has Free WPF course
Microsoft learning has their WPF online course available for free. It will probably cost money at some time in the future after Vista is released.
The course is 6 hours long and pretty comprehensive. You might want to check it out:
https://www.microsoftelearning.com/eLearning/offerDetail.aspx?offerPriceId=109340
WPF and XAML
So I wrote yesterday a little bit about how difficult designing a GUI can be for a developer. We deliver the world of functionality, but get nagged about the font style. I once argued for hours with a business user whether a certain function should have been launched with a button or a hyperlink.
With Windows Vista, Microsoft will be releasing the Windows Presentation Foundation classes. These will be in Vista by default, but can also be installed on XP. WPF will be one quarter of the release being billed as ".Net 3.0" (although you will find much debate on the web as to whether or not it deserves the label--it's not really a new version of .Net).
WPF will add a new layer to Windows presentation. A new XML protocol called XAML (pronounced Zammel) will allow developers or designers to design GUI's with a markup language similar to HTML.
If I want a button that says Click Me, the XAML would look like this:
<Button HorizontalAlignment="Left" Margin="122,194,0,228" Width="116" x:Name="btmMyButtonName" Content="Click Me!"/>
By separating out the design from development, actual I've-been-to-art-school designers can develop GUI's and developers can build robust systems.
If this was all that XAML could do, there would be a huge 'so what' factor, but the advances in GUI design are pretty spectacular.
One of the things that really used to bother me in the VB6 days was that every application we'd ever build would look that same. It was a square box with other square boxes on it. A lot of the time it was gray. We used to argue about whether to keep the system colours (and allow users to adjust the look of the form with their desktop theme) or jazz it up with some bitmaps and change the background color. Nothing we'd design would ever look like a cool app from the movies (Like, when someone gets an email in the movies, a giant envelope flies around the screen and opens up in front of them). Our apps were boring. .Net apps are that big an improvement. Although you can inherit the Form class and make a round form or something, it was more trouble than it was worth.
With XAML, the designer starts with a blank page and builds a GUI from thin air. He or she can easily change the appearance of all buttons, forms, listboxes, etc.
For designers, this would be as easy as drawing pictures on a palette.
Microsoft is releasing a suite of tools called Microsoft Expression. The three tools in this suite include Graphic Designer, Interactive, and Web Developer.
The Microsoft Expressions Graphic Designer application is basically a paint programme like Photoshop or PaintShopPro. The only difference is that it will export Vector graphics in XAML format which can easily be loaded into Expressions Interactive.
Expressions Interactive Designer is an application that allows the designer to build GUI's with a WYSIWYG editor. This produces the XAML--including mouseovers, button clicks, etc.
The third tool in the suite, "Web Designer", is used for Web applications. (obviously).
I've always prided myself on being an HTML guy. I don't do WYSIWYG. I've never trusted WYSIWYG-generated code to render consistently in all browsers. However, I think I will go the Expressions route when building XAML. Since we are talking single platform--single runtime-- I see no reason not to use it.
I'll save a post for Microsoft Expressions for another time. If you want to see Microsoft's official Expressions website, click here. I've downloaded RC1 of the software and don't have any problems with it so far.
MSDN Evenings in Reading
A few years ago, I used to attend loads of MSDN evening event held at Microsoft Campus in Reading in the UK. They are basically a little lecture on a developer-centric topic with a small intermission and dinner. I've been to events about Asp.net, Biztalk server, Office XP, etc.
I just got back from my first Microsoft event for about three years.
Tonight's topic: Windows Vista for Developer Features Beyond .Net.
The presentation was given by Daniel Moth and I was very impressed with the presentation.
One of the reason's I haven't been to many events recently is that I figured "well, I can just watch a webcast anytime I want." But, I'm starting to realise some of the flaws with that theory. There is a big difference between watching a webcast and attending an event live.
Watching a webcast does not demand your full attention. Usually, the speaker, without an audience to feed off of, speaks in a monotonous voice and literally reads of power point slides. I often find it difficult to pay close attention and will start shifting through papers on my desk, etc. In a webcast, you can't hear the reactions of the audience. With Microsoft products, it is not uncommon to hear developers groan at some of the claims made of the product. It's always nicer to do this collectively.
Now, I don't think all webcasts are bad. The DNRTV screencasts are very interesting with more code than slides and you get to witness Carl Franklin's sense of humour when talking about otherwise dry subjects. I also find Rory Blythes screencasts on Channel9 to be very entertaining. But usually, booooring.
Tonight's MSDN presentation by Daniel Moth was very good. No one looked bored, everyone enjoyed the stealth jokes, and I didn't once feel like I was being fed marketing information on how great Vista would be. He was very objective with how things should work.
Another nice thing about attending a live event is hearing other people's concerns and questions that had not occurred to me yet. I also get to hear the usual developer cynicism. While some, like myself, obviously thought incorporating RSS feeds into our applications would be a great idea, others claimed they couldn't see the reason for it.
When we were looking at the new Vista Sidebar, someone asked why anyone would use it when they had Google Sidebar. A collective groan filled the room.
Attending a live event also gives you the opportunity to chat with other like-minded developers. Great.
If you like in the UK near Reading or London, I highly recommend attending a Microsoft event.
