GridView / DataGrid difference

  • Thread starter Thread starter GaryDean
  • Start date Start date
G

GaryDean

I have a 1.1 control that I wrote that prints a DataGrid as a Crystal Report
and it works just fine. I now have enhaced it to 2.0 and the ability to
print a GridView. The gridview, however, doesn't print properly because the
gridview is handling escaped characgters differently. I placed a
SQLDataSource, a gridview, and a datagrid on a page and bound both grids to
the same datasource pointing to Northwind db.

The gridview shows the escaped characters properly in the web page but they
are not displayed properly in the generated Html (view source). As you can
see below the snippet from the Gridview shows the name containing
ö.....


<td>BERGS</td><td>Berglunds snabbköp</td><td>Christina Berglund</td>

The source below is from the datagrid and dispalys the escaped character as
it should be displayed....

<td>BERGS</td><td>Berglunds snabbköp</td><td>Christina Berglund</td>

When my print control digs the data out of the GridView it is getting the
&#246 instead of the O with the two little dots above it. So it prints it
wrong. The Gridviw is letting the web page ingterpret the escaped
characters whereas the datagrid apparently does the interpreting before
rendering.

Why this difference? If I'm going to have to do this conversion myself, is
there any docs on how it's done?
Thanks
Gary

The entire aspx page is pasted here...
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td style="width: 32px">
</td>
<td style="width: 599px">
<div style="overflow: auto; width: 869px; height:
244px">
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="CustomerID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="CustomerID"
HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName"
HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName"
HeaderText="ContactName" SortExpression="ContactName" />
<asp:BoundField DataField="ContactTitle"
HeaderText="ContactTitle" SortExpression="ContactTitle" />
<asp:BoundField DataField="Address"
HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="City"
HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Region"
HeaderText="Region" SortExpression="Region" />
<asp:BoundField DataField="PostalCode"
HeaderText="PostalCode" SortExpression="PostalCode" />
<asp:BoundField DataField="Country"
HeaderText="Country" SortExpression="Country" />
<asp:BoundField DataField="Phone"
HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Fax"
HeaderText="Fax" SortExpression="Fax" />
</Columns>
</asp:GridView>
</div>
</td>
<td style="width: 126px">
</td>
</tr>
<tr>
<td style="width: 32px">
</td>
<td style="width: 599px">
</td>
<td style="width: 126px">
</td>
</tr>
<tr>
<td style="width: 32px">
</td>
<td style="width: 599px">
<div style="overflow: auto; width: 869px; height:
244px">
<asp:DataGrid ID="DataGrid1" runat="server"
DataSourceID="SqlDataSource1">
</asp:DataGrid></div>
</td>
<td style="width: 126px">
</td>
</tr>

<tr>
<td style="width: 32px">
</td>
<td style="width: 599px">
</td>
<td style="width: 126px">
</td>
</tr>
<tr>
<td style="width: 32px">
</td>
<td style="width: 599px">
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT CustomerID, CompanyName,
ContactName, ContactTitle, Address, City, Region, PostalCode, Country,
Phone, Fax
FROM Customers">
</asp:SqlDataSource>
</td>
<td style="width: 126px">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
 
Hi Gary,

Regarding on this issue, since the data is retrieved from database and
directly displayed on page through DataGrid or GridView's databound
field(built-in one), I think the problem maybe caused by some particular
processing gridView has peformed. As far as I know, ASP.NET 2.0 GridView
control has add a "HtmlEncode" attribute, by default it is enabled which
means it will perform HtmlEncoding against the data bound and displayed
through this Field/column. This field is used to protect some malicious
code/script in the text data from executing.

You can try manually disable it as below to see whether it makes the output
become identical to the datagrid ones:

========
<asp:BoundField HtmlEncode=false ........../></Columns>
==============

#BoundField.HtmlEncode Property
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfiel
d.htmlencode.aspx


Also, you can use a TemplateField to maually bind and display the column
data through <%# Eval() %> expression to see whether this will suffer the
problem. I assume that using TemplateField and <%# Eval() %> will give the
same output as DataGrid control.

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.








--------------------
 
The datagrid was converting the escape sequences to characters for my. The
GridView does not do that - it apparently lets the browser to that.

So, my print utility will have to do the conversions. Are you aware of any
docs on that process? i.e. convertingö to a little o with dots on top?
Thanks,
Gary
 
Thanks for the followup Gary,

Yes, GridView and datagrid have difference on text encoding. So far the
what I get is for built-in BoundColumn or BoundField, GridView has the
"HtmlEncode" attribute setting for you to control the escaping of databound
values while DataGrid didn't(it may always do or not do).

Also, glad that you've found your own way to workaround the problem.

If you have any further questions, please feel free to post here also.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
From: "GaryDean" <[email protected]>
References: <[email protected]>
Subject: Re: GridView / DataGrid difference
Date: Sun, 9 Sep 2007 17:26:23 -0700
 
Back
Top