2014-02-12 99 views
2

我想在「錯誤」的位置換行時插入一個換行符(這個字符串是一個名爲「BreadCrumbsRepeater」的轉發器中的麪包屑樣式導航)。現在,無論是否打破產品名稱,文本都會自動換行。例如:在文本換行的字符串中插入換行符?

首頁>其他產品>染色&完成> ZAR染色&完成> ZAR超最大木材變色> ZAR超最大[換行符]咖啡木材變色 - 夸脫

我想例如,在上例中,在「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++; 

         } 
        } 
       } 

     } 

    } 

回答

0

您可以通過「&nbsp;」更換產品名稱中的空格

dr["Name"] = xPath.Name.Replace(" ", "&nbsp;"); 

OR

與CSS:

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

,你還需要修改這一部分:

<SeparatorTemplate> &gt; </SeparatorTemplate> 
+0

我不知道爲什麼我需要這個有關係嗎? –

+0

nbsp代表「不間斷的空間」。如果寬度太小,則該空間不會被破壞 – alu

+0

但是,如果使用nowrap屬性,則文本將在頁面寬度上延伸... –