ThreadAbortException

August 2005 Entries

Introducing Microsoft Windows Presentation Foundation (Avalon)

Windows Presentation Foundation (formerly code named Avalon) is the Microsoft unified presentation subsystem for Windows, consisting of a display engine and a managed-code framework. Windows Presentation Foundation unifies how Windows creates, displays, and manipulates documents, media, and the user interface, enabling developers and designers to create visually stunning, personalized user experiences that improve the application connection with the user.

XAML, a markup language to represent user interfaces declaratively for Windows applications, will also be introduced with Windows Presentation Foundation, improving the richness of the tools for composing and repurposing user interfaces. For Web developers, XAML provides a familiar UI description paradigm. XAML also enables the separation of UI design from the underlying code, enabling developers and designers to work more closely.

Using XAML, Creating a Button is as simple as

<Button Width="100"> OK
<Button.Background>
LightBlue
</Button.Background>
</Button>


As we can see, XAML extends the power of XML and makes declarative programming which is very useful for reducing the amount of work done currently for server side programming.

Also, you can attach events to the XAML Button and have an associated Codebehind in C# or VB.NET, whatever may be

XAML Declaration
<Button Click="Button_Click">Click Me!</Button>

C# code-behind file
void Button_Click(object sender, System.Windows.Controls.ClickEventArgs e)
{
btn1.Background = System.Windows.Media.Brushes.Red;
}

For developers and designers, Windows Presentation Foundation provides a unified UI platform, such that they can learn a single paradigm which provides limitless possibilities of UI experiences.

Avalon leverages on WinFX which is the next set of APIs which will be available in Windows Vista (Longhorn) and the power of XAML to render powerful rich graphics to improve the user experience.

More information can be found at
Create the Experience

Windows Presentation Foundation and Windows Vista surely will provide a new experience of Development / Usability for the years to come.

Treeview - XML, XPATH - Show part of XML Document

Treeview is one of the important controls being used in applications to improve the usability experience for users for displaying Hierarchial Data.

ASP.NET 1.x versions did not ship with a built-in treeview control as a part of the Web Controls. However Microsoft did release a set of UI Controls including the Treeview, Tabstrip Control etc., collectively called as Microsoft IE Webcontrols.

Though Microsoft doesnt support these controls currently (as mentioned in their MSDN Documentation), those controls do provide a great resource for developing web applications with rich user experience.

Treeview is one such component which is very useful in representing Hierarchial Data (mostly based on XML structure).

Whidbey (ASP.NET 2.0) does come with a built-in TreeView control, Menu Control etc.,which are part of the current IE Webcontrols and I guess that much be one of the reasons for Microsoft to stop supporting the existing IE Webcontrols which have a lot of limitations.

However, even with the current TreeView control we can very well utilize the Hierarchial structure of representing data.

You can bind a Treeview to an XML Document (which is a common practice since XML is again a hierarchial data representation). But with the current Treeview control, the XML needs to adhere to certain standards.

The Root Node must be <TREENODE> and it MUST be in Caps. Otherwise, it would raise a "did not contain the required outer <TREENODES> container. " Error.

The Node elemets much start with the element name <treenode> and must have the attribute Text which is the text displayed in the TreeView Node. You can as well enhance the look and feel by specifying ImageURL, CheckBox, NavigateURL etc.,

However, on some occasions we would like to show only a portion of the XML Document. Say you want to show only certain set of Nodes based on a condition. An xml document may contain a lot of nodes from which we would like to display particular nodes. That is a little tricky since you need an XML file with the strucutre as explained above for the TreeView to display properly.

I give herebelow the steps involved in selecting particular nodes of an XML Document to display in TreeView. I am using XPATH to select particular set of nodes. If you are unfamiliar with XPATH, it is a W3C Standard for querying and retrieving XML Data from XML Documents. XPATH is fully supported in .NET and there are lots of classes and methods provided with XPATH to navigate and query XML Documents. More information on XPATH and an excellent Tutorial can be found at
XPATH Tutorial

Let us consider the following XML Tailored to suit the format required for TreeView

<TREENODES>
<treenode Text="Books" ID="1" Category="Fiction">
<treenode Text="Look Into Future" ID="1"/>
<treenode Text="What Tomorrow would be?" ID="1"/>
<treenode Text="Books" ID="1"/>
</treenode>
<treenode Text="Books" ID="2" Category="History">
<treenode Text="American History" ID="1"/>
<treenode Text="Indian History" ID="1"/>
<treenode Text="Japanese History" ID="1"/>
<treenode Text="UK History" ID="1"/>
</treenode>
<treenode Text="Books" ID="3" Category="Cartoon">
<treenode Text="Tom and Jerry" ID="1"/>
<treenode Text="Mickey Mouse" ID="1"/>
<treenode Text="Donald Duck" ID="1"/>
</treenode>
<treenode Text="Books" ID="4" Category="Mystery">
<treenode Text="Da Vinci Code" ID="1"/>
<treenode Text="The Wheels" ID="1"/>
<treenode Text="Chasing a Crooked Shadow" ID="1"/>
</treenode>
</TREENODES>


