Me vs. .NET HTML Control naming conventions

J

Josh Wolf

The naming conventions that .NET uses in its HTML controls are driving
me crazy! Pulling one in from a control or within a master page, for
example, causes .NET to add class info to the "name" attribute. So, if
I have a control that contains:

<input class="TextField" id="foo" runat="server"/>

It ultimately renders as <input name="_ctl0:[control ID]:foo"
type="text" id="_ctl0_[control ID]_foo" class="TextField" />

This, of course, makes it problematic to refer to these forms fields
via javascript. I've also got plain-vanilla HTML input fields I'm
rendering via XML that are named properly.

I realize I could convert EVERYTHING over to .NET controls and
validation, but you know what? I don't want to, and I shouldn't have
to. Is there any way to tell .NET to render the Name (it can keep ID)
attribute the way I want, short of creating a custom derived class for
each type of HtmlInput type to override the render method? And if I do
so, will I break things like ViewState?
 
M

Marina Levit [MVP]

In javascript, you have to refer not to the hard coded id, but to a
dynamically emitted ClientID. If you look at the ASP.NET controls, they all
have a serverside ClientID property which is what the actual 'id' in the
HTML will be. So create a javascript variable, the value of which gets set
once in the beginning by Response.Writing the ClientID property or by <%=
%>, or whatever. Then, use that javascript variable all over the place to
refer to the control.
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top