在文本换行的字符串中插入换行符?

时间:2014-02-12 20:27:39

标签: c# newline repeater line-breaks breadcrumbs

我想在一个字符串中插入一个换行符,只要它包裹在“错误”的位置(所讨论的字符串是一个名为“BreadCrumbsRepeater”的转发器中的面包屑样式导航)。现在,无论是否分解产品名称,文本都将换行。例如:

主页>其他产品>染色&完成> ZAR Stain&完成> ZAR Ultra Max Wood Stain> ZAR Ultra Max [LINE BREAK] Espresso Wood Stain - Quart

例如,我想在上面的例子中做的是在“>”之前插入换行符符号就在“ZAR Ultra Max ....”之前。我已经阅读了有关Environment.NewLine的内容,但我不确定如何将其应用于此类特定事件。甚至可以这样做吗?

作为一个重要的注意事项,转发器位于具有设定宽度的div内(没有设置宽度,文本将在页面的边框处运行)。此外,我已设法在任何给定时间获取转发器内的字符总数。

任何帮助将不胜感激! :)

以下是具有转发器的模板代码:

    <div class="breadCrumbs categoryBreadCrumbs">
    <div class="innerSection">
        <div class="content" style="top: 10px;float: left; clear:both; margin:0 auto; width:650px; display:table-cell; vertical-align:middle; left: 244px; position: relative; white-space: inherit; overflow: inherit; font-size: 11px;">
            <asp:HyperLink ID="HomeLink" runat="server" NavigateUrl="" Text="Home "></asp:HyperLink>
            <asp:Repeater ID="BreadCrumbsRepeater" runat="server">
                <HeaderTemplate>&nbsp;&gt;&nbsp;</HeaderTemplate>
                <ItemTemplate>
                   <span id="crumbs"><asp:HyperLink ID="BreadCrumbsLink" runat="server" NavigateUrl='<%#Eval("NavigateUrl")%>' Text=' <%#Eval("Name")%>'></asp:HyperLink></span>
                </ItemTemplate>
                <SeparatorTemplate>&nbsp;&gt;&nbsp;</SeparatorTemplate>
            </asp:Repeater>
            <asp:Label ID="CurrPageBCLbl" runat="server" style="font-weight:bold;" /> 
            <br /><asp:Label ID="foo" runat="server" visible="false"/>
            <br /><asp:Label ID="numChar" runat="server" />
        </div>
    </div>
</div>

这是背后的代码:

 [Description("Displays bread crumbs for the current category")]
    public partial class CategoryBreadCrumbs : System.Web.UI.UserControl
    {
        int _CategoryId = 0;
        bool _HideLastNode = false;

        public int CategoryId
        {
            get { return _CategoryId; }
            set { _CategoryId = value; }
        }

        [Browsable(true), DefaultValue(true)]
        [Description("If true last node is hidden")]
        public bool HideLastNode
        {
            get { return _HideLastNode; }
            set { _HideLastNode = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.CategoryId = AbleCommerce.Code.PageHelper.GetCategoryId();

            Webpage _webpage = WebpageDataSource.Load(PageHelper.GetWebpageId());
            Product _product = ProductDataSource.Load(PageHelper.GetProductId());
            Category _category = CategoryDataSource.Load(PageHelper.GetCategoryId());

            HomeLink.NavigateUrl = AbleCommerce.Code.NavigationHelper.GetHomeUrl();
            int MainCatalog = BairdLookUp.GetCatalogStartNode();
            if (this.CategoryId != 0)
            {
                IList<CatalogPathNode> path = CatalogDataSource.GetPath(CategoryId, false);

                DataSet ds = new DataSet();
                DataTable dt = ds.Tables.Add("NavTable");
                dt.Columns.Add("Name", Type.GetType("System.String"));
                dt.Columns.Add("NavigateUrl", Type.GetType("System.String"));

                foreach (CatalogPathNode xPath in path)
                {
                    if (Convert.ToInt32(xPath.CatalogNodeId) != MainCatalog)
                    {
                        // Response.Write(xPath.Name + " ");
                        DataRow dr = dt.NewRow();
                        dr["Name"] = xPath.Name;
                        dr["NavigateUrl"] = xPath.NavigateUrl;
                        dt.Rows.Add(dr);
                    }
                }
                BreadCrumbsRepeater.DataSource = ds;
                BreadCrumbsRepeater.DataMember = "NavTable";
                BreadCrumbsRepeater.DataBind();

            }
            else BreadCrumbsRepeater.Visible = false;

            //need to show/hide last node and show the current label

            if (_webpage != null)
            {
                if (BreadCrumbsRepeater.Controls.Count > 1)
                    CurrPageBCLbl.Text = " > ";
                if (_webpage.Title != "")
                    CurrPageBCLbl.Text += _webpage.Title;
                else
                    CurrPageBCLbl.Text += _webpage.Name;
            }
            else if (_product != null)
            {
                CurrPageBCLbl.Text = " > ";
                CurrPageBCLbl.Text += _product.Name;
            }
            else
            {
                if ((HideLastNode) && (BreadCrumbsRepeater.Controls.Count > 0))
                {
                    BreadCrumbsRepeater.Controls[(BreadCrumbsRepeater.Controls.Count - 1)].Visible = false;
                    CurrPageBCLbl.Text += _category.Name;
                }
            }

            int count = 0;
            string label = CurrPageBCLbl.Text;
            foreach (RepeaterItem items in BreadCrumbsRepeater.Items)
            {
                HyperLink crumbs = items.FindControl("BreadCrumbsLink") as HyperLink;

                string str = crumbs.Text;

                foreach (char c in str)
                {
                    if (char.IsWhiteSpace(c) || char.IsLetterOrDigit(c) || char.IsPunctuation(c) || char.IsSeparator(c) || char.IsSymbol(c))
                    {
                        count++;
                        if (_product !=null)
                        {
                            string newString = crumbs.Text + label;
                            count++;

                        }
                    }
                }

        }

    }

1 个答案:

答案 0 :(得分:0)

您可以使用“&nbsp;

替换产品名称中的空格
dr["Name"] = xPath.Name.Replace(" ", "&nbsp;");

使用CSS:

dr["Name"] = "<span style=\"white-space:nowrap;\">" + xPath.Name + "</span>";

您还需要修改此部分:

<SeparatorTemplate> &gt; </SeparatorTemplate>