c# - 使用 jQuery ajax 加载母版页后,带有更新面板的 aspx 页面无法正常工作

标签 c# jquery asp.net ajax updatepanel

我有一个包含更新面板的 aspx 页面。并通过在母版页中的 div 内使用 jquery ajax 加载该页面。但是,更新面板的内容仅在第一次更新而其他时间不更新。

我的母版页:

<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>

我的 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>

我的 ASPX 页面代码隐藏

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

我的 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
}

非常感谢

最佳答案

在您提供的代码中,您从未调用函数 loadContent。我将 loadContent 添加到 jQuery ready() 并且它可以在我的计算机上运行。

如果您的页面上有两个表单,您就有两个“StateView”变量。当 asp.net 检查变量时它抛出错误可能是因为状态变量第二次来自 Site.Manager 的形式而不是来自 Page.aspx。我刚刚删除了母版页上的表单,现在按钮可以正常工作了。

我的母版页:

<%@ 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>

我的页面.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>

我的 ASPX 页面代码隐藏

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

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();
});

关于c# - 使用 jQuery ajax 加载母版页后,带有更新面板的 aspx 页面无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23852574/

相关文章:

c# - 每隔两行更改一次的方程式

asp.net - 两种类型的 asp.net 核心 Web 应用程序?

c# - 异步 asp.net 表

c# - "Specified cast is not valid"将字符串数组转换为 int 数组时

c# - 如果您被迫使用 Anemic 域模型,您将业务逻辑和计算字段放在哪里?

c# - Kinect 红外灰色 Pixelformat 转换为 8bpp

javascript - 向下滑动 SVG 元素的简单方法?

javascript - 更改 lib jquery.knob.js 的元素大小

c# - 加载 SNI.dll 失败

php - 我的第一个 jquery ajax 页面(初学者)