views:

1626

answers:

3

I am retrieving data from an Oracle database and binding the same to a gridview control.

I noticed that there are instances when the column contains a single quote or double quote, the spaces or whitespace characters get stripped off.

Sample of some data in fields in Oracle:

To Be Phased Out ASAP ' ",

When retrieved, it becomes...

To Be Phased Out ASAP ' ",

And another one...

IT''S TEST RECORD" DDD " FFF

which becomes

IT''S TEST RECORD" DDD " FFF

I don't have any clue why this is happening...

any ideas?

I think even here the spaces are getting trimmed. In my example in the first field which is:

To Be Phased Out ASAP ' "

there are actually two spaces after the single quote but it is displaying just a single space here.. Odd...

single quote space space double quote --> ' "

I think the extra space after a quote or single quote is being removed by asp.net??

I also found out that when I am editing my gridview, the extra white spaces are retained but when I go back to the original view, the whitespaces are gone.

To rephrase this question..

How Do I Preserve the WhiteSpace in the gridview when displaying the data?

+2  A: 

This is not an ASP.Net thing, it is an HTML parsing thing. If you were to create a plain Jane HTML page with a div tag in it, and then put 100 spaces between the opening and closing tag it would all be condensed into a single space.

This is a classic web issue. If you really want to have everything come out correctly, then you will need to HTML encode any spaces before displaying them on the page.

Try replacing all your spaces with  

Here is an article that explains this a little more in-depth: http://webdesign.about.com/od/beginningtutorials/f/blfaqwhitespace.htm

To answer the question in the comment:

If you need to have a lot of control over exactly what is going out to your grid even when it is DataBound, you can simply handle the RowDataBound event that fires after each row is bound.

Suppose you have a GridView that looks like this:

<asp:GridView ID="gbGridWithSpaces" AutoGenerateColumns="false" runat="server" 
    onrowdatabound="gbGridWithSpaces_RowDataBound">
    <Columns>
        <asp:BoundField DataField="ItemWithSpaces" HeaderText="Item With Spaces" />
    </Columns>
</asp:GridView>

In your code behind, you can handle the event to re-format the outgoing text however you please. For example:

protected void gbGridWithSpaces_RowDataBound(object sender, GridViewRowEventArgs e)
{
    foreach (TableCell cell in e.Row.Cells)
    {
     cell.Text = cell.Text.Replace(" ", "& nbsp;"); 

            //There is no space between the ampersand and the nbsp; 
            //part, but SO's wiki syntax doesn't handle code formatting 
            //inside of other code formatting
    }
}

That would effectively replace all spaces with &nbsp; and preserve them even when rendered to the browser. Just remember your data will come back this way as well, so you will need to handle this on the server if you plan to pass this information back into persistent storage.

Josh
Batuta
A: 

If the spaces are important, you could try wrapping in preformatted text elements <pre> </pre> tags.

<pre>To Be Phased Out ASAP '  "</pre>

becomes

To Be Phased Out ASAP '  "
Dave
How Do I do it in an Itemtemplate in a gridview?
Batuta
My item template in gridview is marked up like this. Where do I add the <pre> tags?<ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<% Eval("datacol") %>' </asp:Label>/<ItemTemplate>
Batuta
<ItemTemplate><pre><asp:Label/></pre></ItemTemplate>
Dave
A: 

Good Solution, i have been breaking my head with the issue for 2 days