1

I have a js function that will be executed automatically page initialized. This js will be called and executed perfectly when first open the page, but when I postback (click the link on asp.net page) page, it will not be called and the function broken.

This is my js function in Default.aspx:

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

it only executed once first enter the Default.aspx, it doesn't trigger twice after I click the button on the page (postback).

Is this related to client cache? Is there a way(by setting some properties?) to prevent client cache when postback?

Thanks

JerryCai
  • 1,663
  • 4
  • 21
  • 36

1 Answers1

0

Be warned that code in <%= ... %> block executed only on full page reload. What do you need it's to update keywords array and re-call hHightLightKeywords function on each partial postback with ScriptManager control. Check code below:

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

Code-behind:

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);
}
Yuriy Rozhovetskiy
  • 22,270
  • 4
  • 37
  • 68