在div的InnerText属性中添加换行符

时间:2013-05-22 18:37:51

标签: c# asp.net

我有一个按钮单击事件,它应该从文本框中获取文本,然后在同一页面上写入div的InnerText属性。文本正在输入div,但它永远不会出现在新行上。我尝试了以下变化: 编辑:对于选择关闭此问题的人,这是在div内部而不是在多行文本框内。它们的工作方式不同,因为当我使用多行文本框时,我的工作正常。

protected void Button1_Click(object sender, EventArgs e)
    {

        if (txtCategory.Text.Length > 0)
        {
            StringBuilder sb = new StringBuilder(drugTerms.InnerText);
            sb.AppendLine(txtCategory.Text);
            drugTerms.InnerText = sb.ToString();
            txtCategory.Text = "";
        }

    }


 protected void Button1_Click(object sender, EventArgs e)
    {

        if (txtCategory.Text.Length > 0)
        {
            StringBuilder sb = new StringBuilder(drugTerms.InnerText);
            sb.Append(txtCategory.Text).Append(Environment.NewLine);
            drugTerms.InnerText = sb.ToString();
            txtCategory.Text = "";
        }

    }

ASPX

<%@ Page Language="C#" AutoEventWireup="true" Inherits="_Default" Codebehind="Default.aspx.cs" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>AutoComplete Text Box with jQuery</title>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css"
        rel="stylesheet" type="text/css" />
    <link href="StyleSheet1.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            SearchText();
        });
        function SearchText() {
            $(".auto").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "Default.aspx/GetAutoCompleteData",
                        data: "{'CategoryName':'" + document.getElementById('txtCategory').value + "'}",
                        dataType: "json",
                        success: function (data) {
                            response(data.d);
                        },
                        error: function (x) {
                            alert("Error Occurred");
                        }
                    });
                }
            });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="demo">
        <div class="ui-widget">
            <label for="tbAuto">
                Enter Drug&nbsp; Name:
            </label>
            <asp:Button ID="Button2" runat="server" OnClick="Button1_Click" Text="Button 2" />
            <asp:TextBox ID="txtCategory" CssClass="auto" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button 1" />
            <div id="drugTerms" runat="server"></div>




        </div>
    </div>



    </form>
</body>
</html>

我尝试在其中添加一个html断行符。每次单击该按钮时,文本框中的文本都会添加,新术语是空格而不是换行符。为什么我不能在这个div中添加换行符?

2 个答案:

答案 0 :(得分:3)

使用"<br>"代替Environment.NewLine

答案 1 :(得分:0)

你尝试过使用新的Literal和PlaceHolder吗?

aspx:

<asp:PlaceHolder ID="ph" runat="server' visible="true"></asp:PlaceHolder>

然后你的代码看起来像这样:

C#:

Literal literal = new Literal(); 
lit.Text = "<br/>";
ph.Controls.Add(lit);

我实际上喜欢@ I4V使用中断换行环境的解决方案。