Can we use $(document).ready() in asp.net page?

此生再无相见时 提交于 2019-12-13 00:01:39

问题


I have an error trying to use $(document).ready() as in the above image. What should i try to solve this problem ?


Edit 23/05/2011 10:54

I have got a clue. The page i am working inherit from masterpage

In master page this code isn't work, maybe problem of different path

<head id="Head1" runat="server">
<title>Back Office</title>
<link href="~/Styles/MasterPage.css" rel="stylesheet" type="text/css" />
<link href="Styles/custom-theme/jquery-ui-1.8.12.custom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="scripts/jquery-1.5.1.min.js"></script>
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>

This code don't seems to work

On the body of master page, there is a script manager and jquery is there, This make the jQuery works, is because we use scriptmanager in the body so that the document.ready is not work ?

            <asp:ScriptManager ID="ScriptManager1" runat="server" OnAsyncPostBackError="ScriptManager1_AsyncPostBackError">
                <Scripts>
                    <asp:ScriptReference Path="~/Scripts/jquery-1.5.1.min.js" />
                </Scripts>
            </asp:ScriptManager>
            <asp:ContentPlaceHolder ID="MainContent" runat="server">
            </asp:ContentPlaceHolder>

I have found the problems.

The problem is the jquery code is on the masterpage, in the script manager, and script manager is in inside the body tag.

the document.ready is on the header tag on page that inherit on master page, so before the code to excuting to the body, the jquery is not included yet and that wat it is error.

The easiest way to solve this is, i have to move javascript code in the bottom of body tag. The proper way to solve, which i still can't find is to put the javascript include code in the header. but i don't find a way that work yet. My master page and inherit page is on the differnt path. I have find a lot of technique such as but none of them work.


回答1:


Take out all of the other javascript includes except for jquery-1.5.1.min.js. Try it with just that one include. If that works, add the other includes back in one at a time until you get the error. If it doesn't work with just the jQuery script included, chances are it's not pointing at the correct path.

Short answer though, yes, you can use jQuery and document.ready in an ASP.NET page.




回答2:


Surely you can. Seems, however that jQuery object is not available at the moment of script execution. Verify that jquery library file is indeed loaded via script directive. Or maybe there are some other nuances behind this.



来源:https://stackoverflow.com/questions/6092379/can-we-use-document-ready-in-asp-net-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!