ThreadAbortException

January 2009 Entries

Updated: ASP.NET TreeView CheckBoxes – Check All – JavaScript

 

Updated the post with the XML File I had used.

I posted this article originally in March 2006 when the ASP.NET 2.0 TreeView had just been released.  This article has received tremendous response (35000 views for this particular post) and also many recommendations / suggestions / corrections as comments.

The script then supported checking/unchecking of children/parents when selecting/deselecting a particular node.  However, in cases of having multiple children and de-selecting one of them, the parent doesn’t automatically get un-selected.  Also there seemed to be a few issues with Firefox, Safari etc.,

So the intent of this post is to publish the best working Java Script (submitted by the community) that fixes the above issues as well as support  multiple browsers and provide a better way of handling the event.  Special thanks to pushp_aspnet for taking the pain to make this script work across multiple browsers and multiple levels.

To begin with, create a blank new ASP.NET Website and in your web form, drag and drop a TreeView control in to the page.  Also, drag and drop an XML DataSource control into your page (this can be found under “Data” tab in the Visual Studio toolbox).

Configure the DataSource to an XML File (I have pasted the XML Code at the end of this post) or any method that returns an XML from your Database etc., Bind the TreeView to the XML DataSource.  Also, set the ShowCheckBox=”All” property for the TreeView.  The sample code looks as below:-

<asp:TreeView ID="TreeView1" runat="server" DataSourceID="XmlDataSource1" ShowCheckBoxes="All">
        </asp:TreeView>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Syllabus.xml"></asp:XmlDataSource>

Note, I have set the Datafile to a physical XML file I had which has decent hierarchy of parent/child nodes. (As mentioned earlier, I have pasted this XML at the end of this post)

Just run the solution to check if your Tree renders on the page with the checkboxes as well as the node elements.

The next step is to add the TreeView attribute.  In the code-behind, within the Page_Load event, add the following code:-

protected void Page_Load(object sender, EventArgs e)
    {
        TreeView1.Attributes.Add("onclick", "OnCheckBoxCheckChanged(event)");
    }

The next step is to add the Java Script function to handle the above:-

In the ASPX Page, within the <head> </head> tags, add the following code:-

