【问题标题】:Script not executing on Ajax postback脚本未在 Ajax 回发上执行
【发布时间】:2023-03-24 17:46:02
【问题描述】:

我正在开发一个显示一些图块的网站。瓷砖的高度可能会因插入的图像和文本而异。为了确保所有的瓷砖都达到相同的高度,我在 head 中编写了以下脚本:

function BindEvents()
{
    $(window).load(function () 
    {
        var maxHeight = Math.max.apply(null, $(".producttile").map(function () {
            return $(this).height();
        }).get());

        $('.producttile').height(maxHeight);

        maxHeight = Math.max.apply(null, $(".clistdiv").map(function () {
            return $(this).height();
        }).get());

        $('.clistdiv').height(maxHeight);
    });
}

以上脚本已绑定到datalist,如下:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <hr width="100%" noshade/>
        <script type="text/javascript">
            Sys.Application.add_load(BindEvents);
        </script>
        <asp:DataList 
                ID="DataList1"
                runat="server"
                onitemcommand="DataList1_ItemCommand1"
                class="itemsdatalist"
                RepeatDirection="Horizontal"
                RepeatLayout="Flow"
                onload="DataList1_Load">
            <ItemTemplate>
                ...........rest of the code

现在,首次加载页面时,这种绑定和磁贴自动调整大小可以正常工作。一个按钮会触发一个 ajax 调用并引入更多图块并重新更新此更新面板。这就是问题所在。

BindEvents() 没有被再次调用,因此瓷砖大小不同,它们不会再次自动调整。

我也尝试将脚本保留在更新面板内容模板中,希望它能够正常工作,但难怪,它根本没有工作。

请帮忙。

【问题讨论】:

  • 尝试使用 $("#UpdatePanel1").load 而不是 $(window).load
  • 您好,我已经尝试过您的解决方案,其中包含更新面板控件的类和 ID,但没有一种方法有效。
  • 我会尝试完全删除 window.load,因为每次加载 ContentTemplate 时都会调用此函数,对吗?我会在你的 BindEvents 函数中做一个 console.log,看看它什么时候被调用。
  • 好的,我写了 3 个脚本:在 head 部分,我写了一个带有一些消息的 console.log 语句,然后在内容模板中,我用 console.log 写了另一个脚本,并在内容结束时模板,另一个console.log。在第一次加载页面时,所有三条消息都显示在日志中,但在部分回发时,它们都没有再次显示。意味着,没有一个脚本在部分 postbak 上工作。

标签: javascript jquery asp.net ajax


【解决方案1】:

最后我解决了这个问题。这是为您提供帮助的解决方案。 我刚刚在页面加载时编写了以下代码,并且效果很好。

protected void Page_Load(object sender, EventArgs e)
{
    if (((ScriptManager)this.Master.FindControl("ScriptManager1")).IsInAsyncPostBack)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<script language='javascript' type='text/javascript'>");
        sb.Append("Sys.Application.add_load(func);");
        sb.Append("function func() {");
        sb.Append("Sys.Application.remove_load(func);");
        sb.Append("var maxHeight = Math.max.apply(null, $('.producttile').map(function () { return $(this).height(); }).get()); $('.producttile').height(maxHeight);");
        sb.Append("maxHeight = Math.max.apply(null, $('.clistdiv').map(function () { return $(this).height(); }).get()); $('.clistdiv').height(maxHeight);");
        sb.Append("}");
        sb.Append("</script>");
        ScriptManager.RegisterStartupScript(this, GetType(), "script", sb.ToString(), false);
    }
}

感谢以下帮助我实施此解决方案的文章: The solution link.

请告诉我这种方法是否存在安全/性能漏洞?

【讨论】:

  • 请告诉我这种方法是否存在任何安全/性能漏洞?您可以创建新问题并单独提问,而不是在答案中。
猜你喜欢
  • 2015-12-09
  • 2015-12-24
  • 2012-09-03
  • 2016-07-19
  • 2020-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多