Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1971
  • Last Modified:

Why does (runat="server") prefix my id tags with ctl100_?

In my html document I have a line like:

            <h1 runat="server" id="testText">SAMPLE HEADING</h1>

to dynamically change the inner text at run time in the code behind using:

             this.testText.innerText="NEW HEADING";

....

I want to be able to use CSS to style this heading based on the id tag of "testText"; however at run-time the id name gets prefixed with "ctl100_"

Is there a way to prevent this prefixing?

Using C# and ASP.NET 2.0
0
ctl100
Asked:
ctl100
  • 2
2 Solutions
 
David H.H.LeeCommented:
Hi ctl100,
This is .net interpreted id after rendered in HTML version. If you're trying to refer the id in javascript based on rendered ID, you can check its client ID instead.
eg:
<script>
var obj;
obj=document.getElementById('<%= testText.ClientID%>');
obj.className='yourCssClassID';
</script>

Lastly, you can't prevent the prefix include inside the rendered ID. That is spontaneous behavior for the page that running under .net framework.

0
 
ctl100Author Commented:
So, there is no way to stop .NET from prefixing?
0
 
tetorvikCommented:
It's .NET Framework that prefixes controls to identify them uniquely. Each control that implements INamingContainer prefixes child controls with its own id.
Therefore ids can grow very large and become unusable in many circumstances. I don't think there is a workaround for that until ASP.NET 4.0 comes in.
http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx

So until them I would engourage to identify your style with CSS classes.
0
 
David H.H.LeeCommented:
Hi tetorvik,
Good approach for the incoming .net 4.0 framework. Thanks for sharing thought here.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now