Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Sys' is undefined - Ajax - master and contentpages ASP.NET 4.0

I've been having this problem for quite a while now and tried various solutions, Editing the Webconfig with various tags.

Im using Updatepanels, timers and AJAX on my webpage.(can it be AJAX that throws the error?)

I really dont know how to define the problem, becuase i really dont know where to trace it.

I've placed a onload on the body in the masterpage (dont know if thats right) Heres some markup code and my Javascript code that i think throws the error.

MASTERPAGE:

<!DOCTYPE html>
<html lang="en">
<head runat="server">
    <meta charset="utf-8" />
    <title><%: Page.Title %> - </title>
    <link href="~/Content/Site.css" rel="stylesheet" /> 
    <link href="~/Content/BarChart.css" rel="stylesheet" />
    <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <asp:PlaceHolder runat="server">        
        <script src="<%: ResolveUrl("~/Scripts/_references.js") %>"></script>
        <script src="<%: ResolveUrl("~/Scripts/modernizr-2.5.3.js") %>"></script>
        <script src="<%: ResolveUrl("~/Scripts/jquery-1.7.1.js") %>"></script>
        <script src="<%: ResolveUrl("~/Scripts/jquery-1.7.1.min.js") %>"></script>
        <script src="<%: ResolveUrl("~/Scripts/jquery-ui-1.8.20.js") %>"></script>
        <script src="<%: ResolveUrl("~/Scripts/jquery-ui-1.8.20.min.js") %>"></script>
        <script src="<%: ResolveUrl("~/Scripts/ButtonManager.js") %>"></script>
        <script src="<%: ResolveUrl("~/Scripts/BarChart.js") %>"></script>
    </asp:PlaceHolder>
    <meta name="viewport" content="width=device-width" />
    <asp:ContentPlaceHolder runat="server" ID="HeadContent" />
</head>
<body >
    <form runat="server">
    <asp:ScriptManager runat="server">
        <Scripts>
            <asp:ScriptReference Name="jquery" />
            <asp:ScriptReference Name="jquery.ui.combined" />
        </Scripts>
    </asp:ScriptManager>
    <header>
        <div class="content-wrapper">
            <div class="float-left">
                    <asp:ImageButton ID="Logo" cssclass="site-title" runat="server" PostBackUrl="~/" ImageUrl="~/Images/Logo.png" />
            </div>
            <div class="float-right">
                <section id="login">
                    <asp:LoginView runat="server" ViewStateMode="Disabled">
                        <AnonymousTemplate>
                            <ul>
                                <li><a id="registerLink" runat="server" href="~/Account/Register.aspx">Register</a></li>
                                <li><a id="loginLink" runat="server" href="~/Account/Login.aspx">Log in</a></li>
                            </ul>
                        </AnonymousTemplate>
                        <LoggedInTemplate>
                            <p>
                                Hello, <a runat="server" class="username" href="~/Account/Manage.aspx" title="Manage your account">
                                    <asp:LoginName runat="server" CssClass="username" />
                                </a>!
                                <asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/" />
                            </p>
                        </LoggedInTemplate>
                    </asp:LoginView>
                </section>
                <nav>
                    <ul id="menu">
                        <li><a runat="server" href="~/">Home</a></li>
                        <li><a runat="server" href="~/Barchart.aspx">About</a></li>
                        <li><a runat="server" href="~/Contact.aspx">Contact</a></li>
                    </ul>
                </nav>
            </div>
        </div>
    </header>
    <div id="body">
        <asp:ContentPlaceHolder runat="server" ID="FeaturedContent" />
        <section class="content-wrapper main-content clear-fix">
            <asp:ContentPlaceHolder runat="server" ID="MainContent" />            
        </section>
    </div>
    <footer>
        <div class="content-wrapper">
            <div class="float-left">
                <p>&copy; <%: DateTime.Now.Year %> - My ASP.NET Application</p>
            </div>
        </div>
    </footer>  
    </form>
</body>
</html>

CONTENTPAGE:

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
</asp:Content>
<asp:Content runat="server" ID="BodyContentHome" ContentPlaceHolderID="MainContent" >
    <asp:Panel ID="UpdatepanelWrapper" CssClass="Updatepanelwrapper" runat="server">
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" >
            <ContentTemplate>
                <asp:PlaceHolder runat="server" ID="WholeWrapper">

                    <asp:PlaceHolder runat="server" ID="QuestionWrapper">
                        <asp:PlaceHolder runat="server" ID="LabelQuestion"></asp:PlaceHolder>
                        <asp:PlaceHolder runat="server" ID="Question"></asp:PlaceHolder>                        
                    </asp:PlaceHolder>
                </asp:PlaceHolder>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" />
                <asp:AsyncPostBackTrigger ControlID="btnDelete" EventName="Click" />
            </Triggers>

        </asp:UpdatePanel>    
        <asp:Panel ID="ButtonPanel" CssClass="ButtonPanel" runat="server">
            <asp:ImageButton ID="btnDelete" runat="server" ImageUrl="~/Images/Deleteicon.png" CssClass="DeleteButtonHidden" OnClientClick="btnDelete_Click" />
            <asp:ImageButton ID="btnAdd" runat="server" ImageUrl="~/Images/Addicon.png" CssClass="AddButtonMoreMargin" OnClientClick="btnAddQuestion_Click" />
        </asp:Panel>
        </asp:Panel>  
    <asp:Button ID="btnSendQuestions" runat="server" Text="Skapa Föreläsning och frågor" OnClick="btnSendQuestions_Click" ValidationGroup="QuestionGroup" />

</asp:Content>

JAVASCRIPT CODE:

if (window.addEventListener) window.addEventListener("load", load(), true);
else window.onload = load();

function load() {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}

function EndRequestHandler(sender, args) {
// NOT IMPORTANT
}
like image 956
J.Olsson Avatar asked Dec 15 '22 13:12

J.Olsson


2 Answers

I just solved the problem. I think. I knew the function was run before the DOM was build.

So i just did this

$(document).ready(function () {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
});

One problem yet presists. When i clicking on the Imagebutton btnAdd The console says:

Uncaught ReferenceError: btnAddQuestion_Click is not defined
onclick

this directs me to my code line where this ImageButton is written.

Internet Explorer doesn't like this at all. but other browsers just proceed like it was nothing, even if the error message appears in the Dev-Console.

The Button functionality works to. the only problem is the error.

EDIT: This is now Solved. i was using "OnClientClick". on the ImageButtons. I changed it to OnClick and that solved this issue. I dont know the difference between theese two, but im happy as long as it works.

like image 88
J.Olsson Avatar answered Dec 27 '22 20:12

J.Olsson


In my case I found that the culprit was caused by missing the call to MyBase.OnPreRenderComplete(e) in the overridden version of the function/method OnPreRenderComplete.

By including the call to MyBase.OnPreRenderComplete(e) everything works nicely. If the call to MyBase.OnPreRenderComplete(e) is missing then the error "Uncaught ReferenceError: Sys is not defined" is received and many things don't work properly.

Protected Overrides Sub OnPreRenderComplete(ByVal e As EventArgs) 'MyBase.OnPreRenderComplete(e) ... End Sub

like image 26
M Kelly Avatar answered Dec 27 '22 19:12

M Kelly