问题描述:

We are trying to write a form at ASP.net using visual studio.

We want to be able to control the final client side ID of elements so we can refer to them in the jquery code easily.

The regular 'id' attribute at ASP.net gets an odd prefix that we cannot control at the client side.

How can we fully control the final client side ID of an element?

Thank you!

IE:

<asp:TextBox ID="TBICDCode6" runat="server" Width="240">

I want to be able to refer to the text box using

$("#TBICDCode6");

网友答案:

If you are using ASP.NET 4.0, you can do this by setting Control.ClientIDMode to static

<asp:TextBox ID="TBICDCode6"  runat="server" 
        ClientIDMode="Static"
        Width="240">
</asp:TextBox>

Or, if you are using an older version of ASP.NET, try this.

$("#" + '<%= TBICDCode6.ClientID %>')
网友答案:

Try:

$("#<%= TBICDCode6.ClientID %>");
网友答案:

it's a well known fact that asp.net does nasty things to ids. it's supposed to be better in asp.net 4.0, but I haven't tried it yet.
try reading 'Referencing Server Controls in Client Script' here.
Alternatively, you can bypass the whole thing completly by using another attribute (such as name).
your selectors will then be $("[name=someId]"), instead of $("#someId")

相关阅读:
Top