Code behind example

This example shows you how to use the code behind mechanism. It is the same program than the Event click example simply using the code behind mechanism.

Here we have an ASP page written in C# that references some code behind written in Eiffel. The code behind display or erase the message "Hello world!" in the label `my_lbl' when the button is clicked.

Note: Within an ASP page written in Eiffel, it is not possible to write any code behind also written in Eiffel.

 

C# ASP. NET page using Eiffel code behind:

<%@ Page Language="C#" inherits="BUTTON_EVENT" Src="button_event_code\button_event.e" %>
<html>
    <body>
	<form runat=server>
	    <asp:Button id=Button1 text="Click me" OnClick="button_click"; runat=server />
	    <br>
	    <asp:Label runat=server text="" id=my_lbl />
	</form runat=server>
    </body>
<html>>

 

Eiffel code behind class:

There are some rules you must follow in order to write a code behind class:

indexing
	description: "Code behind class of page code_behind.aspx"

class
	BUTTON_EVENT

inherit
	WEB_PAGE
		undefine
			finalize,
			get_hash_code,
			equals,
			to_string
		end
	ANY

feature -- Access

	my_lbl: WEB_LABEL
			-- Label to display message when button is clicked.

feature -- Basic Operation

	button_click (source: SYSTEM_OBJECT; e: EVENT_ARGS) is
			-- Action performed on button click
		do
			if my_lbl.text.length = 0 then
				my_lbl.set_text (("Hello world!").to_cil)
			else
				my_lbl.set_text (("").to_cil)
			end
		end

end -- Class BUTTON_EVENT

 

Here is the output after one click on the button:

 

 

[run the example]