最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - How to use JavaScript strings to create a '<%= ...ClientID %>' dynamically? - Stack Over

programmeradmin0浏览0评论

Hi I trying to write a javascript generate <%= element.ClientID% > where element is one of the input parameter of function. I am trying to write a variable

var elementName = "<%=" + element + ".clientID%>" 

which is giving an error

CS0117: 'string' does not contain a definition for 'clientID'

Hi I trying to write a javascript generate <%= element.ClientID% > where element is one of the input parameter of function. I am trying to write a variable

var elementName = "<%=" + element + ".clientID%>" 

which is giving an error

CS0117: 'string' does not contain a definition for 'clientID'

Share Improve this question edited Mar 10, 2011 at 22:30 user166390 asked Mar 10, 2011 at 22:03 johnjohn 631 gold badge2 silver badges6 bronze badges 5
  • 3 What error are you are getting? – Piyush Mattoo Commented Mar 10, 2011 at 22:06
  • 1 what error does it give? – Ilya Saunkin Commented Mar 10, 2011 at 22:07
  • 2 What error do you get? ( follows the trend ) – meder omuraliev Commented Mar 10, 2011 at 22:08
  • CS0117: 'string' does not contain a definition for 'clientID' – john Commented Mar 10, 2011 at 22:16
  • Now where does that error appear? :) That will solve the riddle in a minute and a half. (See Visual Studio Error CS0117) – user166390 Commented Mar 10, 2011 at 22:26
Add a ment  | 

2 Answers 2

Reset to default 8

This throws an error because the server-side processing of <% %> tags happens first. Then, the result is output as text, which is JavaScript in this case. So, the server sees this <%=" + element + ".clientID%> and throws an error because it can't understand that. It's assuming that the string " + element + " is the value that has .clientID called on it.

There are two ways that you can get around this.

Option 1: Output All IDs

Make a hash of all possible values of element. If you have more than a couple, this method probably won't work for you.

var elements = [
    coolbutton: '<%= coolbutton.ClientId %>',
    simplebutton: '<%= simplebutton.ClientId %>',
    otherbutton: '<%= otherbutton.ClientId %>'
];

Then you can get the id by doing this:

var elementId = elements[element];

Option 2: Use Ends-with

If you can use jQuery, then there is handy way to refer to ASP.Net controls by ID. Essentially, all ASP.Net Ids end with the actual variable name you use in the code behind. So, if you have an element called CoolButton, you can find it in javascript, using jQuery, like so:

var element = $('[id$=CoolButton]');

The $= operator means ends-with. So, it finds all elements on the page that have an ID that ends with CoolButton. The actual ID of the ASP.Net control on the page will be something long, but it will always end with something like NamingContainer$CoolButton.

If you can't use jQuery, you could write this logic yourself.

function findByIdEndsWith(endId) {
    //loop through all elements on the page
        //if the current element's id ends with endId, return it

    //return null;
}

This type of function is discussed more here.

As EndangeredMassa points out, your server-side processing is evaluating <% %> (perhaps ERB?).

Try escaping the <%= with &lt;%=

发布评论

评论列表(0)

  1. 暂无评论