简体   繁体   English

使用jQuery ajax在母版页中加载后,带有更新面板的aspx页不起作用

[英]aspx page with update panel not work after load in master page with jQuery ajax

I Have a aspx page contain update panel . 我有一个aspx页面包含更新面板。 and load that page by using jquery ajax inside div in master page .However, the content off update panel just update for first time and other time not update. 并在母版页的div中使用jquery ajax加载该页面。但是,更新面板之外的内容只是第一次更新,而其他时间则不更新。

My Master Page: 我的母版页:

<head id="Head1" runat="server">
    <script src="Script/jquery-1.10.2.min.js" type="text/javascript"></script>
    <script src="Script/site.js" type="text/javascript"></script>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
    <div id="content">
        <div class="main">
            <asp:ContentPlaceHolder ID="contentMain" runat="server">
            </asp:ContentPlaceHolder>
        </div>
    </div>
</form>
</body>

My ASPX Page 我的ASPX页面

<head runat="server">
</head>
<body>
    <form id="form2" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel runat="server" ID="UpdatePanel" UpdateMode="Conditional">
        <ContentTemplate>
            <div style="text-align:center; margin:50px">
                <asp:Button ID="btnUpdate" runat="server" Text="update" Width="50px" 
                    onclick="btnUpdate_Click"/>
                <br />
                <asp:Label ID="lblTime" runat="server"></asp:Label>
            </div>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btnReg" EventName="Click" />
        </Triggers>
    </asp:UpdatePanel>
    </form>
</body>
</html>

My ASPX Page Code Behind 我的ASPX页面代码背后

protected void btnUpdate_Click(object sender, EventArgs e)
{
    lblTime.Text = DateTime.Now.ToString();
}

My JavaScript 我的JavaScript

$(document).ready(function () {
    $(window).load(function () {
        loadContent();
    });
});

function loadContent() {
    $.ajax({
        type: 'get',
        url: 'Page.aspx',
        contentType: 'application/html; charset=utf-8',
        dataType: 'html',
        beforeSend: sendContent,
        success: successContent,
        error: errorContent
    });
}
function sendContent() {
    ////loading
}
function successContent(data, status) {
    $("#content .main").html(data);
    $("#content").show();
}
function errorContent(request, status, error) {
    ////error
}

thanks a lot 非常感谢

In the code you provided you never call function loadContent. 在您提供的代码中,您永远不会调用loadContent函数。 I added the loadContent to jQuery ready() and it work on my computer. 我将loadContent添加到jQuery ready()中,并且可以在我的计算机上使用。

If you have two forms on your page you have two "StateView" variables. 如果页面上有两种形式,则有两个“ StateView”变量。 When asp.net checks for the variable it throws error probably because the state variable comes the second time from the form of Site.Manager and not from Page.aspx. 当asp.net检查变量时,它将引发错误,可能是因为状态变量第二次来自Site.Manager而不是Page.aspx。 I just deleted the form on Master Page and now the button works perfectly. 我刚刚删除了母版页上的表单,现在该按钮可以正常使用了。

My Master Page: 我的母版页:

<%@ Master Language="C#" CodeBehind="Site.master.cs" Inherits="WebApplication4.SiteMaster" %>

<!DOCTYPE html>

<html lang="en">
<head id="Head1">
    <script src="Scripts/jquery-1.10.2.min.js" type="text/javascript"></script>
    <script src="Scripts/site.js" type="text/javascript"></script>

</head>
<body>

    <div id="content">
        <div class="main">

        </div>
    </div>

</body>
</html>

My Page.aspx 我的Page.aspx

<%@ Page Language="C#" CodeBehind="Page.aspx.cs" Inherits="WebApplication4.Page" %>

<form id="form2" enableviewstate="false" runat="server">
<asp:ScriptManager ID="ScriptManager1">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Button ID="btnUpdate" Text="update" Width="50px" 
                onclick="btnUpdate_Click" runat="server"/>
        <asp:Label ID="lblTime" runat="server"></asp:Label>
                //////////// Some Tags ///////////
    </ContentTemplate>

</asp:UpdatePanel>
</form>

My ASPX Page Code Behind 我的ASPX页面代码背后

protected void btnUpdate_Click(object sender, EventArgs e)
{
    lblTime.Text = DateTime.Now.ToString();
}

site.js site.js

function loadContent() {
    $.ajax({
        type: 'get',
        url: 'Page.aspx',
        contentType: 'application/html; charset=utf-8',
        dataType: 'html',
        beforeSend: sendContent,
        success: successContent,
        error: errorContent
    });
}
function sendContent() {
    ////loading
}
function successContent(data, status) {
    $("#content .main").html(data);
    $("#content").show();
}
function errorContent(request, status, error) {
    ////error
}
$(function () {
    loadContent();
});

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

相关问题 需要使用c#中的更新面板通过主页面超链接事件异步加载.aspx页面 - Need to Load .aspx page asynchronously through Master page hyperlink event using update panel in c# jquery ajax发布到.aspx页面加载 - 如何读取变量发布? - jquery ajax post to .aspx page load - how to read variable posted? 文件上载在Ajax更新面板的页面中不起作用 - File Upload doesn't work in a page with Ajax Update Panel ajax jquery将vaule返回到aspx页面 - ajax jquery return vaule to aspx page jQuery AJAX发布到ASPX页面或ASHX Handler - jQuery AJAX Post to ASPX page or ASHX Handler 母版页中的更新面板和子页面中的asp文件上载 - Update panel in master page and asp fileupload in child page 当控件位于内容页面上的更新面板中时,更新母版页 - Updating the master page when the control is within an update panel on the content page 在aspx页面中加载一个控件 - Load one control after another in aspx page 继承相同母版页的.aspx页无法访问母版页中包含的jQuery文件 - jQuery file included in master page not accessible for .aspx page inheriting the same master page 什么是使用MVC + Ajax(jquery)加载页面内容,aspx或ascx或两者的最佳方法 - Whats the best way of using MVC + ajax (jquery) to load page content, aspx or ascx or both
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM