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:
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.
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
Example 6-13: BlogClient_VS.aspx
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
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.
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
- 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 http://www.prosetech.com/.