Click here to Skip to main content
15,890,947 members
Articles / Web Development / ASP.NET
Article

Avoid Multiple Space Elimination in ASP.NET GridView Control

Rate me:
Please Sign up or sign in to vote.
2.67/5 (5 votes)
6 Nov 2008CPOL1 min read 33.8K   15   2
This article describes how to avoid multiple space elimination in ASP.NET Gridview Control.

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.NET
<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.NET
    <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(" ", "&nbsp;"); 

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)


Written By
CEO DatumSquare IT Services Pvt. Ltd.
Pakistan Pakistan
A computer science graduate with senior-level programming skills, Raheel Afzal has developed a number of management information systems and real estate portals. Through his work with other companies, Raheel was highly exposed to client relations, as well as American business models and operations.

Raheel started DatumSquare IT Services in 2009, facilitating daily operational management. His previous career experience and administration expertise have given him the insight and strategic planning skills to successfully transform a two-employee company into a thriving business with more than 60 highly skilled employees on staff.

Raheel presently resides in Rawalpindi, Pakistan, with his wife and two children. In his spare time he loves to read historical books and keep up with the latest worldly technological advancements.

Comments and Discussions

 
Generalnice Pin
MalikRizwan12-Oct-10 23:52
MalikRizwan12-Oct-10 23:52 
GeneralMy vote of 1 Pin
Dima Pasko9-Mar-09 11:27
Dima Pasko9-Mar-09 11:27 

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

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