Click here to Skip to main content
15,883,705 members
Articles / Web Development / HTML

ASP.NET: How to Resolve Client ID

Rate me:
Please Sign up or sign in to vote.
3.60/5 (15 votes)
1 Feb 2007CPOL4 min read 246K   524   21  
A method to resolve the client IDs of controls in containers.
/// <summary>
/// FileName: mycontrol.ascx.cs
/// 
/// Purpose: controls container
/// 
/// Author: 0garcia (og-bit.com) 
/// 
/// Update/Fix History:
/// 0g01302007 Initial Implementation
/// </summary>

namespace ResolveClientId
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		Summary description for ValidateControl.
	/// </summary>
	public class myControl : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.TextBox mytext;
		protected System.Web.UI.WebControls.Literal   ctrlPrefix;

		private void Page_Load(object sender, System.EventArgs e)
		{			
			string name = this.ClientID;
			string []elm =ctrlPrefix.ClientID.Split('_');		//get an array of values 
			ctrlPrefix.Text = ctrlPrefix.ClientID.Replace(elm[elm.Length - 1],"");    //get the last element and replace from the id     
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect OG-BITechnologies
United States United States
Software engineer, author & speaker who enjoys mentoring, learning, speaking and sharing with others about software development technologies. Microsoft MVP.

My Blog

Comments and Discussions