1

我有一个 js 函数,它将在页面初始化时自动执行。这个js在第一次打开页面时会被完美调用执行,但是当我回发(点击asp.net页面上的链接)页面时,它不会被调用并且功能被破坏。

这是我在 Default.aspx 中的 js 函数:

<script type="text/javascript">
        function HightLightKeywords() {
            var container = document.getElementById("result");
            var keywords = new Array();
            <%  
                // This is C# code runs in server side.
                for (int i = 0; i < keywords.Count; i++)
                {
                    Response.Write(string.Format("keywords['{0}'] = '{1}';", i, keywords[i]));
                }
            %>
            for (var i = 0; i < keywords.length; i++)
            {
                var a = new RegExp(keywords[i], "igm");
                container.innerHTML = container.innerHTML.replace(a, "<span style='background:#FF0;'>" + keywords[i] + "</span>");
            }
        }
        HightLightKeywords();
    </script>

它只在第一次进入 Default.aspx 时执行一次,在我单击页面上的按钮后它不会触发两次(回发)。

这与客户端缓存有关吗?有没有办法(通过设置一些属性?)在回发时防止客户端缓存?

谢谢

4

1 回答 1

0

请注意,<%= ... %>块中的代码仅在整页重新加载时执行。你需要的是更新数组并在每个部分回发时keywords重新调用函数并控制。检查以下代码:hHightLightKeywordsScriptManager

 <script type="text/javascript">
      function hHightLightKeywords() {
           var container = document.getElementById("result");
           for (var i = 0; i < keywords.length; i++) {
                var a = new RegExp(keywords[i], "igm");
                container.innerHTML = container.innerHTML.replace(a, "<span style='background:#FF0;'>" + keywords[i] + "</span>");
           }
      }
 </script>

 <asp:UpdatePanel runat="server">
      <ContentTemplate>
           <asp:TextBox runat="server" ID="KeywordsTextBox" Text="cras turpis et" />
           <asp:Button runat="server" Text="Click Me" />
      </ContentTemplate>
 </asp:UpdatePanel>
 <p id="result">
      Et ac tincidunt nisi pid vel? Nunc turpis risus duis porta cursus cum augue, nisi,
      scelerisque turpis aliquam. Scelerisque tristique pid, elementum augue sociis augue
      cras mauris lundium nisi rhoncus diam cras parturient nec, velit odio, et turpis
      risus porttitor aliquam facilisis, ut duis augue porta magnis velit velit, augue
      vut sed facilisis dignissim, arcu, cras! Ultrices adipiscing? Est eros ac augue
      turpis integer mus massa! Est nisi auctor sagittis augue sit? Tortor tristique elementum,
      adipiscing. Nec platea ac natoque facilisis, porta tincidunt magna lorem augue tincidunt
      turpis, scelerisque adipiscing elementum scelerisque. Risus'vel ac augue lorem odio!
      Cursus ultrices et elementum egestas dapibus proin arcu velit augue?
 </p>

代码隐藏:

protected IEnumerable<string> Keywords
{
    get
    {
        return KeywordsTextBox.Text.Split(' ').Where(kw => !string.IsNullOrEmpty(kw)).Select(kw => "'" + kw.Trim().Replace("'", @"\'") + "'");
    }
}

protected void Page_PreRender(object sender, EventArgs e)
{
    ScriptManager.RegisterArrayDeclaration(this, "keywords", string.Join(",", Keywords));
    ScriptManager.RegisterStartupScript(this, this.GetType(), "hHightLightKeywords", "hHightLightKeywords()", true);
}
于 2013-03-11T12:36:15.783 回答