Framework-generated ID woes

M

mark.norgate

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
 
B

Bruno Alexandre

component.ClientID

:)

like:

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

mark.norgate

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
 
B

Bruno Alexandre

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)


<[email protected]> escreveu na mensagem
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
 
M

mark.norgate

Yeah, perhaps what I wrote could be misinterpreted!

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

Mark
 
M

Mark Rae

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...
 
M

mark.norgate

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

Cheers, Mark
 
B

Bruno Alexandre

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!
 
M

Mark Rae

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...
 
M

mark.norgate

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
 
B

Bruno Alexandre

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)


<[email protected]> escreveu na mensagem
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
 

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,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top