What's the best way to deal with ASP.NET's ClientID

后端 未结 4 1838
闹比i
闹比i 2021-01-15 03:55

I\'m trying to find the best way to deal with the way that ASP.NET prepends a value to any element that is created with runat=\"server\", without having to reso

相关标签:
4条回答
  • 2021-01-15 04:08

    The right answer is as follow

    var txt1 = $('#<%= txt1.ClientID %>');
    $(txt1).click(function () {
        alert('hi');
    });
    

    her I create a new variable on the javascript which is reference to the control based on its rendered client ID

    0 讨论(0)
  • 2021-01-15 04:11

    If you are using .Net framework 4.0 or higher ,Use the ClientIDMode property of the Control

    You may use Static as the ClientIDModeEnumeration

    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.

    0 讨论(0)
  • 2021-01-15 04:17

    Why not just use ClientIDMode=Static? Set it in the Web.config so that your client IDs will be as-written throughout the site.

    0 讨论(0)
  • 2021-01-15 04:20

    You can use the jQuery ends with selector:

    $('input[id$="myServerId"]')

    But remember, that kind of selector is a lot slower than selecting directly with the id with <%= id.ClientID %>.

    http://jsperf.com/id-vs-ends-with

    0 讨论(0)
提交回复
热议问题