ASP.Net Binding to Gridview Strips some space (whitespace characters)

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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;");
    }
}

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.

Method 2

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 '  "


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x