Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop asp:PlaceHolder from removing whitespaces

I came across some strange behavior of asp:PlaceHolder, it removes whitespaces between the asp:Literals within it.

For example for this code:

<asp:PlaceHolder ID="PlaceHolder1" runat="server">
    <asp:Literal ID="Literal1" Text="aa" runat="server"></asp:Literal>
    <asp:Literal ID="Literal2" Text="bb" runat="server"></asp:Literal>
</asp:PlaceHolder>
<asp:Literal ID="Literal3" Text="cc" runat="server"></asp:Literal>
<asp:Literal ID="Literal4" Text="dd" runat="server"></asp:Literal>

The output will be aabb cc dd.

Is there is a way to stop it from removing the whitespaces (and not by adding &nbsp; between the literals or Text="aa ")?

like image 970
Oleg Grishko Avatar asked Nov 09 '11 10:11

Oleg Grishko


People also ask

How to remove whitespace from a string in C?

How to remove whitespace from a C# string? 1 # Four ways to delete whitespace from C# strings. ... 2 # Remove all whitespace from a C# string. ... 3 # Strip whitespace from the start and end of a C# string. ... 4 # Remove whitespace from a C# string's start. ... 5 # Delete whitespace from the end of a C# string. ... 6 # Summary. ...

How do we remove the whitespaces for earlier versions?

How do we remove the whitespaces for earlier versions Show activity on this post. If going back to basics I would suggest using the trim () function followed by a regex replacement using replaceAll () that matches instances of the space characters i.e. (\s+) and replaces them with a single space.

What is placeholder control in ASP NET?

The placeholder control in ASP.NET is inherited from the control class. This is one of the controls in the ASP.NET framework that acts as an accommodating control for other controls and does not produce any outputs on the web page. Of course, the placeholder control is not viewed as it is not visible to the user and does not produce any HTML.

How to use trim() in C# to remove all the blank spaces?

Following is the Syntax of how we can use Trim () in C# to remove all the blank spaces as well as specific characters. 1. To remove the blank spaces from starting and ending. 2. To remove specific characters. First, it is used to remove whitespaces only and here we don’t need to provide any argument.


2 Answers

The most elegant solution is to simply override PlaceHolder:

using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomControls
{
    [ToolboxData("<{0}:Holder ID='Holder1' runat='server'></{0}:Holder>"), ControlBuilder(typeof(ControlBuilder))]
    public sealed class Holder: PlaceHolder
    {
    }
}

The key here is using ControlBuilder instead of PlaceHolderControlBuilder, which just overrides it to always return false for AllowWhiteSpaceLiterals.

You use it as any other web control:

<%@ Register TagPrefix="asp" Namespace="CustomControls" %>
<asp:Holder runat="server">...</asp:Holder>
like image 112
Nikola Bogdanović Avatar answered Oct 07 '22 18:10

Nikola Bogdanović


I came up with two solutions:

First: To replace the asp:PlaceHolder with asp:Panel. The disadvantage is that you get a wrapping div.

Second: To put an empty HTML comment between the literals

<asp:PlaceHolder ID="PlaceHolder1" runat="server">
    <asp:Literal ID="Literal1" Text="aa" runat="server"></asp:Literal><!>
    <asp:Literal ID="Literal2" Text="bb" runat="server"></asp:Literal>
</asp:PlaceHolder>

And yes, <!> is a legal HTML comment.

like image 37
Oleg Grishko Avatar answered Oct 07 '22 20:10

Oleg Grishko