![]() |
Web Development »
ASP.NET Controls »
Grid Controls
Intermediate
License: The Code Project Open License (CPOL)
Avoid Multiple Space Elimination in ASP.NET GridView ControlBy Raheel Afzal KhanThis article describes how to avoid multiple space elimination in ASP.NET Gridview Control. |
.NET, ASP.NET, WebForms, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
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.
If you want to avoid the automatic elimination of multiple spaces string type data inside gridview, there are some workarounds that exist for it.
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:
HtmlEncode=False DataFormatString inside the <pre></pre> tag 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:
<pre></pre> tag (in the above example, I have enclosed the Label control inside <pre></pre>. 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. 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(" ", " ");
| You must Sign In to use this message board. | ||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
General
News
Question
Answer
Joke
Rant
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 |