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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: