Name and ID attributes of the asp.net server HTMLControls

Discussion in 'ASP .Net Web Controls' started by Parag Mahajan, Jun 22, 2006.

  1. 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.
    Parag Mahajan, Jun 22, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gopal Prabhakaran

    Difference between HtmlControls and server controls

    Gopal Prabhakaran, Jan 26, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,414
    Rainer Maurer [MS]
    Jan 26, 2004
  2. Abhishek Srivastava

    WebControls and HtmlControls

    Abhishek Srivastava, May 12, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    381
    Abhishek Srivastava
    May 12, 2004
  3. Henri
    Replies:
    4
    Views:
    3,397
    Henri
    May 18, 2004
  4. =?Utf-8?B?U3RldmVuLkRhaGxpbg==?=

    htmlcontrols not reflecting changes upon return to the server

    =?Utf-8?B?U3RldmVuLkRhaGxpbg==?=, Aug 5, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    413
    Ken Cox [Microsoft MVP]
    Aug 6, 2004
  5. Replies:
    4
    Views:
    650
Loading...

Share This Page