Wednesday, September 26, 2007

TextBox TextChanged client side

The TextChanged event of a TextBox fires when doing a postback in ASP.NET. So when you type something in the textbox, it won't fire before you change the focus or do something else that causes a postback to the server.

To solve this, I found a piece of code that adds a Javascript to the HTML control's OnKeyUp event. This Javascript causes a PostBack, so that your event fires each time you type something in your textbox.

Code:

string js = "javascript:" + ClientScript.GetPostBackEventReference(TextBox1, "@@@@@buttonPostBack") + ";" ;
TextBox1.Attributes.Add("onkeyup", js);

3 comments:

J.H. said...

The line

string js = "javascript:" + ClientScript.GetPostBackEventReference(TextBox1, "@@@@@buttonPostBack") + ";" ;

didn't work for me. However, when I turned ClientScript into Page.ClientScript, it worked fine.

Just putting this here to save others a lot of searching the MSDN library ;)

joc Cby said...

forgive the code works fine but when I refresh the page clears the form page and I need to clean out the form, so I was wondering if you had a way to avoid this inconvenience

PDF signature said...

Grateful to check out your website, I seem to be ahead to more excellent sites and I wish that you wrote more informative post for us. Well done work.