altering .UniqueID

J

jason.hau

Is there any way to alter this? I am aware that it's read only.
Essentially the problem is that I have a webcontrol that dynamically
creates a textbox and a requiredfieldvalidator control with the
validator validating the textbox. This worked fine in my test harness
and works fine in my other projects using master page based pages when
in IE but in Firefox, the other project doesn't work. I've been able to
narrow it down to teh fact that we've got a preappend on the ID of the
controls of "_ctl0" and as an underscore isn't a valid start character,
firefox doesn't run the javascript (quite rightly).
I'd prefer to alter this within the original code that creates the sub
controls rather than hack it in the other project as it's to be part of
a common library for use elsewhere.
 
R

RVB

Not really an option, the validators work by writing a javascript array
called Page_Validators, automatically generated by .NET itself. This is
then referenced by another automatically generated item, a javascript
file, which it pulls out using a handler via
webresource.axd?somerandomguid. As such, can't really alter the
javascript call itself where the validation fires off nor is it really
practical to try and reverse engineer this js file.
I have found another ID reference: ClientID but again, read only. The
only one which you appera to be able to alter is ID which is of no use
whatsoever. All I want is to be able to change the ID, I fail to
understand why this is so hard, especially as I keep hearing "oh yes,
validators have been fixed to work with Firefox in .NET 2.0".
BTW, Test Harness works in Firefox because it lacks the auto
pre-append. I'm not exactly sure where this auto generated _ctl0 is
coming from. Admittedly the control sits inside an ascx (which has a
normal id) and then itself with a contentplaceholder based off a
seperate master page. If I knew where that was coming from I would be
able to override it but the control has an ID, the placeholder has an
ID and I'm at a loss to determine why it's appearing.
 
E

Edwin Knoppert

I don't have ff installed but i often needed to use document.GetEl.. ByID(
'name of control') to get an object refer.
Is the quoted way not a solution?
 
E

Edwin Knoppert

Like:
var o = document.getElementById('<%=txt_Index.ClientID%>');
o.value = ....
 
R

RVB

As far as I'm aware, it's not, reason being that the actual javascript
array creation is done by the .NET framework automatically if it
detects that there are validators in the validator collection. It is
doing essentially the same as your snippet, it just outputs the actual
clientid for the validator control (at that point rendered as a span)
instead of doing a codebehind call. I can't actually get alter this
array and in any case, it wouldn't work because of the actual format of
the ID:
Using your example, "txt_Index.ClientID" would equate to
"_ctl0_txt_Index1". As it starts with an underscore, firefox doesn't
recognize it as a correct ID. So, as you can see, the problem stems
from the whole auto-generation of the clientID. Fix that and you'd be
laughing.
Several other people have apparently got around this by doing
control.ClientID = control.ClientID.Replace("_","") but I find that
highly unlikely as you -cannot- set the ClientID property, nor the
UniqueID.
 
E

Edwin Knoppert

I had the thought the quoted situation might have helped
(getElementById('...')
Have you actually tried this approach?
 
R

RVB

I -cannot- alter the javascript as it is auto-generated and the API
does not provide me with an entry point to do so.

I have gotten a touch further, the original _ctrl0 was coming from the
ID of the Master.ID property on the page itself. When I altered the ID
of the Page.Master property, I was able to remove the "_" from the
generated clientID so it ends up as "ctrl0_txtSomething_ctrl1" which,
as far as I can see is definately a valid ID which can be passed to the
document.getElementById function. However, still doesn't work.

Additionally, I've crossposted to the javascript newsgroup but has
anyone hit this before in .NET?
 
E

Edwin Knoppert

I -cannot- alter the javascript as it is auto-generated and the API does
not provide me with an entry point to do so.

That was not my point.
I asked if you tried to obtain an object using this getElementById('...')
often people directly refer to a control in js, which not always works.
Obtaining a refer to the control using getElementById('...') is imo better.

So.. a simple:
alert( getElementById(' your clientid stuff here ') );

Does that result in an object or null?
 
R

RVB

Sorry for the mix up.
I've actually got a touch further in that it now does populate the
array correctly but the actual validation function isn't being called.
I've checked with various formats of IDs and it definately wasn't
picking it up when using an underscore as the beginning character, i.e.
the array was empty. Got it to output without that beginning underscore
and it now picks up the elements. Now just seeing why it doesn't
register the validator.IsValid property as false.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top