views:

1985

answers:

2

Hi all, I've got some trouble with JQuery script on my Asp.net page. During first load it works well, after postback it does not. Initiali 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 textarea and set the lenght 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 dont use any ajax at this page, its just master page , content page and jquery.

What am I missing...

+2  A: 

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

Xabatcha
A: 

Hi...where did u place this code? Master page or content page?

Sundeep Arun
I placed it into master page.
Xabatcha