Solved

Why won't System.Environment.Newline create a newline?

Posted on 2014-09-03
5
272 Views
Last Modified: 2014-09-03
I'm trying to create a line of text with carriage returns in it but my code is ignoring the System.Environment.Newline call for some reason. I've also tried putting: "\r\n" into the string as well but to no avail. What am I doing wrong?

HTML

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
    <asp:Panel ID="pnlManager" runat="server">
        <div class="divManager">
            <div class="divError">
                <asp:Label ID="lblError" runat="server" />
            </div>

C#

    protected void Submit_Click(object sender, EventArgs e)
    {
        lblError.Text = "";

        if (txtDtOfReq.Text.Trim().Equals(""))
        {
            lblError.Text = lblError.Text + "- Date of Request cannot be blank!" + System.Environment.NewLine;
        }
        if (txtEmpName.Text.Trim().Equals(""))
        {
            lblError.Text = lblError.Text + "- Employee name cannot be blank!" + System.Environment.NewLine;
        }
        if (txtHrDtTrmDt.Text.Trim().Equals(""))
        {
            if (!lblError.Equals(""))
                lblError.Text = lblError.Text + System.Environment.NewLine + "- Hire date cannot be blank!";
        }
        if (txtImmSupName.Text.Trim().Equals(""))
        {
            lblError.Text = lblError.Text + "- Immediate supervisor cannot be blank!" + System.Environment.NewLine;
        }
        if (!pUserBranch.Equals("NAT"))
        {
            if ((ddlDepartment.SelectedIndex.Equals(0)) || (ddlPosJobTitle.SelectedIndex.Equals(0)))
            {
                lblError.Text = lblError.Text + "- Please select a department and corresponding position or \"Other\"!" + System.Environment.NewLine;
            }
            if ((ddlPosJobTitle.SelectedValue.Equals("Other")) && (!ddlDepartment.SelectedValue.Equals("Other")))
            {
                if (txtModelAfterTextBox.Text.Trim().Equals(""))
                    lblError.Text = lblError.Text + "- Please enter a description of position to model after in \"Model After:\" text box!" + System.Environment.NewLine;
            }
        }
        else if (pUserBranch.Equals("NAT"))
        {
            if (txtModelAfterTextBox.Text.Trim().Equals(""))
                lblError.Text = lblError.Text + "- Please enter a description of position to model after in \"Model After:\" text box!" + System.Environment.NewLine;
        }
    }

Open in new window

0
Comment
Question by:mikesExpertExchange
5 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 40301181
what if is your test data going into? just to narrow it down.
0
 
LVL 1

Accepted Solution

by:
kjsiemek earned 500 total points
ID: 40301189
Just taking a stab here - but since this is ASP, wouldn't you want "<br/>" instead of "\r\n"?  Just a guess.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40301205
To expand on kjsiemek's comment, HTML collapses multiple whitespaces when rendering a document to the screen. If you examine the source code of the rendered page, you most likely will see the newline that you output. However, due to the aforementioned collapsing, you won't see it rendered on the screen. Use the tag that kjsiemek mentions.
0
 
LVL 1

Author Closing Comment

by:mikesExpertExchange
ID: 40301250
That was it.I do find it strange that in a previous web application the System.Environment.Newline; worked. Obviously I'm missing a detail with regards to the difference of that project vs. this one. Never the less thank you.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40301277
If your text is inside of a <pre> tag, then that tag will respect the text as it is--meaning that all whitespace and newlines are displayed exactly as they are. Without using a <pre> tag, whitespace will be collapsed.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now