System.Web.UI.Control.GetUniqueIDRelativeTo C# (CSharp) Method

GetUniqueIDRelativeTo() public method

public GetUniqueIDRelativeTo ( Control control ) : string
control Control
return string
		public string GetUniqueIDRelativeTo (Control control)
		{
			if (control == null)
				throw new ArgumentNullException ("control");

			Control parent = this;
			Control namingContainer = control.NamingContainer;
			
			if (namingContainer != null)
				while (parent != null && parent != namingContainer)
					parent = parent.Parent;

			if (parent != namingContainer)
				throw new InvalidOperationException (
					String.Format ("This control is not a descendant of the NamingContainer of '{0}'", control.UniqueID)
				);

			int idx = control.UniqueID.LastIndexOf (IdSeparator);
			if (idx < 0)
				return UniqueID;

			return UniqueID.Substring (idx + 1);
		}
#endif

Usage Example

Example #1
0
	protected void Page_Load (object sender, EventArgs e)
	{
		var list = new List<string> {
		    "One",
		    "Two",
		    "Three"
		};

		try {
			GetUniqueIDRelativeTo (null);
		} catch (ArgumentNullException ex) {
			log.InnerText += String.Format ("Page; Relative to: null; Result: exception {0} (expected)\n", ex.GetType ());
		} catch (Exception ex) {
			log.InnerText += String.Format ("Page; Relative to: null; Result: exception {0}\n", ex.GetType ());
		}

		var ctl = new Control ();
		try {
			ctl.GetUniqueIDRelativeTo (this);
		} catch (InvalidOperationException ex) {
			log.InnerText += String.Format ("A control; Relative to: {0}; Result: exception {1} (expected)\n", this.UniqueID, ex.GetType ());
		} catch (Exception ex) {
			log.InnerText += String.Format ("A control; Relative to: {0}; Result: exception {1}\n", this.UniqueID, ex.GetType ());
		}

		try {
			textBox1.GetUniqueIDRelativeTo (this);
		} catch (InvalidOperationException ex) {
			log.InnerText += String.Format ("TextBox; Relative to: {0}; Result: exception {1} (expected)\n", this.UniqueID, ex.GetType ());
		} catch (Exception ex) {
			log.InnerText += String.Format ("TextBox; Relative to: {0}; Result: exception {1}\n", this.UniqueID, ex.GetType ());
		}

		repeater1.DataSource = list;
		repeater1.DataBind ();
	}
All Usage Examples Of System.Web.UI.Control::GetUniqueIDRelativeTo