Click here to Skip to main content
15,892,517 members
Articles / Web Development / ASP.NET

ASP.NET User Controls - Notify One Control Of Changes In Other Control

Rate me:
Please Sign up or sign in to vote.
4.39/5 (23 votes)
12 Mar 20026 min read 195.5K   2.6K   87  
A tutorial on how to use delegate model to notify one ASP.NET user control of changes in the other user control.
// Disclaimer and Copyright Information
// ADSIUtil.aspx.cs : 
//
// All rights reserved.
//
// Written by Pardesi Services, LLC
// Version 1.0
//
// Distribute freely, except: don't remove our name from the source or
// documentation (don't take credit for my work), mark your changes (don't
// get me blamed for your possible bugs), don't alter or remove this
// notice.
// No warrantee of any kind, express or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
//
// Send bug reports, bug fixes, enhancements, requests, flames, etc. to
// softomatix@pardesiservices.com
///////////////////////////////////////////////////////////////////////////////
//

using System;
using System.Diagnostics;
using System.DirectoryServices;
using System.Xml;
using System.Collections;


namespace ASPNet_App
{
	/// <summary>
	/// 
	/// </summary>
	public class ADSIUtil
	{
		protected string m_strErrors = "";
		protected string m_strRoot = "";
		protected DirectoryEntry m_obDirEntry;
		public ADSIUtil()
		{
			// 
			// TODO: Add constructor logic here
			//
		}

