posted on Saturday, August 28, 2004 3:16 PM
by
leon
Execute javascript and perform postback from the same ASP.NET button
Another common question:
How can we execute both client script function and server side event from the same ASP.NET Button server control?
Button server control rendered as <input type=“submit”> html element. It submits the form and executes server side Click event handler on each click, mousedown or “Enter” pressed on form. Sometimes button controls render javascript in onlick event to perform validation tasks (if validator controls were droped on page). Knowing this, we don't want to interfere with validation or forms submit. The simplest way to execute client script before submit, is to attach scriptto mousedown event. You have to initiate button click after script execution to continue with submit:
<
script language=
“javascript“>
function doSomething()
{
// Do some thing usefull ... // Continue with submit this.click();
}
</
script>
<
asp:button onmousedown=
“doSomething()“ id=
“Button1” Text=
"Button" runat=
"server"></
asp:button>
This button will execute client side doSomething function and proceed to server to fire Button1_Click event.