Friday, March 9, 2007

What is the difference between Implicit and Explicit interfaces?

Hi if you want to find out the answer to that “guru” question and also to have really good understanding of how C# is working under the hood, understanding IL, Garbage Collector, Type Reflection, Late Binding, Attribute-Based Programming, Object Serialization etc, I am recommending to read Andrew Troelsen’s book Pro C# 2005 and the .NET 2.0 Platform”.




PART 1 ■ ■ ■ Introducing C# and the

.NET Platform

CHAPTER 1 The Philosophy of .NET
CHAPTER 2 Building C# Applications

PART 2 ■ ■ ■ The C# Programming Language
CHAPTER 3 C# Language Fundamentals
CHAPTER 4 Object-Oriented Programming with C#
CHAPTER 5 Understanding Object Lifetime
CHAPTER 6 Understanding Structured Exception Handling
CHAPTER 7 Interfaces and Collections
CHAPTER 8 Callback Interfaces, Delegates, and Events
CHAPTER 9 Advanced C# Type Construction Techniques
CHAPTER 10 Understanding Generics


PART 3 ■ ■ ■ Programming with .NET Assemblies


CHAPTER 11 Introducing .NET Assemblies
CHAPTER 12 Type Reflection, Late Binding, and Attribute-Based Programming
CHAPTER 13 Processes, AppDomains, Contexts, and CLR Hosts
CHAPTER 14 Building Multithreaded Applications
CHAPTER 15 Understanding CIL and the Role of Dynamic Assemblies


PART 4 ■ ■ ■ Programming with the .NET Libraries


CHAPTER 16 The System.IO Namespace
CHAPTER 17 Understanding Object Serialization
CHAPTER 18 The .NET Remoting Layer
CHAPTER 19 Building a Better Window with System.Windows.Forms
CHAPTER 20 Rendering Graphical Data with GDI+
CHAPTER 21 Programming with Windows Forms Controls
CHAPTER 22 Database Access with ADO.NET


PART 5 ■ ■ ■Web Applications and XML

Web Services

CHAPTER 23 ASP.NET 2.0 Web Pages and Web Controls
CHAPTER 24 ASP.NET 2.0 Web Applications
CHAPTER 25 Understanding XML Web Services


Control’s identifiers in ASP.NET 2.0: ID, ClientID, UniqueID, AssosiatedControlID

“What is the difference between ID, ClientID, UniqueID and AssosiatedControlID identifiers of the control?” -

I’ve heard this question a lot of times from .Net developers . That is why I decided to start my blogs with the simple but common subject, about the difference between identifiers of web control in ASP.NET.

Web controls contain several identifiers : ID, UniqueID, ClientID and AssosiatedControlID. What are they?


“Theory”

ID is a unique identifier of element which helps us to access programmatically to the controls properties, methods and events. Each control on the page should contain unique value of the ID property. In Visual Studio value of ID property will be set automatically after dragging and dropping element from toolbox to the page. Property is not readonly and can be changed by programmer to have more explainable name. In case of missing ID compiler will not generate an error and control will be generated on a page but access to control will be lost.

ClientID is an identifier of the rendered element on html level. It helps us to access the element by script languages or css. ClientID is a read only property and generates automatically. Common usage of this property is when we are programmatically creating script code from code behind level and need to specify the ID of element in the script’s code.

For instance:

protected TextBox txtMyTextBox;


protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("<script>");
sb.Append("function MyFunction()");
sb.Append("{");
sb.AppendFormat("document.getElementById('{0}').value = 'Test'; ", this.txtMyTextBox.ClientID);
sb.Append("}");
sb.Append("</script>");


Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CustomKey", sb.ToString());
}


UniqueID is an identifier which generated automatically by ASP.NET to identify control within a page as there can be several controls with the same ID included in different containers. UniqueID generates based on ID of container and ID of control. ASP.NET accesses to Page’s element through UniqueID of the element.


“How does it work”

I think it is time to code a little bit. Let’s create a new web project and on default page add Label control with ID=”MyLabel”.

<asp:Label ID="MyLabel" runat="server" Text="My Label on a page "/>

Then in Controls folder let us to add new User Control called MyUserControl.ascx and add Label control inside of the user control and set ID of the Label control to “MyLabel”.

<%@ Control Language="C#" AutoEventWireup="true" ... %>

<asp:Label ID="MyLabel" runat="server" Text="My Label in user control"/>


Let add this user control into the page where previous Label controls is located. Now we have two elements on a page “MyLabel” and “MyUserControl “ (which contains another “MyLabel “ control).

<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="MyLabel" runat="server" Text="My Label on a page"></asp:Label>
<br />
<br />
<uc1:MyUserControl ID="MyUserControl1" runat="server" />
</div>
</form>
</body>



Let’s to set a breakpoint in Page_Load level of page initialization and investigate identifiers of the Label controls which we have on a page: one is located directly on a page and the second one inside of UserControl. As you can see ID property of the both controls are the same like we set.





There is no issue with this as server identifies control by UniqueID and not ID. You can see it in next image. UniqueID of the Label which was set directly on the page has the same value as ID but Label control located in MyUserControl has UniqueID which ASP.NET generates by concatenation of container’s ID and control’s ID divided by “$” sign (in ASP.NET 1.1 it is “:”).



The same story with a ClientID. In case whether control located in some kind of container ASP.NET generates ClientID based on container ID and control ID divided by “_” sign.



Here is the result of rendered html code :








“What about AssosiatedControlID”

In ASP.NET 2.0 Label web control contains a new property, AssociatedControlID, which points to another input control such as a TextBox control. When the property is set, generated tag of the Label control will be not <span> but <label> For attribute of <label> element assumes the clientId of the associated control as the value.

ASP.NET code:



<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server" AssociatedControlID="TextBox1" Text="Label Text" />


Generated Html code:

<input name="TextBox1" type="text" id="TextBox1" />
<label for="TextBox1" id="Label1">Label Text</label>


The result of rendering is that after clicking on label assosiated textbox control will get focus.




“Summary”

It is very important to understand the difference between identifiers as you will use them almost everywhere during web development.