How to use JQuery with Master Pages?

后端 未结 13 1120
甜味超标
甜味超标 2020-12-04 18:13

I can get simple examples to work fine as long as there\'s no master page involved. All I want to do is click a button and have it say \"hello world\" with the javascript in

13条回答
  •  离开以前
    2020-12-04 18:42

    • PROBLEM --> when using Site.Master pages the control id names (for ASP controls) get the ContentPlaceHolderID prefixed to them. (Note this not a problem for non-asp controls as they don't get 'reinterpreted' - i.e. they just appear as written)

    • SOLUTIONS:

        1. Simplest --> add ClientIDMode="Static" to the asp control definition (or set with properties) in aspx page
      • Alternatives include:
        1. Hardcoding the ContentPlaceHolderID name in the js code e.g "#ContentPlaceHolder1_controlName" - eek!!!!
        1. using the <%= controlName.ClientID %> in the ASP page - plus, assigning it - there- to a variable (or object of variables). The variable (or object dot notation) can then be used in external js page (NOTE: Can't use <%= controlName.ClientID %> in external js)
        1. Using CssClass with a unique(same name as ID) in ASP page and refering to the control as ".controlName" instead of "#controlName"
        1. Using the "[id$=_controlName]" instead of "#controlName" - this is involves a small search and is looking for a control that ends with the unique name - that way the start is irrelevant

提交回复
热议问题