Name and ID attributes of the asp.net server HTMLControls

P

Parag Mahajan

Hi,

I am having trouble in assigning "id" as well as the "name" attribute for
the asp.net 2.0 webcontrol I am developing.

Consider my control below:
_________________________________
public class MyWebControl : WebControl
{
....
Controls.Clear();
divCtrl = new HtmlGenericControl("div");
HtmlInputText t = new HtmlInputText();
t.Attributes["name"] = "textboxnameAttrName";
t.Attributes["id"] = "textboxnameAttrID";
divCtrl.Controls.Add(t);
Controls.Add(divCtrl);
....
}
_________________________________

For this, I get the following output HTML.

<span id="MyWebControl1"><div><input name="ctl03" type="text"
id="textboxnameAttrID" /></div></span>


My question over here is, why does the name attribute of the input html
element is "ctl03" and not "textboxnameAttrName" (the one which I set it
programmatically). I even tried with the below statement

t.Name = "textboxname";

I still get the same output as above..


_______________________

I have one more question here, is that when I set the property "ID" of the
HtmlInputText control, it also sets the "name" attribute of the rendered
HTML text box to be the same as "id" attribute. (This is what MSDN also
says.) But I did not understand why is this such a behaviour.

When I have the following lines in the control,
t.ID = "textboxnameAttrID";
t.Attributes["name"] = "textboxnameAttrName";

the output HTML is :
<span id="MyWebControl1"><div><input name="IDText" type="text" id="IDText"
type="text" /></div></span>


Can anyone please let me know, why is this such a behaviour?
For the ASP.Net control, can I not set "id" as well as the "name" attribute
as per my requirement.?

By this, I mean I want the HTML output to be like this,
<span id="MyWebControl1"><div><input name="textboxnameAttrName" type="text"
id="textboxnameAttrID" /></div></span>

Thanks and Regards,
Parag.
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top