简体   繁体   English

通过ASPX页面加载文本文件

[英]Loading Text File Through ASPX Page

Ok I am working on a project using Web Pages. 好的,我正在使用Web Pages进行项目。 The Task is to load Text from a Text File and use it in a div. 任务是从文本文件加载文本并在div中使用它。 I used the ajax xmlhttprequest, the tutorial used in http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first . 我使用了ajax xmlhttprequest,即http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first中使用的教程。 All is working fine, but the problem is the function is called when the button is clicked. 一切工作正常,但是问题是单击按钮时调用了该函数。 But i want it to be called when the page loads. 但是我希望在页面加载时调用它。 That is when the page is immediately opened. 那是当页面立即打开时。 And I dont know how to use it. 而且我不知道如何使用它。 Here is my code:- 这是我的代码:

My Default.aspx File..... 我的Default.aspx文件.....

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master"  AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Project02._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <script src="/Scripts/Dynamic.js"></script>
    <div id="Header"></div>




</asp:Content>



    function loadXMLDoc() {

    window.onload = loadXMLDoc;

    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("Header").innerHTML = xmlhttp.responseText;
            document.getElementById("Header").innerHTML = loadXMLDoc;
        }
    }
    xmlhttp.open("GET", "http://localhost:49683/Scripts/Content1.txt", true);
    xmlhttp.send();
}

Please Tell me how to code in here. 请在这里告诉我如何编码。 Thank You in advance 先感谢您

Your javascript needs to be wrapped in a script block. 您的JavaScript需要包装在脚本块中。 Also the window.load event needs to be outside the function declaration. 而且window.load事件也需要在函数声明之外。 Currently the function will never fire. 目前,该功能永远不会触发。

   <script type='text/javascript'> 
     window.onload = loadXMLDoc;
    function loadXMLDoc() {

    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("Header").innerHTML = xmlhttp.responseText;
            document.getElementById("Header").innerHTML = loadXMLDoc;
        }
    }
    xmlhttp.open("GET", "http://localhost:49683/Scripts/Content1.txt", true);
    xmlhttp.send();
}
    </script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM