jquery - Masterpage javascript causes an object reference error when asp.net control doesn't exist in the content page -
so here's deal: have asp.net control located on content pages not others. reference asp.net control within $(document).click event, code located in master page. code below how went doing that; searches contentplaceholder "body" control "dep_ddb" , retrieves client id.
var dep_ddb = document.getelementbyid('<%= body.findcontrol("dep_ddb").clientid %>'); var filterbutton = document.getelementbyid('<%=body.findcontrol("filterbutton").clientid %>'); this works great when loading page includes control. unfortunately, when loading page doesn't use control uses master page, object reference error on vb code: <%= blah blah %>.
i tried using javascript if statement detect if content page doesn't include control (simply using title check if dealing content page know won't include control):
if (ttl.indexof('| sign up') == -1) { var dep_ddb = document.getelementbyid('<%= body.findcontrol("dep_ddb").clientid %>'); var filterbutton = document.getelementbyid('<%=body.findcontrol("filterbutton").clientid %>'); }; . . . still caused object reference error. believe asp trying compile code included in <% %> tags first, , returning error since doesn't exist. mean there no javascript-based "if statement" can use omit code when necessary.
so question this:
what method can used safely reference asp.net control master page when control exists in content page, , omit reference when not exist?
please both of below links. may you'll help:
check if element exists in jquery
is there "exists" function jquery?
one more thing, instead of using body.findcontrol("dep_ddb").clientid try give direct client id getting page source. won't show server error.
Comments
Post a Comment