I'm not sure what you're trying to do.
Right now you are handling the .NET "TextChanged" event.
When the text in the TextBox (t1) is changed a postback to the server occurs.
Then your .NET code is executed.
You have a bunch of JavaScript stuff going on here that I can't understand.
JavaScript is run in the web browser (client side).
It is probably best that you use JavaScript to handle the TextChanged event in the browser instead of posting back to the server....so you have the right idea, I just don't know what you're doing.
Another thing that is not a good idea is use the Response.Write() method in your C# code. The reason is because when you use this method in your C# code it is very likely that it will be placed somewhere invalid (like outside of the <html> tag).
For example, if I had the following C# code:
-
Response.Write("<script type = 'text/javascript'>");
-
Response.Write(" alert('Hello World!');");
-
Response.Write("</script>");
The resulting HTML would be something like:
-
<script type='text/javascript'>
-
alert('Hello World!');
-
</script>
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
......
-
Notice that the script is before the <html> tag?
This is makes the web page invalid and some browsers wont even run the code, other browsers will try to run the code but it may not run as expected.
I recommend putting the JavaScript into the ASPX page itself, or placing it into an external JavaScript file.
If you place the JavaScript in the ASPX page itself then you can use ASP to tell the Response.Write method where the content should be placed.
For example, the following JavaScript code is located with my Default.aspx page:
-
<script type="text/javascript">
-
function onTextBoxBlurEventHandler(){
-
var textBoxElement = document.getElementById('<% Response.Write(t1.ClientID); %>');
-
if(textBoxElement && textBoxElement.value){
-
alert("The length is: "+textBoxElement.value.length());
-
}
-
}</script>
Notice how I used the Response.Write method?
I told it to write the ClientID of the t1 TextBox into the getElementById JavaScript method... Now the JavaScript is placed in the right place on the page and everything is valid.
This script doesn't even really need to use the getElementById JavaScript method...and it doesn't require the use of the Response.Write method either.
I would modify the JavaScript function so that it took 1 parameter: the TextBox element.
-
<script type="text/javascript">
-
function onTextBoxBlurEventHandler(textBoxElement){
-
if(textBoxElement && textBoxElement.value){
-
alert("The length is: "+textBoxElement.value.length());
-
}
-
}</script>
I would remove the "TextChanged" event from the t1 TextBox and in the page load event I would put:
- t1.Attributes.Add("onblur","onTextBoxBlurEventHandler(this);');
Happy coding,
-Frinny