		/// <summary>
		/// 
		/// </summary>
		public string Errors
		{
			get
			{
				return m_strErrors;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string RootNode
		{
			get
			{
				return m_strRoot;
			}
			set
			{
				m_strRoot = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="strRoot"></param>
		/// <returns></returns>
		public bool Initialize(string strRoot)
		{
			if (m_strRoot == null || m_strRoot.Length == 0)
			{
				RootNode = strRoot;
			}

			try
			{
				m_obDirEntry = new DirectoryEntry(strRoot);
/*
				foreach (string propName in m_obDirEntry.Properties.PropertyNames) 
				{
					foreach (object value in m_obDirEntry.Properties[propName] ) 
					{
						Trace.WriteLine("name=" + propName + "  value=" + value );
					}
				}
*/
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.Message);
				return false;
			}
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="strLogin"></param>
		/// <returns></returns>
		public XmlDocument GetUserSchema(string strLogin)
		{
			XmlDocument userDataDoc = null;
			// Make sure that we have a root node specified.
			if (m_strRoot == null || m_strRoot.Length == 0)
			{
				m_strErrors += "Root Node not initializes";
				return userDataDoc;
			}

			SearchResultCollection results;
			DirectorySearcher srch = new DirectorySearcher(m_obDirEntry);
			srch.Filter = ("(cn=" + strLogin + ")");
			
			try
			{
				results = srch.FindAll();
			}
			catch (NotSupportedException ex)
			{
				m_strErrors += ex.Message;
				Trace.WriteLine(ex.Message);
				return userDataDoc;
			}
			catch (Exception ex)
			{
				m_strErrors += "\n";
				m_strErrors += ex.Message;
				Trace.WriteLine(ex.Message);
				return null;
			}

			try
			{
				foreach(SearchResult result in results) 
				{
					ResultPropertyCollection coll = result.Properties;
					ADSIUser user = new ADSIUser();
					if (false == user.Initialize(coll))
					{
						m_strErrors += "\n";
						m_strErrors += "Failed to initialize the ADSI object";
						Trace.WriteLine("Failed to initialize the ADSI object");
						return null;
					}

					if (user.NumProperties > 0)
					{
						userDataDoc = user.GetProperties();
					}
				}
			}
			catch (Exception ex)
			{
				m_strErrors += "\n";
				m_strErrors += ex.Message;
				Trace.WriteLine(ex.Message);
				return null;
			}

			return userDataDoc;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public ArrayList GetUsersList()
		{
			ArrayList usersArr = new ArrayList();

			// Make sure that we have a root node specified.
			if (m_strRoot == null || m_strRoot.Length == 0)
			{
				m_strErrors += "Root Node not initializes";
				return usersArr;
			}

			SearchResultCollection results;
			DirectorySearcher srch = new DirectorySearcher(m_obDirEntry);
			srch.Filter = "(objectClass=User)";
			
			try
			{
				results = srch.FindAll();
			}
			catch (NotSupportedException ex)
			{
				m_strErrors += ex.Message;
				Trace.WriteLine(ex.Message);
				srch.Dispose();
				return usersArr;
			}
			catch (Exception ex)
			{
				m_strErrors += "\n";
				m_strErrors += ex.Message;
				Trace.WriteLine(ex.Message);
				srch.Dispose();
				return null;
			}
			try
			{
				foreach(SearchResult result in results) 
				{
					ResultPropertyCollection propColl = result.Properties;
					ADSIUser user = new ADSIUser();
					if (false == user.Initialize(propColl))
					{
						m_strErrors += "\n";
						m_strErrors += "Failed to initialize the ADSI object";
						Trace.WriteLine("Failed to initialize the ADSI object");
						srch.Dispose();
						return null;
					}

					usersArr.Add(user);
				}
			}
			catch (Exception ex)
			{
				m_strErrors += "\n";
				m_strErrors += ex.Message;
				Trace.WriteLine(ex.Message);
				srch.Dispose();
				return null;
			}

			srch.Dispose();

			return usersArr;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public ArrayList GetGroupsList()
		{
			ArrayList gpsList = new ArrayList();

			// Make sure that we have a root node specified.
			if (m_strRoot == null || m_strRoot.Length == 0)
			{
				m_strErrors += "Root Node not initializes";
				return gpsList;
			}

			SearchResultCollection results;
			DirectorySearcher srch = new DirectorySearcher(m_obDirEntry);
			srch.Filter = "(objectClass=Group)";
			
			try
			{
				results = srch.FindAll();
			}
			catch (NotSupportedException ex)
			{
				m_strErrors += ex.Message;
				Trace.WriteLine(ex.Message);
				srch.Dispose();
				return gpsList;
			}
			catch (Exception ex)
			{
				m_strErrors += "\n";
				m_strErrors += ex.Message;
				Trace.WriteLine(ex.Message);
				srch.Dispose();
				return null;
			}
			try
			{
				foreach(SearchResult result in results) 
				{
					ResultPropertyCollection propColl = result.Properties;
					ADSIGroup gp = new ADSIGroup();
					if (false == gp.Initialize(propColl))
					{
						m_strErrors += "\n";
						m_strErrors += "Failed to initialize the ADSI object";
						Trace.WriteLine("Failed to initialize the ADSI object");
						srch.Dispose();
						return null;
					}

					gpsList.Add(gp);
				}
			}
			catch (Exception ex)
			{
				m_strErrors += "\n";
				m_strErrors += ex.Message;
				Trace.WriteLine(ex.Message);
				srch.Dispose();
				return null;
			}

			srch.Dispose();
			return gpsList;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="strUser"></param>
		/// <returns></returns>
		private bool DumpSchema(string strUser)
		{
			if (m_strRoot.Length == 0)
			{
				return false;
			}

			SearchResultCollection results;
			DirectorySearcher srch = null;
			try
			{
				//DirectoryEntry schemaEntry = m_obDirEntry.SchemaEntry;
				//Trace.WriteLine(schemaEntry.Children.ToString());
				srch = new DirectorySearcher(m_obDirEntry);
				srch.Filter = ("(cn=" + strUser + ")");
				results = srch.FindAll();

				foreach (SearchResult result in results)
				{
					ResultPropertyCollection coll = result.Properties;
					int nItems = coll.Count;
					foreach( string myKey in coll.PropertyNames)
					{
						string tab = "    ";
						Trace.WriteLine(myKey + " = ");
						foreach( object myCollection in coll[myKey])
						{
							Trace.WriteLine(tab + myCollection);
						}
					}
				}
			
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.Message);
			}

			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="strName"></param>
		/// <returns></returns>
		public ADSIUser FindUserByName(string strName)
		{
			ADSIUser obUser = null;

			return obUser;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
To learn more about us, Please visit us at http://www.netomatix.com

Comments and Discussions