开发者

Why does this JS fail when I use a master page? (ASP.NET)

I'm currently working on a project where I need to use a lot of AJAX AutoCompleteExtenders, and they have been working fine- but now I'm tidying up the code and implementing a master page, I'm running into issues. I have this JS:

$开发者_如何学Pythonfind('txtName')._onMethodComplete = function(result, context) {

$find('txtName')._update(context, result, false);
webservice_callback(result,context);
};

And when I load the page, this error occurs relating to that snippet:

Microsoft JScript runtime error: 'null' is null or not an object

Just to reiterate, this only happens when I have a master page for some bizarre reason. Any ideas?


You need to use <%=txtName.ClientID%> because with master pages in use your ids will be mangled to avoid namecollision between masterpage and aspx/usercontrols etc.

However please note that to be able to use <%= you will have to include the JS in the ASP.NET markup code. This can be done with the IIS #include rather than using script's src.

Example:

<!-- #Include virtual=".\JS\YourJSFileWithASPNETMarkup.js" -->


Try View Source on your page and make sure that the <input> is still named txtName. Sometimes with master pages, the name changes.


Can you try this:

('<%=txtName.ClientID>')._onMethodComplete = function(result, context) {


If you're using .NET 4 then you could add ClientIDMode="Static" into the page directive, which would tell ASP.NET to keep it's mucky hands off the element IDs.


When you use masterpage, the ID of your controls change, so you can't use the same ID in Javascript, instead you can modify your selector like this : $('input[id*="txtName"]') now it finds all the controls which have an id that contains txtName. there are different selectors of this kind that you can use. here is a useful link

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