Wednesday 26 October 2011

Checking child nodes when checking the parent node


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Test</title>

    <script language="javascript" type="text/javascript">
   function OnTreeClick(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)
            {
                if(nxtSibling.tagName.toLowerCase() == "div") //if node has children
                {
                    CheckUncheckChildren(parentTable.nextSibling, src.checked);
                }
            }
            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)
            {
                var isAllSiblingsChecked = AreAllSiblingsChecked(srcChild);
                if(isAllSiblingsChecked)
                    checkUncheckSwitch = true;
                else  
                    return;
            }
            else
            {
                checkUncheckSwitch = false;
            }
           
            var inpElemsInParentTable = parentNodeTable.getElementsByTagName("input");
            if(inpElemsInParentTable.length > 0)
            {
                var parentNodeChkBox = inpElemsInParentTable[0];
                parentNodeChkBox.checked = checkUncheckSwitch;
             
                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)
        {
            if(parentDiv.childNodes[i].tagName.toLowerCase() =="table")
            {
               var prevChkBox = parentDiv.childNodes[i].getElementsByTagName("input")[0];
             
              if(!prevChkBox.checked)
              {
                return false;
              }
            }
        }
     }
     return true;
   }
   function GetParentByTagName(parentTagName, childElementObj)
   {
      var parent = childElementObj.parentNode;
      while(parent.tagName.toLowerCase() != parentTagName.toLowerCase())
      {
         parent = parent.parentNode;
      }
    return parent;  
   }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="TreeViewDemo" runat="server" ExpandDepth="0" ShowCheckBoxes="All">
            <Nodes>
                <asp:TreeNode NavigateUrl="" Text="P" Value="P">
                    <asp:TreeNode NavigateUrl="" Text="P:C1" Value="C1"></asp:TreeNode>
                    <asp:TreeNode NavigateUrl="" Text="P:C2" Value="C2"></asp:TreeNode>
                </asp:TreeNode>
                <asp:TreeNode NavigateUrl="" Text="P1" Value="P1">
                    <asp:TreeNode NavigateUrl="" Text="P1:C1" Value="P1:C1"></asp:TreeNode>
                    <asp:TreeNode NavigateUrl="" Text="P1:C2" Value="P1:C2"></asp:TreeNode>
                </asp:TreeNode>
                <asp:TreeNode NavigateUrl="" Text="P2" Value="P2">
                    <asp:TreeNode NavigateUrl="" Text="P2:C1" Value="P2:C1"></asp:TreeNode>
                    <asp:TreeNode NavigateUrl="" Text="P2:C2" Value="P2:C2"></asp:TreeNode>
                </asp:TreeNode>
            </Nodes>
        </asp:TreeView>
    </div>
    </form>
</body>
</html>
------------------------------------------------------------------
 protected void Page_Load(object sender, EventArgs e)
    {
        TreeViewDemo.Attributes.Add("onclick", "OnTreeClick(event)");
    }

Monday 24 October 2011

Find Latitude And Longitude From C# Asp.net


using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net;
using System.Web.UI;

namespace GoogleGeocoder
{
   public interface ISpatialCoordinate
   {
      decimal Latitude {get; set; }
      decimal Longitude {get; set; }
   }
   public struct Coordinate : ISpatialCoordinate
   {
      private decimal _latitude;
      private decimal _longitude;

      public Coordinate(decimal latitude, decimal longitude)
      {
         _latitude = latitude;
         _longitude = longitude;
      }

      #region ISpatialCoordinate Members

      public decimal Latitude
      {
        get
        {
            return _latitude;
        }
        set
        {
            this._latitude = value;
        }
      }

      public decimal Longitude
      {
        get
        {
            return _longitude;
        }
        set
        {
            this._longitude = value;
        }
      }

   #endregion
   }

   public class Geocode
   {
      private const string _googleUri = "http://maps.google.com/maps/geo?q=";
      private const string _googleKey = "ABQIAAAAAbmewSRic45gmeA3SqPU2xTN7OoKn4WqNHLTV0WVkMCPFkG3kxRiF6l_Jlmlap7r6S592BJ_XDSJtg";
      private const string _outputType = "csv"; // Available options: csv, xml, kml, json

      private static Uri GetGeocodeUri(string address)
      {
         address = HttpUtility.UrlEncode(address);
         return new Uri(String.Format("{0}{1}&output={2}&key={3}", _googleUri, address, _outputType, _googleKey));
      }
      public static Coordinate GetCoordinates(string address)
      {
         WebClient client = new WebClient();
         Uri uri = GetGeocodeUri(address);
         string[] geocodeInfo = client.DownloadString(uri).Split(',');
         return new Coordinate(Convert.ToDecimal(geocodeInfo[2]), Convert.ToDecimal(geocodeInfo[3]));
      }

   }
}
------------------------------------
using GoogleGeocoder;

Coordinate coordinate = Geocode.GetCoordinates("limkon");
decimal latitude = coordinate.Latitude;
decimal longitude = coordinate.Longitude;

Convert dd/MM/yyyy to mm/dd/yyyy in c# asp.net


   string UrDate = "YourDate in dd/mm/yyyy"
    System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
    dateInfo.ShortDatePattern = "dd/MM/yyyy";
    DateTime validDate = Convert.ToDateTime(UrDate, dateInfo);

Get all non-clustered indexes

DECLARE cIX CURSOR FOR     SELECT OBJECT_NAME(SI.Object_ID), SI.Object_ID, SI.Name, SI.Index_ID         FROM Sys.Indexes SI             ...