<script type="text/javascript">
    function OnCheckBoxCheckChanged(evt) {
        var src = window.event != window.undefined ? window.event.srcElement : evt.target;
        var isChkBoxClick = (src.tagName.toLowerCase() == "input" && src.type == "checkbox");
        if (isChkBoxClick) {
            var parentTable = GetParentByTagName("table", src);
            var nxtSibling = parentTable.nextSibling;
            if (nxtSibling && nxtSibling.nodeType == 1)//check if nxt sibling is not null & is an element node
            {
                if (nxtSibling.tagName.toLowerCase() == "div") //if node has children
                {
                    //check or uncheck children at all levels
                    CheckUncheckChildren(parentTable.nextSibling, src.checked);
                }
            }
            //check or uncheck parents at all levels
            CheckUncheckParents(src, src.checked);
        }
    }
    function CheckUncheckChildren(childContainer, check) {
        var childChkBoxes = childContainer.getElementsByTagName("input");
        var childChkBoxCount = childChkBoxes.length;
        for (var i = 0; i < childChkBoxCount; i++) {
            childChkBoxes[i].checked = check;
        }
    }
    function CheckUncheckParents(srcChild, check) {
        var parentDiv = GetParentByTagName("div", srcChild);
        var parentNodeTable = parentDiv.previousSibling;

        if (parentNodeTable) {
            var checkUncheckSwitch;

            if (check) //checkbox checked
            {
                var isAllSiblingsChecked = AreAllSiblingsChecked(srcChild);
                if (isAllSiblingsChecked)
                    checkUncheckSwitch = true;
                else
                    return; //do not need to check parent if any(one or more) child not checked
            }
            else //checkbox unchecked
            {
                checkUncheckSwitch = false;
            }

            var inpElemsInParentTable = parentNodeTable.getElementsByTagName("input");
            if (inpElemsInParentTable.length > 0) {
                var parentNodeChkBox = inpElemsInParentTable[0];
                parentNodeChkBox.checked = checkUncheckSwitch;
                //do the same recursively
                CheckUncheckParents(parentNodeChkBox, checkUncheckSwitch);
            }
        }
    }
    function AreAllSiblingsChecked(chkBox) {
        var parentDiv = GetParentByTagName("div", chkBox);
        var childCount = parentDiv.childNodes.length;
        for (var i = 0; i < childCount; i++) {
            if (parentDiv.childNodes[i].nodeType == 1) //check if the child node is an element node
            {
                if (parentDiv.childNodes[i].tagName.toLowerCase() == "table") {
                    var prevChkBox = parentDiv.childNodes[i].getElementsByTagName("input")[0];
                    //if any of sibling nodes are not checked, return false
                    if (!prevChkBox.checked) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
    //utility function to get the container of an element by tagname
    function GetParentByTagName(parentTagName, childElementObj) {
        var parent = childElementObj.parentNode;
        while (parent.tagName.toLowerCase() != parentTagName.toLowerCase()) {
            parent = parent.parentNode;
        }
        return parent;
    }
</script>

The above code should work fine when you run and try selecting/de-selecting nodes and the parent behaviors etc.,

When I last checked, it works with IE8 Beta 2, Firefox 3.0.5 & Safari 3.2.1

The link for my original post View Entry

The ASP.NET Forum where this is being discussed with various options http://forums.asp.net/t/976122.aspx?PageIndex=1

The sample XML File content is here below:-

<?xml version="1.0" encoding="utf-8" ?>
<Syllabus>
    <Classes>
        <Class>XII Standard</Class>
        <Subjects>
            <Subject>Physics</Subject>
            <chapters>
                <chapter>Chapter 1</chapter>
                <Description>This is Chapter 1 in Physics</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 2</chapter>
                <Description>This is Chapter 2 in Physics</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 3</chapter>
                <Description>This is Chapter 3 in Physics</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 4</chapter>
                <Description>This is Chapter 4 in Physics</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 5</chapter>
                <Description>This is Chapter 5 in Physics</Description>
            </chapters>
        </Subjects>
        <Subjects>
            <Subject>Biology</Subject>
            <chapters>
                <chapter>Chapter 1</chapter>
                <Description>This is Chapter 1 in Biology</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 2</chapter>
                <Description>This is Chapter 2 in Biology</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 3</chapter>
                <Description>This is Chapter 3 in Biology</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 4</chapter>
                <Description>This is Chapter 4 in Biology</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 5</chapter>
                <Description>This is Chapter 5 in Biology</Description>
            </chapters>
        </Subjects>
        <Subjects>
            <Subject>Chemistry</Subject>
            <chapters>
                <chapter>Chapter 1</chapter>
                <Description>This is Chapter 1 in Chemistry</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 2</chapter>
                <Description>This is Chapter 2 in Chemistry</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 3</chapter>
                <Description>This is Chapter 3 in Chemistry</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 4</chapter>
                <Description>This is Chapter 4 in Chemistry</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 5</chapter>
                <Description>This is Chapter 5 in Chemistry</Description>
            </chapters>
        </Subjects>
        <Subjects>
            <Subject>Maths</Subject>
            <chapters>
                <chapter>Chapter 1</chapter>
                <Description>This is Chapter 1 in Maths</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 2</chapter>
                <Description>This is Chapter 2 in Maths</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 3</chapter>
                <Description>This is Chapter 3 in Maths</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 4</chapter>
                <Description>This is Chapter 4 in Maths</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 5</chapter>
                <Description>This is Chapter 5 in Maths</Description>
            </chapters>
        </Subjects>
    </Classes>
    <Classes>
        <Class>X Standard</Class>
        <Subjects>
            <Subject>Maths</Subject>
            <chapters>
                <chapter>Chapter 1</chapter>
                <Description>This is Chapter 1 in Maths</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 2</chapter>
                <Description>This is Chapter 2 in Maths</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 3</chapter>
                <Description>This is Chapter 3 in Maths</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 4</chapter>
                <Description>This is Chapter 4 in Maths</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 5</chapter>
                <Description>This is Chapter 5 in Maths</Description>
            </chapters>
        </Subjects>
        <Subjects>
            <Subject>Social</Subject>
            <chapters>
                <chapter>Chapter 1</chapter>
                <Description>This is Chapter 1 in Social</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 2</chapter>
                <Description>This is Chapter 2 in Social</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 3</chapter>
                <Description>This is Chapter 3 in Social</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 4</chapter>
                <Description>This is Chapter 4 in Social</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 5</chapter>
                <Description>This is Chapter 5 in Social</Description>
            </chapters>
        </Subjects>
        <Subjects>
            <Subject>Science</Subject>
            <chapters>
                <chapter>Chapter 1</chapter>
                <Description>This is Chapter 1 in Science</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 2</chapter>
                <Description>This is Chapter 2 in Science</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 3</chapter>
                <Description>This is Chapter 3 in Science</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 4</chapter>
                <Description>This is Chapter 4 in Science</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 5</chapter>
                <Description>This is Chapter 5 in Science</Description>
            </chapters>
        </Subjects>
        <Subjects>
            <Subject>English</Subject>
            <chapters>
                <chapter>Chapter 1</chapter>
                <Description>This is Chapter 1 in English</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 2</chapter>
                <Description>This is Chapter 2 in English</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 3</chapter>
                <Description>This is Chapter 3 in English</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 4</chapter>
                <Description>This is Chapter 4 in English</Description>
            </chapters>
            <chapters>
                <chapter>Chapter 5</chapter>
                <Description>This is Chapter 5 in English</Description>
            </chapters>

        </Subjects>
    </Classes>
</Syllabus>

Cheers !!!

Microsoft Web Platform Installer v1.0

 

The Microsoft Web Platform Installer is now v1.0.   Simply putting, it provides you a single installer download of the web platform which includes IIS 7/116, Visual Web Developer Development Tool, SQL Server 2008 Express as well as the .NET Framework.

So, if you are running Windows XP, Windows Vista or Windows Server 2003/2008, it gives you all the stuff you require to start with web development.

Combined with this, the Web Application Installer which provides a lot of popular web applications such as the DotNetNuke, Graffiti etc., the Web Platform Installer is a great resource for starting your web development in Microsoft Platform.  Some of these also support open source and shared source development and even require installation of PHP framework.

If you are a startup web company, all you would require to begin web development would be one of the above operating systems.

To install the Web Platform visit http://www.microsoft.com/web/channel/products/WebPlatformInstaller.aspx

To install the Web Application Installer visit http://www.microsoft.com/web/channel/products/WebApplicationInstaller.aspx

For more information, walk through and forums, visit http://www.microsoft.com/web/channel/products/downloads.aspx

Cheers !!!

Issues with IE8 Beta 2? Opening blank popus?

 

Update: The RCI Version is now available for Download at http://www.microsoft.com/windows/internet-explorer/beta/ 

If you have installed the IE 8 Beta 2 and have been using it for sometime, you would have benefited some of the features such as the Accelerators, In-private browsing, instant search, web slices and other enhanced navigation features.  You can find more information on the same at http://www.microsoft.com/windows/internet-explorer/beta/features/overview.aspx?tabid=1&catid=1 and a detailed recorded online session, that we delivered some time back at http://www.virtualtechdays.com/pastevents_2008november.aspx

As much as people have loved the new features and shared positive feedback, there have also been issues that have been reported, many of them owing to compliance standards not followed by the websites and hence the resulting poor site rendering.

To clarify, IE8 follows the latest web standards and is CSS 2.1 compliant and hence it makes rendering of websites that don’t follow these standards look poorer or break.  However, there is a “compatibility view” icon next to the refresh icon at the end of the URL window that makes these sites look better since they are designed for earlier versions of the browser.

There was one particular issue that surfaced with respect to popups where IE opens up blank popups and loses the popup URLs.  This issue seems to have been fixed in the Beta 2 build and at least I no longer surface this issue.  If you have issues even with the Beta 2 build, try removing it and reinstalling the same.  (You can remove IE 8 in Windows XP from Add-Remove Programs and in Windows Vista from “View Installed Updates” in Add-Remove Programs)

If you have doubts on the version that you are running, open Internet Explorer, click on the “Help” menu and select “About Internet Explorer”.  The version number should read as 8.0.6001.18241  (note the Help menu will show up, if you press “Alt” in case you aren’t able to see the File menu in the top)

Note that in case you are running Windows 7 Beta, IE 8 comes built-in and the version number of it is 8.0.7000.0

If you are a developer targeting IE8, the MSDN http://msdn.microsoft.com/en-us/ie/default.aspx forum should give you valuable information and resources

To follow the IE Team, visit or subscribe to http://blogs.msdn.com/ie/

Cheers !!!

Running the ASP.NET Windows Live Web site in IIS7, Could not load file or assembly 'System.Web.Extensions, Version=3.6.0.0,” Error

 

When I was trying to run the Windows Live Enabled Website that is shipped with the Windows Live Tools for Visual Studio November 2008 CTP (I had blogged about this earlier at http://geekswithblogs.net/ranganh/archive/2008/12/01/windows-live-tools-for-visual-studio-2008.aspx and you can download the CTP from http://dev.live.com ) against IIS 7, I got the error “Could not load file or assembly ‘System.Web.Extensions, Version=3.6.0.0….” error. 

Interestingly this error wasn’t showing up if I create the website in the File Folder that doesn’t use the IIS 7 hosting service.  I had earlier chosen “File – New Website – ASP.NET Windows Live Website” and selected the location as “HTTP” and created the site.  When I was trying to debug, I ran into this issue.  Similarly, when I created a regular ASP.NET Website and tried to add the Windows Live Controls from the ToolBox, I didn’t face this issue.

Apparently, the versioning of the DLLs under the system.webserver setting in the web.config file that was created using my initial approach was referring to the 3.6.0.0 version of the System.Web.Extensions DLL.  This was the preview version that used to be shipped in the earlier days of the ASP.NET 3.5 Extensions Preview.

However, the newer previous that are part of http://codeplex.net/aspnet are back referencing the 3.5.0.0 version.

And this issue must have been coming specifically when running against IIS since the version of ASP.NET mapped to my IIS7 script mappings doesn’t have this version of the DLL and also because of the fact that it was looking into the <system.webserver> setting of the web.config file and trying to locate this version. (the system.webserver is interpreted only when running your site against IIS 7, when you run this using the built-in webserver, this section seems to be ignored)

The solution was simple, I renamed the 3.6 versions back to 3.5 and the site started working.  Guess the website template shipped by the Windows Live Team is referring to the old pattern of the extensions preview reference.

Would update this post, if I find a more appropriate solution/fix or corrections.

Cheers !!!

'WebForm_PostBackOptions' is undefined error, ASP.NET 2.0

Recently one of our customers had an issue when migrating from ASP.NET 1.1 to ASP.NET 2.0.  The issue came up particularly in the deployment server where any page that had validations raised the error ‘Webform_PostBackOptions’ is undefined.

If you had worked with ASP.NET 1.1 you would recollect there was an aspnet_client folder that contains all the scripts that help in client side validation as well as raising the postback events.

In ASP.NET 2.0, the “WebResource.axd” file is the handler that generates all the client side scripts which were earlier processed by the aspnet_client folder.

Coming back to this particular customer scenario, they were running URL Scan utility on their server http://learn.iis.net/page.aspx/473/using-urlscan 

URL Scan is an useful free utility that can be installed on servers that allows you to restrict the file extensions served by the IIS.  This way, potentially harmful requests can be denied and thereby injection attacks avoided.

However, the WebResource.axd handler had to be manually added as an extension to be allowed and thereafter this error went off.

There are many more causes of this particular error and I found that it has been discussed enough in forums.  Here below is one of them for your reference

http://www.eggheadcafe.com/tutorials/aspnet/03cedfc8-e623-4fee-81d8-04663b71b675/aspnet-11-to-aspnet-2.aspx

 

Cheers !!!