Push my button

Let's see how to use a button in an ASP page.

We'll have a text box (aka input line) where entering a string; and a button to push when done with it. A function would be then called, that would read the string we input and write it in a label.

Being such a simple code, we will put all directly in the ASP page.

I'm using an asp:TextBox named myTextBox; an asp:Button, myButton, with the associated text "OK" and the OnClick even handler associated to a method named click; and finally an asp:Label named myLabel.

The click() method would simply say that the text in myTextBox should be assigned to the text of myMessage.

Here is the resulting ASP page:

<%@ Page Language="C#"%>

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

<script runat="server">
void click(Object s, EventArgs e)
{
myMessage.Text = myTextBox.Text;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Push me</title>
</head>
<body>
<form id="myForm" runat="server">
<div>
<asp:TextBox id="myTextBox" runat="server" />
<asp:Button id="myButton" text="OK" runat="server" onClick="click" />
<hr />
Your input message is: <asp:Label id="myMessage" runat="server" />
</div>
</form>
</body>
</html>

No comments:

Post a Comment