ASP.Net 3.5 ID de cliente diferente a 4.0

We're attempting to upgrade our site from framework 3.5 to 4.0. Everything appears to be fine server side - My only problem so far is the IDs of a handful of custom controls.

The Controls deceleration looks like:

    <div runat="server" id="gglSelectedItemsImage" class="gplSelectedItemsText">None selected</div>

When rendered in 3.5 the control looks like:

<div id="gplCategories_gglSelectedItemsImage" class="gplSelectedItemsText">Some Content</div>

And in 4.0 Like:

<div id="ctl00_ctl00_ctl00_ContentPlaceHolder1_Content_gplCategories_gglSelectedItemsImage" class="gplSelectedItemsText">Some Content</div>

This is causing some of our older JavaScript/JQuery to fail.

Yo he puesto <pages clientIDMode="AutoID" controlRenderingCompatibilityVersion="3.5" /> in the web.config so 99.9% of the IDs are correct. The weirdest thing is the 3.5 control looks like it rendering with clientIDMode="Predictable" rather than the standard 3.5 auto IDs?

Furthermore if I access the controls clientID Server side in 4.0 it appears correct:

Me.gglSelectedItemsImage.ClientID = "gplCategories_gglSelectedItemsImage"

This is the main problem, any values inserted into the JavaScript ect are actually the wrong values.

Our site is unfortunately to large to just fix this one error so I need to work out why the control is rendering its ID differently and implement it across the site.

Any idea's you guys have will be very much appreciated.

preguntado el 08 de noviembre de 11 a las 15:11

Why is your older JavaScript/jQuery relying on hardcoded client ids anyway? -

I agree with LukeH. The ClientID property exists so you do not hand-craft them yourself, regardless of framework version. -

It was before my time so I don't know. In this instance the we have a generic control that excepts a clientID of another control ("gglSelectedItemsImage") then using Jquery to update its value. Problem is caused by the Server thinking the ClientID is "gplCategories_gglSelectedItemsImage" when the actual rendered documents is "ctl00_ctl00_ctl00_ContentPlaceHolder1_Content_gplCategories_gglSelectedItemsImage" class="gplSelectedItemsText" -

Consider switching to MVC, it plays much nicer with JQuery. -

As cool as MVC is if I'm having this many problems going up 0.5 of a version I don't want to think about the lack of sleep MVC will give me! -

3 Respuestas

Prueba esta

<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>

respondido 08 nov., 11:19

Thanks Fahad but I've also already got controlRenderingCompatibilityVersion="3.5" (I'll update my question to include it) - pingoo

Do you have multiple web.config files? - Fahad

We have different ones deepening on the current build configuration (debug/Live ect) but that's it. Having looked at it there is nothing that would override the normal <Pages> tag, I did try however but it didn't help (little bit glad - would have been as massive facepalm moment). - pingoo

In the second example you're using a master page, which explains why the ClientID is different. It shouldn't make a difference though if you're referencing the control by ClientID:

var ctrl = $("#<%=gglSelectedItemsImage.ClientID%>");

respondido 08 nov., 11:19

Both examples are from the same website, one complied in 3.5 the other in 4.0. So any master page influences should effect both I assume? - pingoo

You should access the client id's like this in your .ASPX file:

<%=gglSelectedItemsImage.ClientID%>

respondido 08 nov., 11:19

This is the problem the Servers ClientID is not the same as the actual controls. - pingoo

That sounds very strange. It's almost like the site thinks it's running as autoid but the page is running internally as predictable. You could try and inject the client id in the javascript through SaveStateComplete event. I can't see how the client id would be wrong at that point. - Materia gris

how about to access it via a js file? your code blocks doesn't work there... so your answer fails(in 4.0 you can set your clientidmode to static which works great, but in 3.5 it's a total BS) - Bebe del infierno

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.