Hello!
When this text box web control
<asp:TextBox ID="txt" runat="server" />
is sent to the browser a name attribute is added. As you can see the ID
attribute has the same value as the name attribute.
So my question is what is the point in that having two attribute with the
same value ?
<input type="text" ID="txt" name="txt" />
In this example there is none but there are situations where the
generated values are different. An example is when a web page contains
multiple instances of a web user control. If the web user control
contains a textbox
<asp:TextBox ID="txt" runat="server" />
the generated html will be similar to
<div>
<input name="SimpleControl0$txt" type="text" id="SimpleControl0_txt"
/>
</div>
<div>
<input name="SimpleControl1$txt" type="text" id="SimpleControl1_txt"
/>
</div>
This behavior is especially useful when a user web control uses
Javascript methods to act upon itself and there are multiple instances
of the control on a web page. The script methods can take the ID
property as an argument such as
function foo(id)
{
alert(id);
}
and in the control's markup
<div onclick='foo("<%= txt.ClientID %>");'>
<asp:TextBox runat="server" id="txt" ></asp:TextBox>
</div>
When a control's div is clicked on, the method will display the same
ID as its textbox has in the html source. This is because the
design-time id property is superceded by the ClientID property when
the page is rendered. The two property values are not always the same.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx
regards
A.G.