Thursday, September 23, 2010

Retain TreeView Navigation state after postback in

                                                 I have read one of the article in  in Microsoft site about how  to   Persistent TreeViews navigate among pages .But it is not fit for  my application .The  code is very heavy logic .The application is going to slow (Some time may not respond)because of Maintain the state of tree view control using sessions (Which is burden on Web server ).Every  post back  the tree view control  view-state going to increase.hmmm.... .
                                                I'm trying to keep my Master Page as "light" as possible without using a lot of code, because I don't want to slow down the entire site.And Tree-view will be converted to post back every time you click a node .

                                                I'm sure it can be simpler. Another idea was to put Tree View in the i Frame - similar to msdn web site, but that creates some issues with design and SEO.
                                        So i started to refactor the code and architecture as per my project requirement .
Here is the advantages of new code .
New Improvements

1.    No more round trips between client and server when selection node changed  .
2.    No state maintain in web server.
3.    No more loops for saving and restoring the view state in session
4.    Tree view without view state (Big advantage)
5.    More than six methods/events are removed
6.    Implemented client side redirection instead of server side 

Note: In my application i have created userControl for leftnavigation and in that i have a tree view control

Leftnavigation treeView mark up page

 <asp:TreeView ID="TreeviewNavigation" runat="server" NodeWrap="true" EnableViewState="false"

            ExpandDepth="0" ShowLines="true" HoverStyle="border=solid 1px;color=black;background:white;font-size=16;font-weight:bold">

            <SelectedNodeStyle CssClass="SelectedNode"></SelectedNodeStyle>


In code behind logic

       protected void PagePreRender(object sender, EventArgs e)







            catch (Exception ex)


               // Exception handling logic



       public void FillLeftNavigation()



            TreeviewNavigation.ShowExpandCollapse = true;

            // set the default state of all nodes.



        private void ExpandTreeView(TreeNodeCollection nodes)


            foreach (TreeNode node in nodes)


                //Modifying the url string according to tree view not value format

             string reqNodeValue = HttpContext.Current.Request["ReqVal"];   // Here we are sending treeview selected node value through Query string


                 if (node.Value.ToString() == reqNodeValue)                 //Compare the value with tree view nodes


                    node.Selected = true;                                   //If value is equal expand it



                if (node.ChildNodes.Count > 0)






        private void ExpandParent(TreeNode node)


            if (node != null)






Treeview node without firing the _doPostBack script

                                              I have a treeview and in the page load event I populate the treeview completely. I do not have any server side event for the treeview. Butstill when the user click on a node, the treeview page post back. How can I disablle this annoying autopostback? There seems no AutoPostBack= false property for a asp:treeview.I have tryied different ways to handle it but no use.There is a simple way to handle it .Here is the code for remove the _doPostback client side script.

                                              Use navigate url properti in treeview control to remove the _doPostBack.I have given sample code use this .you can specify valid url based on your requirement

Tuesday, September 21, 2010

Data at the root level is invalid. Line 1, position 1


The problem is that strings are stored internally as UTF-16 in .NET however the encoding specified in the XML document header may be different. E.g.:

<?xml version="1.0" encoding="utf-8"?>

              Each Unicode character in a string is defined by a Unicode scalar value, also called a Unicode code point or the ordinal (numeric) value of the Unicode character. Each code point is encoded using UTF-16 encoding, and the numeric value of each element of the encoding is represented by a Char object.

This means that when you pass XmlDocument.LoadXml() your string with an XML header, it must say the encoding is UTF-16. Otherwise, the actual underlying encoding won't match the encoding reported in the header and will result in an XmlException being thrown.


The solution for this problem is to make sure the encoding used in whatever you pass the Load or LoadXml method matches what you say it is in the XML header. In my example above, either change your XML header to state UTF-16 or to encode the input in UTF-8 and use one of the XmlDocument.Load methods instead of XmlDocument.LoadXML method.

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(ConfigurationManager.AppSettings["CatalogXMlRootPath"] + Catalog + ".xml");
return xmlDocument;

Check list:

  • Check the given xml file having any unclosed tags
  • Check wheather xml file starting line having any empty line.
  • have you given enough permission to xml folder
  • Use xmldocument.Load instead of LoadXMl method

Monday, September 13, 2010

Showing default image when image not found


         Recently we have found one problem in our  application when  image is not available the   firefox not showing any thing .In IE just showing Red cross mark Which is not good from client prospective  .
                                 So What I want to do is have a "default" image that is displayed to anyone who requests a image that is no longer in a specific directory. (basically any 404 request in a specific directory).

                                Don't write any server side   logic for  reading external web server image  file .You may or may not have permission to read .Just use java script "onerror " event (I have given sample code below )It will solve your problem.

              It will work for any web server .No need to bother about the external web server permission and IpAddress .Let me know if you need any help or clarification .


 <asp:Image ID="imgHeaderLogo" runat="server" onerror="this.src='';" />


 In this.src you can specify required dimensional default image based on your requirement .