wootscootinboogie
wootscootinboogie

Reputation: 8695

Click button to add new line character

This is frustrating me. I've tried

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txtDrugList.Focus();

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (txtDrugList.Text.Length > 0)
        {
            drugDiv.InnerHtml += txtDrugList.Text + " ";
            Response.Write("\n");

        }
        txtDrugList.Text = "";

    }
}

and

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txtDrugList.Focus();

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (txtDrugList.Text.Length > 0)
        {
            drugDiv.InnerHtml += txtDrugList.Text + Environment.NewLine;


        }
        txtDrugList.Text = "";

    }
}

and neither work. I want to have a button click add a newline character to a div. Why the heck isn't this working?

Upvotes: 0

Views: 911

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502106

You haven't said what you mean by "neither work" - but have you remembered that in HTML you need something like <br /> for a visible line break?

I strongly suspect that if you look at the raw HTML you are including a line break - it's just that it's not doing anything to the displayed version. So I suspect you want something like:

if (txtDrugList.Text.Length > 0)
{
    drugDiv.InnerHtml += "<br />" + txtDrugList.Text;
}

Note that I've also put the line break before txtDrugList.Text - otherwise you've got a line break at the end of the HTML, which probably isn't what you were looking for.

Upvotes: 4

Related Questions