Framework-generated ID woes

Discussion in 'ASP .Net' started by mark.norgate@gmail.com, Jun 14, 2006.

  1. Guest

    Hi

    I have lots of controls on my ASP.NET 2.0 page, some implementing
    INamingContainer, others just <asp:button>s and so on.

    The eternal lament: I need to interact with these controls on the
    client-side, but of course I can't guarantee that the IDs generated by
    the Framework will be consistent (like
    "ctl00_ContentPlaceHolder_LoadPreviousButton").

    Is there a way to tell the Framework what prefix you'd like to use for
    your controls' IDs? Or do I have to resort to injecting some JavaScript
    containing the ID into the page (which is rather ugly I think). If so,
    how do I actually get the fully-qualified name of the control?

    Thanks, Mark
    , Jun 14, 2006
    #1
    1. Advertising

  2. component.ClientID

    :)

    like:

    document.getElementById( '<%= myLabel.ClientID %>' ).value = 'test';

    --

    Bruno Alexandre
    (a Portuguese in Københanv, Danmark)


    <> escreveu na mensagem
    news:...
    > Hi
    >
    > I have lots of controls on my ASP.NET 2.0 page, some implementing
    > INamingContainer, others just <asp:button>s and so on.
    >
    > The eternal lament: I need to interact with these controls on the
    > client-side, but of course I can't guarantee that the IDs generated by
    > the Framework will be consistent (like
    > "ctl00_ContentPlaceHolder_LoadPreviousButton").
    >
    > Is there a way to tell the Framework what prefix you'd like to use for
    > your controls' IDs? Or do I have to resort to injecting some JavaScript
    > containing the ID into the page (which is rather ugly I think). If so,
    > how do I actually get the fully-qualified name of the control?
    >
    > Thanks, Mark
    >
    Bruno Alexandre, Jun 14, 2006
    #2
    1. Advertising

  3. Guest

    Darn it. No way to specify the ID prefix? Seems dangerous to modify
    this ClientID property (if that's even possible, haven't looked...)

    Mark

    Bruno Alexandre wrote:
    > component.ClientID
    >
    > :)
    >
    > like:
    >
    > document.getElementById( '<%= myLabel.ClientID %>' ).value = 'test';
    >
    > --
    >
    > Bruno Alexandre
    > (a Portuguese in Københanv, Danmark)
    >
    >
    > <> escreveu na mensagem
    > news:...
    > > Hi
    > >
    > > I have lots of controls on my ASP.NET 2.0 page, some implementing
    > > INamingContainer, others just <asp:button>s and so on.
    > >
    > > The eternal lament: I need to interact with these controls on the
    > > client-side, but of course I can't guarantee that the IDs generated by
    > > the Framework will be consistent (like
    > > "ctl00_ContentPlaceHolder_LoadPreviousButton").
    > >
    > > Is there a way to tell the Framework what prefix you'd like to use for
    > > your controls' IDs? Or do I have to resort to injecting some JavaScript
    > > containing the ID into the page (which is rather ugly I think). If so,
    > > how do I actually get the fully-qualified name of the control?
    > >
    > > Thanks, Mark
    > >
    , Jun 19, 2006
    #3
  4. with

    document.getElementById( '<%= myLabel.ClientID %>' ).value = 'test';

    you are not modifing anything!!! just READING the ClientID for the specified
    tag.
    in the page the code above will render as:

    document.getElementById( 'ctl00_mainCHolder_myLabel' ).value = 'test';

    if you have the Label in a container called mainCHolder, imagine if you have
    the label inside a container and a wizard component...

    all you need to worry is to mension myLabel.ClientID

    ClientID as u can see is ReadOnly.... you can't alter nothing, we just need
    to read it!

    I really don't know where do you get the idea for change it!

    --

    Bruno Alexandre
    (a Portuguese in Københanv, Danmark)


    <> escreveu na mensagem
    news:...
    Darn it. No way to specify the ID prefix? Seems dangerous to modify
    this ClientID property (if that's even possible, haven't looked...)

    Mark

    Bruno Alexandre wrote:
    > component.ClientID
    >
    > :)
    >
    > like:
    >
    > document.getElementById( '<%= myLabel.ClientID %>' ).value = 'test';
    >
    > --
    >
    > Bruno Alexandre
    > (a Portuguese in Københanv, Danmark)
    >
    >
    > <> escreveu na mensagem
    > news:...
    > > Hi
    > >
    > > I have lots of controls on my ASP.NET 2.0 page, some implementing
    > > INamingContainer, others just <asp:button>s and so on.
    > >
    > > The eternal lament: I need to interact with these controls on the
    > > client-side, but of course I can't guarantee that the IDs generated by
    > > the Framework will be consistent (like
    > > "ctl00_ContentPlaceHolder_LoadPreviousButton").
    > >
    > > Is there a way to tell the Framework what prefix you'd like to use for
    > > your controls' IDs? Or do I have to resort to injecting some JavaScript
    > > containing the ID into the page (which is rather ugly I think). If so,
    > > how do I actually get the fully-qualified name of the control?
    > >
    > > Thanks, Mark
    > >
    Bruno Alexandre, Jun 19, 2006
    #4
  5. Guest

    Yeah, perhaps what I wrote could be misinterpreted!

    I just meant changing the prefix, that's all, not modifying the
    ClientID directly...

    Mark

    Bruno Alexandre wrote:
    > with
    >
    > document.getElementById( '<%= myLabel.ClientID %>' ).value = 'test';
    >
    > you are not modifing anything!!! just READING the ClientID for the specified
    > tag.
    > in the page the code above will render as:
    >
    > document.getElementById( 'ctl00_mainCHolder_myLabel' ).value = 'test';
    >
    > if you have the Label in a container called mainCHolder, imagine if you have
    > the label inside a container and a wizard component...
    >
    > all you need to worry is to mension myLabel.ClientID
    >
    > ClientID as u can see is ReadOnly.... you can't alter nothing, we just need
    > to read it!
    >
    > I really don't know where do you get the idea for change it!
    >
    > --
    >
    > Bruno Alexandre
    > (a Portuguese in Københanv, Danmark)
    >
    >
    > <> escreveu na mensagem
    > news:...
    > Darn it. No way to specify the ID prefix? Seems dangerous to modify
    > this ClientID property (if that's even possible, haven't looked...)
    >
    > Mark
    >
    > Bruno Alexandre wrote:
    > > component.ClientID
    > >
    > > :)
    > >
    > > like:
    > >
    > > document.getElementById( '<%= myLabel.ClientID %>' ).value = 'test';
    > >
    > > --
    > >
    > > Bruno Alexandre
    > > (a Portuguese in Københanv, Danmark)
    > >
    > >
    > > <> escreveu na mensagem
    > > news:...
    > > > Hi
    > > >
    > > > I have lots of controls on my ASP.NET 2.0 page, some implementing
    > > > INamingContainer, others just <asp:button>s and so on.
    > > >
    > > > The eternal lament: I need to interact with these controls on the
    > > > client-side, but of course I can't guarantee that the IDs generated by
    > > > the Framework will be consistent (like
    > > > "ctl00_ContentPlaceHolder_LoadPreviousButton").
    > > >
    > > > Is there a way to tell the Framework what prefix you'd like to use for
    > > > your controls' IDs? Or do I have to resort to injecting some JavaScript
    > > > containing the ID into the page (which is rather ugly I think). If so,
    > > > how do I actually get the fully-qualified name of the control?
    > > >
    > > > Thanks, Mark
    > > >
    , Jun 21, 2006
    #5
  6. Mark Rae Guest

    <> wrote in message
    news:...

    I just meant changing the prefix, that's all, not modifying the
    ClientID directly...

    Luckily, this is easy enough...

    1) In your MasterPage's code class:

    protected void Page_Init(object sender, EventArgs e)
    {
    this.ID = "MyMasterPage";
    }

    2) In your MasterPage's markup:

    <asp:contentplaceholder id="MyPlaceHolder" runat="server" />

    3) In your content page's markup:

    <asp:TextBox ID=MyTextBox runat=server />

    4) Then, from your content page, you can refer to the textbox thus:

    <script>
    document.aspnetForm.MyMasterPage_MyPlaceHolder_MyTextBox.value = 'Hello';
    </script>

    N.B. no matter what you call your form in your MasterPage, it always seems
    to be renamed as "aspnetForm". This doesn't cause me any particular problems
    because I (almost) never have more than one form on a page, so I don't
    really care what it's called...
    Mark Rae, Jun 21, 2006
    #6
  7. Guest

    Thanks a lot! Off to lunch now but will have a look at that when I get
    back.

    Cheers, Mark

    Mark Rae wrote:
    > <> wrote in message
    > news:...
    >
    > I just meant changing the prefix, that's all, not modifying the
    > ClientID directly...
    >
    > Luckily, this is easy enough...
    >
    > 1) In your MasterPage's code class:
    >
    > protected void Page_Init(object sender, EventArgs e)
    > {
    > this.ID = "MyMasterPage";
    > }
    >
    > 2) In your MasterPage's markup:
    >
    > <asp:contentplaceholder id="MyPlaceHolder" runat="server" />
    >
    > 3) In your content page's markup:
    >
    > <asp:TextBox ID=MyTextBox runat=server />
    >
    > 4) Then, from your content page, you can refer to the textbox thus:
    >
    > <script>
    > document.aspnetForm.MyMasterPage_MyPlaceHolder_MyTextBox.value = 'Hello';
    > </script>
    >
    > N.B. no matter what you call your form in your MasterPage, it always seems
    > to be renamed as "aspnetForm". This doesn't cause me any particular problems
    > because I (almost) never have more than one form on a page, so I don't
    > really care what it's called...
    , Jun 21, 2006
    #7
  8. Guest

    That works perfectly, thanks!

    wrote:
    > Thanks a lot! Off to lunch now but will have a look at that when I get
    > back.
    >
    > Cheers, Mark
    >
    > Mark Rae wrote:
    > > <> wrote in message
    > > news:...
    > >
    > > I just meant changing the prefix, that's all, not modifying the
    > > ClientID directly...
    > >
    > > Luckily, this is easy enough...
    > >
    > > 1) In your MasterPage's code class:
    > >
    > > protected void Page_Init(object sender, EventArgs e)
    > > {
    > > this.ID = "MyMasterPage";
    > > }
    > >
    > > 2) In your MasterPage's markup:
    > >
    > > <asp:contentplaceholder id="MyPlaceHolder" runat="server" />
    > >
    > > 3) In your content page's markup:
    > >
    > > <asp:TextBox ID=MyTextBox runat=server />
    > >
    > > 4) Then, from your content page, you can refer to the textbox thus:
    > >
    > > <script>
    > > document.aspnetForm.MyMasterPage_MyPlaceHolder_MyTextBox.value = 'Hello';
    > > </script>
    > >
    > > N.B. no matter what you call your form in your MasterPage, it always seems
    > > to be renamed as "aspnetForm". This doesn't cause me any particular problems
    > > because I (almost) never have more than one form on a page, so I don't
    > > really care what it's called...
    , Jun 21, 2006
    #8
  9. Guest

    That works perfectly, thanks!

    wrote:
    > Thanks a lot! Off to lunch now but will have a look at that when I get
    > back.
    >
    > Cheers, Mark
    >
    > Mark Rae wrote:
    > > <> wrote in message
    > > news:...
    > >
    > > I just meant changing the prefix, that's all, not modifying the
    > > ClientID directly...
    > >
    > > Luckily, this is easy enough...
    > >
    > > 1) In your MasterPage's code class:
    > >
    > > protected void Page_Init(object sender, EventArgs e)
    > > {
    > > this.ID = "MyMasterPage";
    > > }
    > >
    > > 2) In your MasterPage's markup:
    > >
    > > <asp:contentplaceholder id="MyPlaceHolder" runat="server" />
    > >
    > > 3) In your content page's markup:
    > >
    > > <asp:TextBox ID=MyTextBox runat=server />
    > >
    > > 4) Then, from your content page, you can refer to the textbox thus:
    > >
    > > <script>
    > > document.aspnetForm.MyMasterPage_MyPlaceHolder_MyTextBox.value = 'Hello';
    > > </script>
    > >
    > > N.B. no matter what you call your form in your MasterPage, it always seems
    > > to be renamed as "aspnetForm". This doesn't cause me any particular problems
    > > because I (almost) never have more than one form on a page, so I don't
    > > really care what it's called...
    , Jun 21, 2006
    #9
  10. still, I just think that I'm right in what I said:

    using Mark Example you can write the javascript code like he suggest:

    document.aspnetForm.MyMasterPage_MyPlaceHolder_MyTextBox.value = 'Hello';


    or using what I told you from the begining without even care about names,
    like this:

    document.getElementById(«<%= MyTextBox.ClientID %>').value = 'Hello';


    try it!

    --

    Bruno Alexandre
    (a Portuguese in Københanv, Danmark)


    "Mark Rae" <> escreveu na mensagem
    news:...
    > <> wrote in message
    > news:...
    >
    > I just meant changing the prefix, that's all, not modifying the
    > ClientID directly...
    >
    > Luckily, this is easy enough...
    >
    > 1) In your MasterPage's code class:
    >
    > protected void Page_Init(object sender, EventArgs e)
    > {
    > this.ID = "MyMasterPage";
    > }
    >
    > 2) In your MasterPage's markup:
    >
    > <asp:contentplaceholder id="MyPlaceHolder" runat="server" />
    >
    > 3) In your content page's markup:
    >
    > <asp:TextBox ID=MyTextBox runat=server />
    >
    > 4) Then, from your content page, you can refer to the textbox thus:
    >
    > <script>
    > document.aspnetForm.MyMasterPage_MyPlaceHolder_MyTextBox.value = 'Hello';
    > </script>
    >
    > N.B. no matter what you call your form in your MasterPage, it always seems
    > to be renamed as "aspnetForm". This doesn't cause me any particular
    > problems because I (almost) never have more than one form on a page, so I
    > don't really care what it's called...
    >
    Bruno Alexandre, Jun 21, 2006
    #10
  11. Mark Rae Guest

    <> wrote in message
    news:...

    > That works perfectly, thanks!


    Welcome.

    AAMOI, does anyone know the reasoning behind the renaming of the form on the
    MasterPage to 'aspnetForm', no matter what you actually call it yourself...?

    Seems to me that it wouldn't make the slightest difference, unless there's
    something going on under the hood which has been hard-coded to look for that
    name...
    Mark Rae, Jun 21, 2006
    #11
  12. Guest

    Bruno,

    I'd rather not follow your approach to be honest, since the JavaScript
    is in a separate JS file for one thing and I don't want to have to
    inject the ClientID of *every* control I intend to reference on the
    client side.

    Mark

    Bruno Alexandre wrote:
    > still, I just think that I'm right in what I said:
    >
    > using Mark Example you can write the javascript code like he suggest:
    >
    > document.aspnetForm.MyMasterPage_MyPlaceHolder_MyTextBox.value = 'Hello';
    >
    >
    > or using what I told you from the begining without even care about names,
    > like this:
    >
    > document.getElementById(«<%= MyTextBox.ClientID %>').value = 'Hello';
    >
    >
    > try it!
    >
    > --
    >
    > Bruno Alexandre
    > (a Portuguese in Københanv, Danmark)
    >
    >
    > "Mark Rae" <> escreveu na mensagem
    > news:...
    > > <> wrote in message
    > > news:...
    > >
    > > I just meant changing the prefix, that's all, not modifying the
    > > ClientID directly...
    > >
    > > Luckily, this is easy enough...
    > >
    > > 1) In your MasterPage's code class:
    > >
    > > protected void Page_Init(object sender, EventArgs e)
    > > {
    > > this.ID = "MyMasterPage";
    > > }
    > >
    > > 2) In your MasterPage's markup:
    > >
    > > <asp:contentplaceholder id="MyPlaceHolder" runat="server" />
    > >
    > > 3) In your content page's markup:
    > >
    > > <asp:TextBox ID=MyTextBox runat=server />
    > >
    > > 4) Then, from your content page, you can refer to the textbox thus:
    > >
    > > <script>
    > > document.aspnetForm.MyMasterPage_MyPlaceHolder_MyTextBox.value = 'Hello';
    > > </script>
    > >
    > > N.B. no matter what you call your form in your MasterPage, it always seems
    > > to be renamed as "aspnetForm". This doesn't cause me any particular
    > > problems because I (almost) never have more than one form on a page, soI
    > > don't really care what it's called...
    > >
    , Jun 21, 2006
    #12
  13. for seperate files use the 2nd approch ...

    that's what I do, but you never told that the javascript was in a 2nd file
    ;-)

    --

    Bruno Alexandre
    (a Portuguese in Københanv, Danmark)


    <> escreveu na mensagem
    news:...
    Bruno,

    I'd rather not follow your approach to be honest, since the JavaScript
    is in a separate JS file for one thing and I don't want to have to
    inject the ClientID of *every* control I intend to reference on the
    client side.

    Mark

    Bruno Alexandre wrote:
    > still, I just think that I'm right in what I said:
    >
    > using Mark Example you can write the javascript code like he suggest:
    >
    > document.aspnetForm.MyMasterPage_MyPlaceHolder_MyTextBox.value = 'Hello';
    >
    >
    > or using what I told you from the begining without even care about names,
    > like this:
    >
    > document.getElementById(«<%= MyTextBox.ClientID %>').value = 'Hello';
    >
    >
    > try it!
    >
    > --
    >
    > Bruno Alexandre
    > (a Portuguese in Københanv, Danmark)
    >
    >
    > "Mark Rae" <> escreveu na mensagem
    > news:...
    > > <> wrote in message
    > > news:...
    > >
    > > I just meant changing the prefix, that's all, not modifying the
    > > ClientID directly...
    > >
    > > Luckily, this is easy enough...
    > >
    > > 1) In your MasterPage's code class:
    > >
    > > protected void Page_Init(object sender, EventArgs e)
    > > {
    > > this.ID = "MyMasterPage";
    > > }
    > >
    > > 2) In your MasterPage's markup:
    > >
    > > <asp:contentplaceholder id="MyPlaceHolder" runat="server" />
    > >
    > > 3) In your content page's markup:
    > >
    > > <asp:TextBox ID=MyTextBox runat=server />
    > >
    > > 4) Then, from your content page, you can refer to the textbox thus:
    > >
    > > <script>
    > > document.aspnetForm.MyMasterPage_MyPlaceHolder_MyTextBox.value =
    > > 'Hello';
    > > </script>
    > >
    > > N.B. no matter what you call your form in your MasterPage, it always
    > > seems
    > > to be renamed as "aspnetForm". This doesn't cause me any particular
    > > problems because I (almost) never have more than one form on a page, so
    > > I
    > > don't really care what it's called...
    > >
    Bruno Alexandre, Jun 21, 2006
    #13
    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. Anatoly Volodko
    Replies:
    1
    Views:
    2,073
    Mattias Sjögren
    Aug 14, 2003
  2. Charles A. Lackman
    Replies:
    1
    Views:
    1,320
    smith
    Dec 8, 2004
  3. Mark
    Replies:
    4
    Views:
    1,706
    Juan T. Llibre
    Nov 17, 2005
  4. moi
    Replies:
    3
    Views:
    4,034
    quaiser_ali
    Sep 26, 2008
  5. Replies:
    2
    Views:
    9,295
    Darryl L. Pierce
    Sep 11, 2005
Loading...

Share This Page