jqueryasp.net-2.0postback

jquery script not working after postback even with pageLoad


I've got some trouble with jQuery script on my Asp.net page. During first load it works well, after postback it does not. Initially I thought that it is because script is not load again, but alert told me that script runs as I use function pageLoad() instead of $(document).ready(function(). In script I just read value from one text area and set the length value to next text input. I am putting script into page like this:

<script type="text/javascript" src="../js/smsanywhere.js"></script>  

I tried to place directly into page, but there is no difference I guess.

The script is shown below.

    function pageLoad()
    {
       alert('pageLoad is there');
       var textBox  = "textarea[id*='txtMessage']";
       var counterBox  = "input[id*='txtCharLeft']";

       $(textBox).keydown(function(){ 
          alert(this);
          var length = 70 - $(this).val().length;
          $(counterBox).val(length);
          $(counterBox).attr("style",function(){
             return "width:30px;" + (length < 0 ? "background-color:Red;" : "");
         });
       });
    }

I don't use any ajax at this page, its just master page , content page and jQuery.

What am I missing?


Solution

  • I found the problem....as I add reference to jquery like this:

    HtmlGenericControl myJs = new HtmlGenericControl();
    myJs.TagName = "script";
    myJs.Attributes.Add("type", "text/javascript");
    myJs.Attributes.Add("language", "javascript"); //don't need it usually but for cross browser.
    myJs.Attributes.Add("src", ResolveUrl("~/js/jquery/jquery-1.2.6.min.js"));
    Page.Header.Controls.Add(myJs);
    

    But I placed inside if (!IsPostBack){ ... } so I was ....solution is only to remove it from this condition and run it each time...uff.

    So mystery sorted out...