about understand asp.net and html server controller


T

Tony Johansson

Hello!

Below is the source of the Design window and at the bottom I have the
code-behind file.
I just test this HTML control so I can understand them. In the normal case I
should use webbserver control instead.
I just wonder is not possible to have two event handler for the same
control. In this case I have one event handler Button1_onclick which should
execute on the client and one event hander Button1_ServerClick1 which should
execute on the server for the same html Button ?

When I run this page and click on the Button no event handler is called.

Can somebody explain to me what is happening here ?

<%@ 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 language="javascript" type="text/javascript">
<!--

function Button1_onclick()
{
document.write('Hello');
}

// -->
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" runat="server" style="z-index: 100; left: 0px;
position: absolute; top: 0px"
type="button" value="button" onclick="return Button1_onclick()"
onserverclick="Button1_ServerClick1" />

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

In the code-behind file I have the following code
************************************
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 Button1_ServerClick1(object sender, EventArgs e)
{
Response.Write("**********Testing ***********");
}
}

//Tony
 
Ad

Advertisements

G

Gregory A. Beamer

When I run this page and click on the Button no event handler is called.

You need to set up the HTML control with runat=server to ever hope to have
any server side interactivity.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
T

Tony Johansson

As you can see I have added runat = "server"
Read my question again !

//Tony
 
G

Gregory A. Beamer

As you can see I have added runat = "server"
Read my question again !

Have you?

<input id="Button1" runat="server" style="z-index: 100; left:
0px;
position: absolute; top: 0px"
type="button" value="button" onclick="return Button1_onclick
()"
onserverclick="Button1_ServerClick1" />

This is where you need to add it, not in the head.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
T

Tony Johansson

I don't understand what you mean but as you can see I have runat="server" as
the second attribut in the input element below.
<input id="Button1" runat="server" style="z-index: 100; left: 0px; position:
absolute; top: 0px"
type="button" value="button" onclick="return Button1_onclick()"
onserverclick="Button1_ServerClick1" />

Can you specify exactly where I have to add runat="server"

//Tony
 
T

Tony Johansson

If I remove this attribute onclick="return Button1_onclick()" the event
handler on the server will work.
My question is why do I have to remove this attribute onclick="return
Button1_onclick()" to have the event handler on the server to wor.

//Tony
 
Ad

Advertisements

G

Gregory A. Beamer

I don't understand what you mea

I broke one of my cardinal rules of trying to show someone how to fix
their code rather than focus on the solution.

Check out this site:
http://tinyurl.com/ylq3gn8

It is my blog and shows two ways to solve the real problem (doing both
server and client side FUD). You probably want the client side primacy
version (second version), but perhaps not. regardless, there are two
solutions to having JavaScript fire and having the server side code run.
The client side primacy solution also allows for a confirmation button
to popup to stop the server side submit.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 

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

Top