{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
Using an AJAX Server Control from a web site
Summary
This article talks about using an AJAX Server Control from a web site. Registering the control and adding a ScriptManager are also discussed.
 
Table of Contents

Control and a Test Site

Figure 1. A test website for holding the AJAX Server Control

Manually Dropping the AJAX Server Control

Figure 2. AJAX Server Control in the tool box

Figure 3. AJAX Server Control in the tool box

Code for adding a server control

Code Listing 1. Adding a server control

Code Listing 2. Registering a Control

Code Listing 3. Instance of the AJAX Server Control

Figure 4. Absence of ScriptManager

Code Listing 4. Absence of ScriptManager

Code Listing 5. Adding ScriptManager to the Page

 

Control and a Test Site

In the following article the default code for an AJAX Server Control is explained:

First ASP.NET AJAX Server Control with C# Explained

Here a simple test web site is created to add the above AJAX Server Control to an ASP.NET web page. The default control itself is there in the project AjaxServerControl1. A simple site that tests that control is in ‘TestSite’.

Figure 1. A test website for holding the AJAX Server Control

Figure 1. A test website for holding the AJAX Server Control

Manually Dropping the AJAX Server Control

When you compile the solution, AjaxServerControl1 will generate a dll (AjaxServerControl1.dll). This server control should be automatically shown in the Toolbox under AjaxServerControl1 Components.

If not, going to the Design View of a page (say, Default.aspx) would update the Toolbox with the controls in the same solution. You can also add controls manually to the Toolbox.

Figure 2. AJAX Server Control in the tool box

Figure 2. AJAX Server Control in the tool box

Adding a control to page is, of course, just a matter of dragging and dropping the control on to the Design surface of the aspx page.

When you drag-drop a control on to a page, Visual Studio 2008 automatically creates a Bin directory in the site (if it is not already there) and then copies the assembly file (in this case AjaxServerControl1.dll; the pdb file, which contains the symbolic information is also copied to the Bin directory).

Figure 3. AJAX Server Control in the tool box

Figure 3. AJAX Server Control in the tool box

Now let’s take a look at the code added to the aspx page when the above control is dropped onto the Design surface of the aspx page.

Code for adding a server control

Code Listing 1. Adding a server control

<%@ Register assembly="AjaxServerControl1" namespace="AjaxServerControl1" tagprefix="cc1" %>



<body>



        <cc1:ServerControl1 ID="ServerControl11" runat="server" />


Adding an AJAX Server Control follows the same familiar pattern of adding any other custom control.

First you need to register the assembly (i.e. tell Visual Studio / .NET Framework where to find the control you are using). This @Register directive takes the assembly (in this case the code is in AjaxServerControl1 assembly – inside AjaxServerControl1.dll), namespace, and a tagprefix to be used later with the actual instance this AJAX Server Control.

Code Listing 2. Registering a Control

<%@ Register assembly="AjaxServerControl1" namespace="AjaxServerControl1" tagprefix="cc1" %>

Then using the tag prefix (cc1), an instance of server control is defined. Here, this server control doesn’t have any custom properties (other than the standard ID and runat).

Code Listing 3. Instance of the AJAX Server Control

        <cc1:ServerControl1 ID="ServerControl11" runat="server" />

If you compile and run the web site, you might get the following error.

Figure 4. Absence of ScriptManager

Figure 4. Absence of ScriptManager

The error is specific to ASP.NET AJAX components. They require ScriptManger on the page.

Code Listing 4. Absence of ScriptManager

The control with ID ’ServerControl11’ requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it.

The ScriptManager can simply be dragged and dropped from the Toolbox (it is under the AJAX Extensions section) to resolve the above error. It adds the following code:

Code Listing 5. Adding ScriptManager to the Page

        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>

Take a Quick Quiz on this Article

1. The third-party custom control / server control you intend to use in your web site is available as a dll. Where would you typically place this dll file?



: It’s for the binaries
Question 1 of 5
Bookmark and Share This

More Articles With Similar Tags
Google Suggest style autocomplete feature is readily available from the AutoCompleteExtender control from the AJAX Toolkit. This article shows how to use control with the data obtained from database.
icon-ajaxservercontrol-new-project.jpg
This article explains the various pieces of the code generated for beginning an AJAX Server Control. The class ScriptControl and the over-ridden methods GetScriptDescriptors() and GetScriptReferences() are explained, among others.
This test has questions about using an AJAX Server Control from a web site. Registering the control and adding a ScriptManager are also included.
This is a test on the various pieces of the code generated for an AJAX Server Control. This includes the class ScriptControl and the over-ridden methods GetScriptDescriptors() and GetScriptReferences().
icon-ajaxservercontrol-client-files.jpg
With an AJAX Server control, some additional JavaScript is generated behind the scenes (in addition to the JS you write). This article looks at this generated JavaScript when an AJAX Server Control is used.
About  Contact  Privacy Policy  Site Map