oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

User Controls and Custom Server Controls, Part 3
Pages: 1, 2, 3, 4, 5

To allow Visual Studio .NET to read your custom schema, you'll need to add an xmlns attribute to the <body> tag of the page in which you wish to use the schema, as shown in the following snippet:

<body xmlns:aspnetian="urn:">

Notice that this code uses the aspnetian prefix with the xmlns attribute to specify that the schema is for controls prefixed with the aspnetian tag prefix. This recall is set up by the TagPrefix attribute (described in the previous section on "Metadata attributes"). The value of the xmlns attribute should be the same as the targetNamespace attribute defined at the root of the schema.

Once you've wired up your schema via the xmlns attribute, you should be able to type an opening < character and have the Blog control appear as one of the options for statement completion, as shown in Figure 6-5.

Figure 6-5. Statement completion in HTML view


Example 6-13 shows the code for a page that uses the Blog control from Visual Studio .NET, including the xmlns attribute added to the <body> element.

Example 6-13: BlogClient_VS.aspx

<%@ Register TagPrefix="aspnetian" Namespace="aspnetian" 
   Assembly="Blog" %>
<%@ Page Language="vb" AutoEventWireup="True" Debug="True"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
      <title>Blog Client</title>
      <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
      <meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
      <meta content="JavaScript" name="vs_defaultClientScript">
      <meta content="" 
      <script runat="server">
         Sub Page_Load(  )
            If Request.QueryString("mode") = "add" Then
               Blog1.Mode = BlogMode.Add
               Link1.Visible = False
               Link2.Visible = False
               Blog1.Mode = BlogMode.Display
               Link1.Visible = True
               Link2.Visible = True
            End If
         End Sub
   <body xmlns:aspnetian="urn:">
      <form id="Form1" method="post" runat="server">
         <p><asp:hyperlink id="Link1" runat="server" 
               Add Blog
         <p><aspnetian:blog id="Blog1" addredirect="WebForm1.aspx" 
               email="" runat="server" >
         <p><asp:hyperlink id="Link2" runat="server" 
               Add Blog

Notice that Example 6-13 provides support for both displaying and adding blog entries from within the same page; this is done by omitting the Mode property in the tag that defines the control and setting the Mode programmatically (based on whether or not the page request was the result of the user clicking one of the "Add Blog" Hyperlink controls added to the page).

In This Series

User Controls and Custom Server Controls, Part 4
In this final installment in a series of excerpts from ASP.NET in a Nutshell, learn how to share ASP.NET controls across applications.

User Controls and Custom Server Controls, Part 2
In Part 2 in this series of excerpts from ASP.NET in a Nutshell, learn ASP.NET custom server controls.

User Controls and Custom Server Controls, Part 1
In part one from this series of book excerpts from ASP.NET in a Nutshell, get an overview on ASP.NET controls, and learn about ASP.NET user controls.

When the page is loaded for the first time, it will be in Display mode. Clicking one of the hyperlinks will request the page with the mode QueryString element set to add, which will cause the page to render in Add mode.

Adding Client Script

Sometimes you may want to use client-side script in your ASP.NET pages, either with controls or independent of them. In classic ASP, it was possible to write client script to the browser using Response.Write. However, this could get very messy-- particularly if you needed to write the same set of code for use with more than one form element.

The ASP.NET Page class provides several methods for sending client script to the browser that make this process simpler and more reliable.

These methods include:

Renders a string containing the specified client script to the browser.

Adds an <input> element whose type is set to hidden.

Allows you to test whether a given named script block has been already registered by another control to avoid redundancy.

You might use these methods to pop up a message box on the client with the number of Blogs that currently exist in the XML file. To accomplish this, add the following snippet to the DisplayBlogs method of the Blog control:

Page.RegisterClientScriptBlock("Blog", "<script>alert('There are now " +
   BlogRows.Count + " Blogs!');</script>");

Then, if any other controls need to use the same script, call IsClientScriptBlockRegistered, passing it the name of the script shown above, Blog, to determine whether to call RegisterClientScriptBlock again. In this way, a single client-side script block may be shared among multiple controls.

In the final installment, learn about sharing controls across applications


Matthew MacDonald is a developer, author, and educator in all things Visual Basic and .NET. He's worked with Visual Basic and ASP since their initial versions, and written over a dozen books on the subject, including The Book of VB .NET (No Starch Press) and Visual Basic 2005: A Developer's Notebook (O'Reilly). His web site is

G. Andrew Duthie is the founder and principal of Graymad Enterprises, Inc., which provides training and consulting in Microsoft Web development technologies.

Return to the .NET DevCenter