what is name attribute used for in this textbox web control

T

Tony Johansson

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" />

//Tony
 
A

Arne Vajhøj

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" />

As I read the docs, then name is not an ASP.NET control
attribute but an HTML INPUT attribute that ASP.NET just
passes on.

So I think you should only use id and let ASP.NET generate
name based on that.

Arne
 
R

Registered User

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top