How to stop ASP.NET from changing IDs in order to

2019-01-02 15:35发布

I have this label control in my web page

<asp:Label ID="Label1" runat="server" Text="test"></asp:Label>

And when the page rendered the id of the control changes to something like this

  <span id="ctl00_ContentPlaceHolder1_Label3">test</span>

How can I stop asp.net from changing IDs in order to perform a jQuery operation like this

$('#label1').html(xml);

20条回答
谁念西风独自凉
2楼-- · 2019-01-02 15:54

instead of using this selector

$('#Label1')

use this one, basically you are using the classic asp inline server side code.

$('#<%= Label1.ClientID %>')

this will insert whatever ID that is generated to be placed as a literal.

If you wish to use external files then I would suggest you create an obj that is global on the page to hold all of your client ids and then reference that obj within your external files (not ideal but it is a solution)

var MyClientIDs = {
    Label1 = '<%= Label1.ClientID %>',
    Label2 = '<%= Label2.ClientID %>',
    Textbox1 = '<%= Textbox1.ClientID %>',
    Textbox2 = '<%= Textbox2.ClientID %>'
};

and then in your external file you can access Label1 for example: $('#' + MyClientIDs.Label1)

查看更多
心情的温度
3楼-- · 2019-01-02 15:56

.NET 4 now has the ability to let you choose your ClientIDMode:

Type: System.Web.UI.ClientIDMode

A value that indicates how the ClientID property is generated. The default is Inherit.

AutoID The ClientID value is generated by concatenating the ID values of each parent naming container with the ID value of the control. In data-binding scenarios where multiple instances of a control are rendered, an incrementing value is inserted in front of the control's ID value. Each segment is separated by an underscore character (_). This algorithm was used in versions of ASP.NET earlier than ASP.NET 4.

Static The ClientID value is set to the value of the ID property. If the control is a naming container, the control is used as the top of the hierarchy of naming containers for any controls that it contains.

Predictable This algorithm is used for controls that are in data-bound controls. The ClientID value is generated by concatenating the ClientID value of the parent naming container with the ID value of the control. If the control is a data-bound control that generates multiple rows, the value of the data field specified in the ClientIDRowSuffix property is added at the end. For the GridView control, multiple data fields can be specified. If the ClientIDRowSuffix property is blank, a sequential number is added at the end instead of a data-field value. This number begins at zero and is incremented by 1 for each row. Each segment is separated by an underscore character (_).

Inherit The control inherits the ClientIDMode setting of its NamingContainer control.

查看更多
步步皆殇っ
4楼-- · 2019-01-02 15:56

You have to pass the control's ClientID to the javascript code (I doubt though that Label1 gets renamed to Label3)

ClientScriptManager.RegisterClientScriptBlock(GetType(), "someKey", 
    "$('#" + Label1.ClientID + "').html(xml);", true);
查看更多
其实,你不懂
5楼-- · 2019-01-02 15:59

you don't need to "prevent" asp.net from changing the control id in order to use jquery, or javascript at all for that matter.

the id that gets rendered on your page is the ClientID property of the control and what you're setting on the control itself is the ID. you can't set the ClientID property, it's generated for you and may or may not be the same as your ID. however, as others have mentioned you can use the ClientID property either in your aspx:

$('<%= Label1.ClientID %>').html()

or by registering the client script in your code behind file using one of the ClientScriptManager methods.

查看更多
倾城一夜雪
6楼-- · 2019-01-02 16:00

set ClientIDMode="Static". This will solve your problem.

Example:

<asp:Label ID="Label1" ClientIDMode="Static" runat="server" Text="test"></asp:Label>
查看更多
琉璃瓶的回忆
7楼-- · 2019-01-02 16:01

If and only if you're container layout is never going to change and you require to put your JavaSctipt/jQuery in an external file, you could use the generated ids within your jQuery selectors i.e.

$('#ctl00_ContentPlaceHolder1_Label3').html(xml);

Obviously, this approach requires you to find out what the generated ids will be and requires caution if you ever start changing the site/application construction.

Otherwise, your best options are

1. Use the inline server side code markup. The downside to this approach is that you cannot put your js code in an external file -

$('#<%= Label3.ClientID %>').html(xml);

2. Define unique CSS classes on each control you need to use in your jQuery, which would still allow you to put your js code in an external file -

<asp:Label ID="Label3" runat="server" Text="test" CssClass="label3">
</asp:Label>

$('.label3').html(xml);

3. Use jQuery selectors to pattern match the original id, which again, would allow you to put your js code in an external file -

$('[id$=Label3]').html(xml);

This jQuery selector will select all elements with attribute id whose value ends with Label3. The only potential downside that I could see with this approach is that in theory, it could be possible to have a Label control with id Label3 in say, a Master page and also in two content pages. In this example, using the jQuery selector above would match all three labels, which may have unwanted consequences.

EDIT:

I thought it might be useful to raise your attention to the IDOverride control. An Example page can be found here

It allows you to specify which controls should have their mangled id within the outputted HTML markup overridden with the id as is given in the .aspx file when rendering out the HTML page. I have only played with it briefly with a single Master Page and Panels, but it appears to work well. Using this, you could use the original ids within your jQuery selectors. Be aware however, that the results are unpredictable if you were to have controls with the same ids in your Master page(s) and Content page(s) that are combined to render the HTML for one page.

查看更多
登录 后发表回答