The ASP.NET User Control
User controls generally are created when there is a need for reusable content. A corporate logo is an item that could conceivably be used numerous times throughout the course of an application. Additionally, if the need arises to use a control across applications, the user control's sibling, the custom control, may be a more appropriate alternative. Reusability is recursively responsible.
The ASP.NET markup code for the corporate logo user control is defined below. In this specific instance, the code-behind has been intentionally omitted. However, that is because there is no procedural code to mention. At the end of this article, you will find a zip file containing the source code for this example. Please examine the code for yourself.
<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="CorporateLogo.ascx.cs"
Inherits="SilverlightUserControl.CorporateLogo" %<
<div id=<%= "\"silverlightHost" + this.ID + "\"" %>>
<script type="text/javascript" src="js/CreateSilverlight.js">
</script>
<script type="text/javascript" src="js/Silverlight.js"></script>
<script type="text/javascript">
var htmlElement =
document.getElementById(<%= "\"silverlightHost" +
this.ID + "\"" %>);
Sys.Silverlight.createObject
(
"CorporateLogo.xaml",
htmlElement,
<%= "\"silverlight" + this.ID + "\"" %>,
{
width:
height:
inplaceInstallPrompt:false,
background:
isWindowless:
framerate:
version:
},
{
onLoad:null
},
null
);
</script>
</div>
The key item in this code snippet is the utilization of the value of the user control's "ID" property. The "ID" property of the user control is utilized in:
- Assigning the "id" attribute of the hosting DIV element
- Referencing the hosting DIV element within the instantiation of the Silverlight control
- Defining the ID of the Silverlight control
This is important because it ensures unique references of the Silverlight content. To illustrate why this is needed, envision creating a Web page that requires the use of the corporate logo multiple times. Without the unique names, there would be no way to distinguish between the various instances of the logo.
Conclusion
At this point, a very basic user control that utilizes Silverlight has been constructed. This user control demonstrated how to integrate Silverlight content with a Web application. It also demonstrated how one can leverage an existing skillset to harness the power of a new technology.
Silverlight provides new avenues for building lasting connections with users. It's important to grow these connections with minimal effort. With Silverlight, it's easy to strengthen your brand leveraging your existing Web applications.
* This article originally appeared on CodeGuru.