We will save the above file as "Books.XML". Note that the above XML already has the relevant format for binding to the TreeView as per its requirement. We will see in future articles on how to transform a generic XML format into one required for the TreeView as above.

Now, the code to select only Books of Category "Mystery" and display in the TreeView.

using System.IO;
using System.XML;


StringWriter sWriter = new StringWriter();
// Creating a String Writer object

XmlTextWriter xWriter = new XmlTextWriter(sWriter);
// Creating a Xml Writer object

XmlDocument xDoc = new XmlDocument();

xDoc.Load(Server.MapPath("Books.XML"));
//Loading the original XML Document

XmlElement xRoot = xDoc.DocumentElement;
// Creating the Root Element.

string xPathExpr = "//treenode[@ID=4]/descendant-or-self::*";
// XPATH Expression to select specific nodes.

XmlNode xNode = xRoot.SelectSingleNode(xPathExpr);

//Selecting the Node which is the parent node matching the XPATH Expression

if(xNode != null)
{

XmlDocument xNewDoc = new XmlDocument();

xNewDoc.LoadXml("<TREENODES></TREENODES>");
//Creating a Dynamic XML to attach the TREENODE Element to the specific node set.

XmlNode xImpNode = xNewDoc.ImportNode(xNode, true);
//Creating and importing the Node from the original XML Document. The true value indicates, all the child nodes for this node will be selected.

xNewDoc.DocumentElement.AppendChild(xImpNode);
//Appending the Nodes Imported to the Dynamic XML Document.

xNewDoc.WriteTo(xWriter);
//Writing the Appended Nodes to the XML Writer

TreeView1.TreeNodeSrc = sWriter.ToString();
//Assigning the TreeNodeSrc property with the StringWriter which contains the XmlText

TreeView1.DataBind();
}

Though for simplicity I have a 2 level Hierarchy XML Structure, the above code would select all TreeNode elements having attribute ID with the value 4 irrespective of where the Nodes are there in the Tree Hierarchy ie., even if there are child nodes containing Book Details, they will also be selected to give the Tree structure (Parent - Child - Child structure)

This would facilitate in showing only specific portion of an XML which is currently difficult to implement with the IE Webcontrols.

As already explained, this has been much more simplified in ASP.NET 2.0 (Whidbey) which ships with a built-in TreeView control containing a property to specify the XPATH Expression for selecting specific nodes.

Cheers and Happy Tree Viewing !!!

 

You may receive the error: "The underlying connection was closed: Unable to connect to the remote server." while consuming a Webservice

You may receive the error "The underlying connection was closed: Unable to connect to the remote server." while trying to consume a Web Service from your ASP.NET Web Application. However, the same Web Service can be consumed from a Windows Application without any issues.

This error particularly occurs if you are behind a firewall or proxy. When you use a winforms app, it can autodetect the proxy using IE settings for current user, and it can connect out through the proxy. However, when in asp.net, it cannot because the asp.net user identitiy does not have the correct proxy settings.

To resolve this issue, you need to explicitly specify the proxy settings for your application. You can do it at various levels viz., at the Machine.Config file which will apply for all the applications running on the system, at the Web.Config file such that it applies for a single application, and at the Page Level programattically in the code behind / code inline.

Web.Config setting

<configuration>
<system.net>
<defaultProxy>
<proxy
usesystemdefault = "false"
proxyaddress="http://address:port"
bypassonlocal="false"
/>
</defaultProxy>
</system.net>
<system.web>


For Machine.Config use the same settings as above.

To do it programmatically,

using System.Net;

WebServiceClass MyWebServiceClass = new WebServiceClass();
WebProxy proxyObject = new WebProxy("http://address:port", true);
MyWebServiceClass.Proxy = proxyObject;
MyWebServiceClass.MyWebMethod();


where WebServiceClass is the name of the Web Service Class which you are consuming.

That should solve the issue with consuming the Web Service across Proxy Settings.

Cheers and Happy Web Servicing !!!

India MVP Nominations - Now Open !!!

Good news for Technology Experts and Community Crackers. The Nomination Cycle for MVP Program for this quarter is now open.

Check
Microsoft Most Valuable Professional for more information.

For those who are new to MVP Program,

Each year, Microsoft gives out an award called the Microsoft MVP (Most Valuable Professional) Award to outstanding members of a Microsoft online Developer Forum. The MVP Award is given in recognition of a recipient's technical expertise, spirit of community, and their willingness to help their peers by providing reliable and accurate answers to technical questions about Microsoft products & technologies.

So, if you are a person who is passionate to help others in online forums, communities and have the expertise to solve easy to complex queries posted by 1000s of people in the discussion forums and guide them through to solution, you can nominate yourself for the program.

It is a honor to be an MVP and is certainly a great recognition from Microsoft for those sparkling talented professionals who take the extra effort to exceed and help their peers.

For more information on benefits and to nominate, please check
MVP Nomination

Cheers and Best Wishes for your Nominations !!!