views:

18

answers:

1

Last year we moved all our new functionality to Asp.Net MVC.

However we still have a load of legacy WebForms pages with lots of Javascript, and we're not going to get the chance to upgrade them any time soon.

More recently we also moved to Asp.Net 4, and that seems to have broken a load of the Javascript on these legacy pages.

Where before the client id would be:

ctl001_masterControlName_panelControlName_controlWeWant

Now they have become:

ctl001_masterControlName_panelControlName_controlWeWant_0

Where's that _0 suffix come from? There's only one controlWeWant in panelControlName, so the suffix adds no value.

I know hardcoded client ids are a bad idea with WebForms; one of the reasons we moved to MVC was the awful HTML produced by WebForms. However for this legacy code we're stuck with it, and I'd rather not go changing it (until the day comes when we have some free time to do it properly).

Why is Asp.Net 4 adding the _0 suffix at all?

Can I turn it off?

Is there any other way to avoid it?

A: 

Check out this, it explains some new functionality in asp.net 4.0 when it comes to id generation.

Onkelborg
That's great - it looks like future WebForms programmers will be able to make it produce slightly less awful HTML (those that haven't already run for the MVC hills). Unfortunately it doesn't answer my question: why has it added `_0` to the client id and how can I remove it without rewriting everything?
Keith
Try this: http://forums.asp.net/p/1305766/2558955.aspx :)
Onkelborg
Thanks, but that describes a similar problem with a different cause - they were duplicating ids so the suffix was being automatically generated (on Asp.Net 2 too by the looks of it). I have a page that worked in Asp.Net 2, but in 4 the same code (recompiled) now has `_0` suffixed to the client ids and I want it to behave like it used to.
Keith
Well, it's hard to say without a real world sample, but.. It's an effect of what the last link was about. Here's one more link that describes it: http://www.4guysfromrolla.com/articles/031710-1.aspx
Onkelborg
Ah, that lead to something that appears to be the cause - the control is in a table, so stupid WebForms assumes that it will repeat the row content and adds the `_0` just in case. Unfortunately the question remains - how do I turn this breaking change off?
Keith
Try this link: http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx If you set the id generation mode to Legacy then you should get the old behaviour
Onkelborg