Mistake in MSDN

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I think I have found a mistake in MSDN.

I have visual Studio 2003 with MSDN. My problem is very simple: I have a
webform with a HTML button control as:

<INPUT id="Button1" type="button" value="Button" name="Button1"
runat="server" onclick="alert('df');">

So its an HTMLServer control which does not submit.

I have this code for the button in code behind:
private void Button1_ServerClick(object sender, System.EventArgs e)
{
int h=0;
h++;
}


Now according to MSDN: (Section: ASP.NET Server Control Event
Model->Handling a Click Event in Client and Server Code)

***********************************************************************
All other HTML controls (those that do not submit a form by default)

Include an onclick attribute in the HTML syntax for the control, but follow
it with a semicolon (;):

<INPUT Type="Button" Runat="Server" Value="caption"
onclick="clientfunction();" ...>

This causes your function to be called first, before the client-side submit
script is called.

*************************************************************************

Now in my case, its always the client side script which calls first, no
matter what I do, even after using the semi-colon.The control still submits,
but first

the Javascript fires. So how can we make sure that our server side event is
executed first and than the client side script

runs (without using RegisterScriptBlock etc).

Is the above example a mistake in MSDN??

Vivek Thakur
www.vivekthakur.com
 
Hey Vivek,

This is not an error in the documentation. I think you have just
misunderstood what it says. What it means is that first it will call your
own client-side javascript function and then it will call a javascript
function generated by ASP.NET which will submit the form.

You can not make it work the other way around unless you want to use
RegisterClientSideScript or some other similar mechanism because of the way
HTTP works.

HTH, Jakob.
 
Hi,

There are a number of free chapters of .NET related books @ ondotnet.com and
wrox.com. Try using those too.

HTH,
Rakesh Rajan
 
You can't make what you do.

Everything happens first on the client, then on the server.
When it's on the server it's too late for any client action whatsoever!
 
Unless you want to control the wiring, your client side event will always be
called prior to the submit. It appears MS has an unfortunate choice of words
rather than an error.

If you want a client side event to fire after a submit, you will have to
emit the script to the page in the button click event. Within the ASP.NET
model, you will RegisterClientSideScript to emit the JavaScript you wish run.
If you wish to roll your own, slap a panel on the page and use a
LiteralControl to output the JavaScript you want run. The later method only
works if you wish the JavaScript run after some rendering of the page. If
before, use the former method (RegisterClientSideScript).

---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
Back
Top