Microsoft KB Archive/318814

= HOW TO: Associate Client-Side Events with Server-Side Controls in ASP.NET by Using Visual Basic .NET =

Article ID: 318814

Article Last Modified on 8/28/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q318814



For a Microsoft Visual C# .NET version of this article, see 318100.

IN THIS TASK
SUMMARY
 * Requirements
 * Create an ASP.NET Web Application by Using Visual Basic .NET
 * Description of the WebControl.Attributes Property
 * Example That Uses the Attributes Property
 * Verify That It Works
 * Troubleshooting

REFERENCES



SUMMARY
This step-by-step article demonstrates how to programmatically associate client-side events with server-side Web Forms controls in ASP.NET.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Microsoft Windows XP Professional, Microsoft Windows 2000 Professional, Windows 2000 Server, or Windows 2000 Advanced Server
 * Microsoft Visual Studio .NET
 * Microsoft .NET Framework
 * Microsoft Internet Information Services (IIS)

back to the top

Create an ASP.NET Web Application by Using Visual Basic .NET
These steps demonstrate how to create a new ASP.NET Web application that is named ClientEventDemo:
 * 1) Start Microsoft Visual Studio .NET.
 * 2) On the File menu, point to New, and then click Project.
 * 3) In the New Project dialog box, click Visual Basic Projects under Project Types, and then click ASP.NET Web Application under Templates.
 * 4) In the Location box, type ClientEventDemo to replace the default   name in the URL path. If you are using the local server, you can leave the server name as &quot;http://localhost.&quot; This results in an &quot;http://localhost/ClientEventDemo&quot; entry.

back to the top

Description of the WebControl.Attributes Property
The WebControl.Attributes property is implemented as a System.Web.UI.AttributeCollection class of name and value pairs. The Attributes collection contains a collection of all of the attributes that are declared in the opening tag of a Web server control. You can use this to programmatically control the attributes that are associated with a Web server control. You can add attributes to the collection or remove attributes from the collection. In the example that is described in this article, you add an attribute that is to be associated with a Web Forms TextBox control. Specifically, the name and value pair is for an entry named &quot;onblur.&quot; The JavaScript &quot;alert&quot; code serves as the value.

Attributes that are stored in this collection for a Web server control do not correspond to the strongly-typed properties that are located on the specified Web server control. To better demonstrate this concept, another attribute is added for the Web Forms TextBox control in the sample code to set the background color. Notice that the sample code does not use the BackColor property of the Web Forms TextBox control. Instead, the sample code uses the HTML style attribute to set the background color. The Attributes property is rendered with all of the attributes in the collection in the opening tag for the control, no matter what the browser settings are. Not all browsers support every attribute that is rendered.

back to the top

Example That Uses the Attributes Property
This example demonstrates how to use the Attributes property to specify a client-side event for a Web Forms TextBox control:  Follow these steps to add a new Web Form named ClientEventSample.aspx to the project:  In Solution Explorer, right-click the Project node, click Add, and then click Add Web Form. In the Name box, type ClientEventSample.aspx, and then click Open.  On the Design tab, drag a Web Forms TextBox control to the page, and then change the value of the ID property to MyTextBox in the Properties window. Right-click the page, and then click View Code. This opens the code-behind class file in the editor.  Add the following code to the Page_Load event in the code-behind class file: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MyTextBox.Attributes(&quot;onblur&quot;) = &quot;javascript:alert('Focus lost from Web Forms MyTextBox control!!');&quot; MyTextBox.Attributes(&quot;style&quot;) = &quot;BACKGROUND-COLOR: #ccffff;&quot; End Sub </li> On the File menu, click Save All to save the Web form and other associated project files.</li> On the Build menu in the Visual Studio .NET integrated development environment, click Build Solution.</li></ol>

back to the top

Verify That It Works

 * 1) In Solution Explorer, right-click the ClientEventSample.aspx file, and then click View in Browser.
 * 2) With the page open in the browser, click the MyTextBox control to make it active, and then press the TAB key. You see a JavaScript message box that displays a confirmation that the onblur event has been triggered because the Web Forms control lost focus. Also, note that the background color for the MyTextBox control is set to blue.

back to the top

Troubleshooting
The Attributes property is rendered with all of the attributes in the collection in the opening tag for the control, no matter what the browser settings are. Not all browsers support every attribute that is rendered.

back to the top

<div class="references_section">