why is this statement preventing an event handler to be called on the server

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

I have a html control of type submit which has runat=server making it a html
server control.
I have an event handler on the client and on the server for the same html
server control as you can see below.

It works perfect if I use the code below.
Now to my question if I just replace the statement in the client event
handler with
this statement document.write('Now event handler on client is called');
insted of
this statement alert('hello from the client'); then
the event handler on the server will not be called.

Can somebody explain why ?


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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


function Submit1_onclick()
{
alert('hello from the client');
}

// -->
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
&nbsp;&nbsp;
<input id="Submit1" runat="server" style="z-index: 103; left: 0px;
position: absolute; top: 0px"
type="submit" value="submit" onclick="return Submit1_onclick();"
onserverclick="Submit1_ServerClick" />

</div>
</form>
</body>
</html>

//Here is the 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 Submit1_ServerClick(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "test", "alert('Hello from
the server');", true);
}
}

//Tony
 
Tony said:
I have a html control of type submit which has runat=server making it a html
server control.
I have an event handler on the client and on the server for the same html
server control as you can see below.

It works perfect if I use the code below.
Now to my question if I just replace the statement in the client event
handler with
this statement document.write('Now event handler on client is called');
insted of
this statement alert('hello from the client'); then
the event handler on the server will not be called.

Can somebody explain why ?

Your are using client-side Javascript and if you call document.write
after the document has been loaded then an implicit document.open() is
done first meaning your document.write overwrites the actual document
and that way there no longer is any form with any input control to
submit it.
 
Back
Top