The class defined in the following example, SimpleControl, inherits the UserControl class and can be used as an ASP.NET code-behind class. It uses the TextBox, Label, and Button Web server controls and defines a myButton_Click method that assigns the TextBox..::.Text property value, along with two strings, to the Label..::.Text property.
Security Note: |
|---|
This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see
Script Exploits Overview.
|
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class SimpleControl
Inherits UserControl
Public name As TextBox
Public output As Label
Public myButton As Button
Public Sub myButton_Click(sender As Object, e As EventArgs)
output.Text = "Hello, " + name.Text + "."
End Sub
End Class
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
public class SimpleControl:UserControl
{
public TextBox name;
public Label output;
public Button myButton;
public void myButton_Click(object sender, EventArgs e)
{
output.Text = "Hello, " + name.Text + ".";
}
}
The following example is markup contained in an .ascx file. You can use the SimpleControl class defined in the previous example as a code-behind class for the markup in this .ascx file.
<%@ control inherits = "SimpleControl" src = "SimpleControl.vb" %>
<table style="background-color: yellow; font: 10pt verdana;border-width:1;border-style:solid;border-color:black;" cellspacing="15">
<tr>
<td><b>Enter your name here: </b></td>
<td><ASP:TextBox id="name" runat="server"/></td>
</tr>
<tr>
<td><b><ASP:Label id="output" runat="server"/></b></td>
</tr>
<tr>
<td></td>
<td><asp:button text="Submit" OnClick="myButton_Click" runat="server" /></td>
</tr>
</table>
<%@ control inherits = "SimpleControl" src = "SimpleControl.cs" %>
<table style="background-color:yellow;font: 10pt verdana;border-width:1;border-style:solid;border-color:black;" cellspacing="15">
<tr>
<td><b>Enter your name here: </b></td>
<td><ASP:TextBox id="name" runat="server"/></td>
</tr>
<tr>
<td><b><ASP:Label id="output" runat="server"/></b></td>
</tr>
<tr>
<td></td>
<td><asp:button id="myButton" text="Submit" OnClick="myButton_Click" runat="server" /></td>
</tr>
</table>