User Controls and javascript

I have wondered about this for a long time, but have also long since accepted the circumstances.


Why, when I place form elements on a user control, does it have to append “UserControlId_” to the id
of the element and “UserControlId:” to the name of the element? This is really annoying for custom javascript that is written for these elements and form submissions. Once you realize that
this happens, its not that difficult to define a javascript variable and append it to the beginning of all the elements that
you are using, or parse the form collection on the codebehind before you use the posted data, but it can still be frustrating. I was hoping that this behavior would be removed when master pages came out
with asp.net 2.0, but alas, after working on a side project using master pages, I see that this still occurs.

Does anyone know why this is neccesary?

9 Comments so far »

  1. Anonymous said,

    Wrote on December 21, 2004 @ 7:03 am

    Use the ClientId property of the control - this will give you the unique name you can use to refer to the control client-side. It is necessary to ensure unique names for the elements for Javascript. It’s still there in ASP.NET 2.0

  2. Anonymous said,

    Wrote on December 21, 2004 @ 7:26 am

    Why? Because you can use a user control multiple times in the same page — but the ids and names have to be guaranteed unique on the page.

  3. Anonymous said,

    Wrote on December 21, 2004 @ 7:57 am

    I had thought about duplicate names and ids, but I didn’t think that was a good enough reason, it should be up to the developer to make sure they name their elements distinctly.

    However, I had not considered using a user control multiple times on a page, in that case the developer simply cannot make distinct names, so in this case the behavior is warranted, good answer, thanks

  4. Anonymous said,

    Wrote on December 21, 2004 @ 6:45 pm

    To agree what Scott and Paul said, I *never* assume the client ID, even if I’m pretty darn sure (99.9%)… I’ll always use the ClientID property.

  5. Anonymous said,

    Wrote on December 22, 2004 @ 2:24 am

    One thing I have been doing lately is to use the DOM to traverse form elements, that way I only need a reference to the table or div that contains all of the input boxes, etc..

  6. Anonymous said,

    Wrote on July 5, 2005 @ 8:21 am

    how can i access the textbox of a user control inside control using javascript??

  7. harterc2 said,

    Wrote on May 21, 2008 @ 8:12 am

    Try.. something like this..

    protected System.Web.UI.WebControls.Button MyButton;

    protected void Page_Load(object sender, EventArgs e)
    {
    MyButton.Attributes.Add(”onclick”, “javascript:AccessTextbox(” txtYourTextBox.ClientID
    “)”);
    }

    // the onclick event will trigger that javascript function and you’ll already have your textbox’s clientID (the real ID) as a parameter passed to the function.
    // Hope that helped atleast somewhat

  8. harterc2 said,

    Wrote on May 21, 2008 @ 8:13 am

    I just realized that post is from over 2 years ago. :(

  9. ben said,

    Wrote on May 21, 2008 @ 8:19 am

    Ha! it’s actually over 3 years old :) At some point along the way I found out about Control.ClientId too, it makes life a lot easier.

Comment RSS · TrackBack URI

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Enter my name (ben) in this box, so I know you're a human.

Comment: