Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

FIX: DataGrid Web Server Control Does Not Display "<" and ">" Characters Correctly


View products that this article applies to.

This article was previously published under Q317878
The following Microsoft .NET Framework Class Library namespace is referenced in this article:

System.Data

↑ Back to the top


Symptoms

When you create a Web application that displays data in a DataGrid control, if the data contains the less than character (<) and the greater than character (>), Microsoft Internet Explorer interprets these characters as Hypertext Markup Language (HTML) directives. As a result, the string of data between the "<" and ">" characters in the DataGrid control cannot be displayed.

↑ Back to the top


Resolution

To resolve this problem, change "<" to "&lt;" and ">" to "&gt;".

↑ Back to the top


Status

This bug was corrected in Microsoft .NET Framework 1.0 and in Microsoft Internet Explorer (Programming) 5.01, 5.5, and 6.0.

↑ Back to the top


More information

Steps to Reproduce Behavior

  1. Create a new file named Repro.aspx, and then add the following code to this file:
    <%@ Import Namespace="System.Data" %>
    <html>
    <script language="VB" runat="server">
    
        Dim Cart As DataTable
        Dim CartView As DataView
        
        Function CreateDataSource() As ICollection
            Dim dt As New DataTable()
            Dim dr As DataRow
            
            dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
            dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
            dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
            
            Dim i As Integer
            For i = 0 To 3
                dr = dt.NewRow()  
                
                dr(0) = i
                dr(1) = "Item " & i.ToString()   ' If you use "<Item>" instead of "Item", 
                                                 ' the row is not displayed correctly.
                dr(2) = 1.23 *(i + 1)
                
                dt.Rows.Add(dr)
            Next i
            dr = dt.NewRow()
            dr(0) = 4
            dr(1) = "<Item> " & 4.ToString()   ' The problem occurs here. 
                                               ' To resolve the problem, comment the preceding
                                               ' line, and uncomment the following line.
            'dr(1) = "<Item> " & 4.ToString()
             dr(2) = 1.23 *(4 + 1)
             dt.Rows.Add(dr)
            
            Dim dv As New DataView(dt)
            Return dv
        End Function 'CreateDataSource
    
        Sub Page_Load(sender As Object, e As EventArgs)
            
            If Not IsPostBack Then
                'Load this data only one time.
                ItemsGrid.DataSource = CreateDataSource()
                ItemsGrid.DataBind()
            End If
        End Sub 'Page_Load
          
    </script>
     
    <body>
     
       <form runat=server>
    
          <h3><font face="Verdana">Q317878: DataGrid Web Server Control Does Not Display '<' and '>' Characters Correctly</font></h3>
          <h4>Because you added the "<Item>" string in the DataGrid control, the data grid is not displayed correctly.</h4>
          <h4>The first four rows in the data grid appear as expected. However, the last row demonstrates the problem. </h4>
          <h5>This problem occurs because you include < and > around "Item". </h4>
     
          <asp:DataGrid id="ItemsGrid" runat="server"
               BorderColor="black"
               BorderWidth="1"
               CellPadding="3"
               ShowFooter="true"
               AutoGenerateColumns="true">
    
             <HeaderStyle BackColor="#00aaaa">
             </HeaderStyle>
    
             <FooterStyle BackColor="#00aaaa">
             </FooterStyle>
     
          </asp:DataGrid>
           
       </form>
     
    </body>
    </html>
    					
  2. Open the Repro.aspx file in Internet Explorer. Note that Internet Explorer does not display the fourth row, which contains the "<Item>" string, correctly.

↑ Back to the top


Keywords: KB317878, kbctrl, kbprb, kbwebforms, kbdatabinding, kbsystemdata

↑ Back to the top

Article Info
Article ID : 317878
Revision : 4
Created on : 6/5/2003
Published on : 6/5/2003
Exists online : False
Views : 574