Sometimes you might need to expose a property in a User control (.ascx file). In this way, user controls can be more than static documents – they can have flexibility. Save the following (two files) and see how to access an assigned property through your main ASPX document.
Granted, this is no more than a text property and the implementation here could be handled easily in a label. However, this simple implementation shows a little of the power that can be had when using user controls. You’ll notice there’s a default string for the Whatever property in the user control, along with the Whatever property being explicitly used in the ASPX file. If you removed the explicity Whatever property in the ASPX file, the default string assigned to the Whatever property would be used.
ASCX File (save for this example, as usercontrol.ascx):
<script language=”VB” runat=”server”>
Public Whatever as String=”This is the Default String”
</script>
<hr>
<% =Whatever %>
<hr>
ASPX File:
<%@ Register TagPrefix="xprs" tagname="String" src="usercontrol.ascx" %>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 2.0">
<title>Untitled</title><script language="VB" runat="server">
Sub Button1_click(Source as Object, E as EventArgs)
xprsString.Whatever=text1.text
End Sub
</script></head>
<body>
<Form id="MyForm" runat="server">
<asp:TextBox id="text1" runat="server" /><br>
<asp:Button id="button1" Text="Enter Text Here" onclick="Button1_click" runat="server" />
</Form>
<div align="center"><b><xprs:String id="xprsString" runat="server" /></b> </div></body>
</html>