Why is not this simple example working


Tony Johansson


I just trying to learn how asp.net is handling html server control.
Normally I have used webbserver control here.
In this example I have dragged a html input button into the page see below.
I have then created an event handling method on the client named
Then I added runat="server" making the ASP.NET to look for control if it
any server-side processing.
As the last thing I added an event handler for the html server control in
the code-behind file.

Now to my question when I now run the page and hit the button
no event handler is called. But two event handler should have been called
the one on the client which is called Button1_onclick and the one on the
which is called Button1_ServerClick.
So is it not possible do do what I have done here ?

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">

function Button1_onclick()
document.write('Now event handler on client is called');

// -->
<form id="form1" runat="server">
<input id="Button1" runat="server" style="z-index: 102; left: 116px;
position: absolute; top: 368px"
type="button" value="button" language="javascript"
onclick="return Button1_onclick()" onserverclick="Button1_ServerClick" />


Code-behind file
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)

protected void Button1_ServerClick(object sender, EventArgs e)
Response.Write("Now event handler on server is called");




bruce barker

<input type="button"> does not do a postback, its for client javascript
(no need to cancel postbacks). you probably want type="submit".

-- bruce (sqlwork.com)

Tony Johansson

But if it's for client javascript why is not
the event handler on the client called.



Mark Rae said:
Haven't you already asked this question...? Anyway, make the following
changes and all will be well...

Remove the language="javascript" tag, as that has been deprecated for more
than fourteen years.

Change that to: alert('Hello from the client');
Not absolutely necessary, but try to avoid writing directly to the page in
I believe that is for more than just .net.
Apparently some antivirus software will block document.write calls.



Tony Johansson

Yes it worked if I follow you instructions. Both the client event handler
and the server event handler was called.
First the client and then the server.

But if I for example used this message document.write('Now event handler on
client is called');
in the client event handler insted of this message alert('Hello from the
then the server event handler was not called for some reason.

Can you give a explanation why the server event handler was not called if I
just used
this message document.write('Now event handler on client is called'); insted
this message alert('Hello from the client'); ??


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question