Tuesday, July 11, 2006

ASP.NET – controlling PostBack through JavaScript

this.TextBox1.Attributes.Add("onchange", "if (doSomeThing()) ");

Note the if & extra parenthesis around the client function.

When a webcontrol with AutoPostBack=True is rendered, the ASP.NET insert a javascript function as __doPostBack(,) in the page and bind this function to client events corresponds to the server side bound events.

e.g. the HTML on the client side looks like:

<input name="TextBox2" type="text">onchange="__doPostBack('TextBox2','')" />

By adding the if (doSomeThing()) , it becomes

<input name="TextBox2" type="text">onchange="if (doSomeThing()) __doPostBack('TextBox2','')" />

The doSomeThing() function must evaluate to bool.

No comments: