Click here to Skip to main content
Licence CPOL
First Posted 6 Nov 2008
Views 12,182
Bookmarked 15 times

Avoid Multiple Space Elimination in ASP.NET GridView Control

By Raheel Afzal Khan | 6 Nov 2008
This article describes how to avoid multiple space elimination in ASP.NET Gridview Control.
1 vote, 25.0%
1
1 vote, 25.0%
2
1 vote, 25.0%
3

4
1 vote, 25.0%
5
2.45/5 - 4 votes
μ 2.45, σa 2.99 [?]

Introduction

We often observe a gridview "bug" that we are saving the string data in the database just like this "Testing    Testing1       Testing2    Testing123", but when it is shown in the gridview, it looks like this "Testing Testing1 Testing2 Testing123".
GridView eats the multiple spaces. This is only because of the browser that parses HTML. It never knows spaces, it only considers   as space.

Workarounds

If you want to avoid the automatic elimination of multiple spaces string type data inside gridview, there are some workarounds that exist for it.

1. BoundField

In case if you are using the BoundField, then define it in this way.

<asp:BoundField DataField="description" DataFormatString="<pre>{0}</pre>" 
	HtmlEncode="False" />

There are two important properties of this field:

  1. Keep the HtmlEncode=False
  2. Enclose the DataFormatString inside the <pre></pre> tag

2. TemplateField

If you are using the TemplateField, then define your template field in this way.

    <asp:TemplateField ConvertEmptyStringToNull="False">
        <ItemTemplate>
            <pre ><asp:Label ID="Label1" runat="server" 
		Text='<%# Bind("description") %>'></asp:Label></pre>
        </ItemTemplate>
    </asp:TemplateField>

There is one important property that needs to be set in this case:

  1. Enclose your template control inside the <pre></pre> tag (in the above example, I have enclosed the Label control inside <pre></pre>.

Save the HTML Friendly Spaces

If you don't want to enclose BoundField or the TemplateColumn inside the <pre></pre> tags, then you have to manually change the space with HTML compatible space, i.e. &nbsp; at the time while saving it in the database.

A very simple way is to use the Replace function of string. Just like this:

string str = "Testing    Testing1       Testing2     Testing123";
str.Replace(" ", " "); 

History

  • 6th November, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Raheel Afzal Khan

Software Developer (Senior)
Self Employee
Pakistan Pakistan

Member

Follow on Twitter Follow on Twitter
I have been in the programming for the last 6 years. Created many desktop and web based applications using Microsoft technologies.Currently i have been exploring the features of .NET 4.0.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalnice PinmemberMalikRizwan0:52 13 Oct '10  
GeneralMy vote of 1 PinmemberDima Pasko12:27 9 Mar '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120209.1 | Last Updated 6 Nov 2008
Article Copyright 2008 by Raheel Afzal Khan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid