ID of web control changed

Discussion in 'ASP .Net Web Controls' started by Adam Plocher, Apr 18, 2006.

  1. Adam  Plocher

    Adam Plocher Guest

    Hello, I have been developing a website locally for quite some time now
    in ASP.NET 2.0/C# and all of the web controls (locally) have IDs like
    this: ctl00_ContentPlaceHolder1_ddSearchType

    I just uploaded it to our live site today, for the first time, and now
    all of my IDs look like this:
    _ctl0_ContentPlaceHolder1_ddSearchType

    What gives? My javascript is now all broken because of this. Is there
    a place where I can specify to use ctl00 instead of _ctl0 ? The only
    way I can think to fix this is by using a literal (or something) and
    have c# generate the JS on the fly using the controls UniqueID
    property, which seems really stupid. It's hundreds of lines of JS.

    Thanks
    -Adam
     
    Adam Plocher, Apr 18, 2006
    #1
    1. Advertising

  2. Adam  Plocher

    Guffa Guest

    You shouldn't rely on the IDs to be in a specific format. Use the ClientID
    property to get the ID to use in Javascript.

    /Guffa

    "Adam Plocher" wrote:

    > Hello, I have been developing a website locally for quite some time now
    > in ASP.NET 2.0/C# and all of the web controls (locally) have IDs like
    > this: ctl00_ContentPlaceHolder1_ddSearchType
    >
    > I just uploaded it to our live site today, for the first time, and now
    > all of my IDs look like this:
    > _ctl0_ContentPlaceHolder1_ddSearchType
    >
    > What gives? My javascript is now all broken because of this. Is there
    > a place where I can specify to use ctl00 instead of _ctl0 ? The only
    > way I can think to fix this is by using a literal (or something) and
    > have c# generate the JS on the fly using the controls UniqueID
    > property, which seems really stupid. It's hundreds of lines of JS.
    >
    > Thanks
    > -Adam
    >
    >
     
    Guffa, Apr 19, 2006
    #2
    1. Advertising

  3. Adam  Plocher

    Adam Plocher Guest

    So should I be loading all my JS into a Literal or something? I can't
    just write JS code into my aspx or into a standalone .js file?
     
    Adam Plocher, Apr 20, 2006
    #3
  4. Adam  Plocher

    Guffa Guest

    > So should I be loading all my JS into a Literal or something? I can't
    > just write JS code into my aspx or into a standalone .js file?


    You have several options. You can either create all of the code dynamically,
    or just part of it. You can for an example make wrappers for the fields that
    you need to access:

    function getSomeField() {
    return document.getElementById('<asp:Literal id="SomeFieldLiteral"
    runat="server"/>');
    }

    and in code-behind:

    SomeFieldLiteral.Text = SomeField.ClientId;

    You can also create the wrapper entirely frfom code-behind:

    Page.RegisterClientScriptBlock("GetSomeField", "<script
    type=\"text/javascript\">function getSomeField() { return
    document.getElementById('" + SomeField.ClientId + "'); }</script>");
     
    Guffa, Apr 20, 2006
    #4
  5. Adam  Plocher

    Adam Plocher Guest

    Thanks Guffa, I'll play around with these and see what I come up with.
     
    Adam Plocher, Apr 20, 2006
    #5
    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. John Saunders
    Replies:
    0
    Views:
    487
    John Saunders
    Aug 29, 2003
  2. Reny J Joseph Thuthikattu
    Replies:
    1
    Views:
    576
    =?Utf-8?B?UHJha2FzaC5ORVQ=?=
    Dec 30, 2004
  3. Replies:
    1
    Views:
    714
    Rosanne
    Oct 11, 2005
  4. Christian Blackburn
    Replies:
    2
    Views:
    330
    Christian Blackburn
    Jun 21, 2006
  5. mxbrunet
    Replies:
    1
    Views:
    228
Loading...

Share This Page