1

I want to create a script like this an run that :(below is attemp)

protected void txtPassword_TextChanged(object sender, EventArgs e)
{
        script  =  "<script type='text/javascript'>";
        script += "function showmsg(){";
        script += "$('#msg').slideDown('normal');";
        script += "$('#msg span').addClass('message');";
        script += "setTimeout('showmsg',100);";
        script += "}";
        script += "</script>";
        ClientScript.RegisterClientScriptBlock(this.GetType(), "ShowMessage",script);
}

Where is my mistake ? why there is no run ?

sap
  • 87
  • 1
  • 11

5 Answers5

0

seems you forgot to say that your script is a string, it shoud work this way.

 protected void txtPassword_TextChanged(object sender, EventArgs e)
    {
            string script  =  "<script type='text/javascript'>";
            script += "function showmsg(){";
            script += "$('#msg').slideDown('normal');";
            script += "$('#msg span').addClass('message');";
            script += "setTimeout('showmsg',100);";
            script += "}";
            script += "</script>";
            ClientScript.RegisterClientScriptBlock(this.GetType(), "ShowMessage",script);
    }
0

Presuming the script is now a part of the page, the function showmsg() needs to be called:

<script type="text/javascript">
$(function() {
    showmsg();
    });
</script>

since you are using jQuery, this should execute the code in the showmsg function.

DaveB
  • 9,470
  • 4
  • 39
  • 66
  • 1
    why registering code , runs in a load or click event but not in text_changed ???? – sap Mar 11 '12 at 18:28
0

There is no need to register the script this way in code. You can simply check for the change in text of the textbox using javascript/jquery.

Since you are using jquery, the change() event does not work as in standart JavaScript when detecting change in textbox text change.

Have a look at this link where something similar was already answered.

https://stackoverflow.com/a/1481155/448407

Hope this helps.

Community
  • 1
  • 1
Dinesh
  • 3,652
  • 2
  • 25
  • 35
0

You don't need to register the script. Just do this to handle the change event on the client side:

$(document).ready(function() {
  $('#<%= txtPassword.ClientID %>').change(function() {
     alert('Handler for .change() called.');
  });
});

You use the control's ClientID property to find it with jQuery and subscribe to change event.

Hope it helps!

Reinaldo
  • 4,556
  • 3
  • 24
  • 24
0

It seem that you are using some jquery markup to do apply some animation, remember that with jquery you must load the Document before manipulate the DOM.

$(document).ready(function() {

//your script

});

Nudier Mena
  • 3,254
  • 2
  • 22
  • 22