Click here to Skip to main content
6,295,667 members and growing! (15,152 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » Grid Controls     Intermediate License: The Code Project Open License (CPOL)

Avoid Multiple Space Elimination in ASP.NET GridView Control

By Raheel Afzal Khan

This article describes how to avoid multiple space elimination in ASP.NET Gridview Control.
.NET, ASP.NET, WebForms, Dev
Posted:6 Nov 2008
Views:5,051
Bookmarked:14 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
4 votes for this article.
Popularity: 1.61 Rating: 2.67 out of 5
1 vote, 25.0%
1
1 vote, 25.0%
2
1 vote, 25.0%
3

4
1 vote, 25.0%
5

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


Member

Occupation: Web Developer
Location: Pakistan Pakistan

Other popular ASP.NET Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinmemberDima Pasko12:27 9 Mar '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Nov 2008
Editor: Deeksha Shenoy
Copyright 2008 by Raheel Afzal Khan
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project