Microsoft KB Archive/318101

= PRB: HTML User Control (.ascx File) May Not Work as Expected in Netscape =

Article ID: 318101

Article Last Modified on 2/19/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition

-



This article was previously published under Q318101



SYMPTOMS
The Hypertext Markup Language (HTML) that Visual Studio .NET generates for user controls on Web forms may fail to interact as expected with JavaScript when you use Netscape to render the control. You may receive the following error message in Netscape:

Element has no content

The problem is not specific to user controls (.ascx files). This problem occurs with any naming container. For example, you may experience the same behavior in a page that uses a Repeater control with a TextBox in the Repeater item.



CAUSE
This problem occurs because Netscape uses the name instead of the ID to identify form input elements in its document object model (DOM) representation. Because ASP.NET uses a colon to name form input elements in user controls on a Web control on a page, you cannot use certain JavaScript access syntaxes that retrieve data from form fields through the DOM.



RESOLUTION
To resolve this problem, edit the script to use the following syntax: document.formName[&quot;UserControlID:TextBox1&quot;].value After you change the script to use this syntax, the script works as expected on both Microsoft Internet Explorer and Netscape.



Steps to Reproduce the Behavior
 Create a user control (.ascx) that contains at least one text input control. Declare the control (in its code-behind class file) to implement the INamingContainer interface. Create an .aspx page, and then drag an instance of the user control onto that page. In the containing page, include some JavaScript that tries to access values of the  tag. This code fails with the error message that is listed in the &quot;Symptoms&quot; section.  Render the resulting .aspx page in Netscape 4.73, and then review the HTML code. Notice that the ASP.NET naming convention for controls appears as follows: INPUT ID=&quot;UserControlID:ControlID&quot; NAME=&quot;UserControlID:ControlID&quot; </li></ol>

If you change the targetSchema property of both the user control and the .aspx page to Internet Explorer 3.02/Navigator 3, the control is persisted as: input name=&quot;UserControlID:TextBox1&quot; type=&quot;text&quot; id=&quot;UserControlID_TextBox1&quot; You always receive the &quot;Element has no content&quot; error if all of the following conditions are true: <ul> You add a TextBox control (TextBox1) to an instance of a user control with ID &quot;UserControlID&quot; in a form named formName.</li> You use client-side JavaScript to access the value that the user types in the TextBox1 text box.</li>  You try to use the following simple dotted DOM syntax to access through the ID of the TextBox (UserControlID_TextBox1) when you view the page in Netscape: myval = document.formName.UserControlID_TextBox1.value; </li></ul>

The error always occurs in this scenario because JavaScript under Netscape uses the Name attribute of the TextBox to identify the TextBox in the DOM. The ASP.NET naming convention causes the Name attribute to be rendered with a colon (for example, UserControlID:TextBox1). However, in JavaScript, you cannot reference the name that is assigned to the TextBox in the simple dotted DOM syntax. Therefore, you must use the explicit form of addressing instead. For example: myval = 'document.formName[&quot;UserControlID:TextBox1&quot;].value'; With this revised syntax, the JavaScript works as expected on both Microsoft Internet Explorer and Netscape.

The third-party products that are discussed in this article are manufactured by companies that are independent of Microsoft. Microsoft makes no warranty, implied or otherwise, regarding the performance or reliability of these products.

Additional query words: aspnet

Keywords: kbprb KB318101

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.