2014-09-12 59 views
1

我有一個ASP.NET Web應用程序。此應用程序呈現出詞彙類型的項目,類似如下:動態添加控件時跨度兩列

glossary

此經過字母表中的物品所有的字母。我呈現了這一點,並使用以下直接附加到一個控件集合在一個服務器控制:

 List<char> alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray().ToList(); 
    foreach (char c in alpha) 
    { 
      Label lblAlphaCharacter = new Label(); 
      lblAlphaCharacter.Font.Size = 24; 
      lblAlphaCharacter.Font.Bold = true; 
      lblAlphaCharacter.Text = c.ToString(CultureInfo.InvariantCulture); 
      Controls.Add(lblAlphaCharacter); 
      Controls.Add(new LiteralControl("<p>")); 
      FilterOnAlphaCharacter(this, Page, c); 
      Controls.Add(new LiteralControl("<p>")); 
     } 

     private static void FilterOnAlphaCharacter(Control control, Page page, char character) 
     { 

      foreach (List<Things> item in items) 
      { 
       string title = item.Title; 
       string description = item.Definition; 
       HyperLink link = new HyperLink(); 
       link.Text = title; 
       control.Controls.Add(link); 

       Label lblDescription = new Label(); 
       lblDescription.Text = string.Format(" - {0}", description); 
       control.Controls.Add(lblDescription); 
      } 
     } 
    } 

我想,這取決於內容,平分這一點,所以它看起來是這樣的:

enter image description here

這可以有不同數量的項目。所以實際上,在「A」下可能有25個條目,而在「Z」下可能有1個條目。以上只是一個例子,它貫穿所有字母A-Z。預期的結果將基於內容的數量,它將平分兩欄。我必須做這個服務器端(我想使用表或HtmlTable和相關的對象)。

Howe你會實現一個解決方案,用於在表格或類似內容中平等地分割內容(對處理方式無所謂)。

回答

1

試試這個:

//it shows the number of line that inserting during the process 
      private int _inserteditemCount; 
      //its number of items in each column 
      private int _itemsCount; 
      //line height use for determine paragraph line height 
      private const string Lineheight = "30px"; 

      protected void Page_Load(object sender, EventArgs e) 
      { 
       _inserteditemCount = 0; 


     var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); 
//you can do this query in data access layer 
        var listCountcount = new Thingsclass().GetThings().Count; 
        //Count of rows on dictionary + number of leters 
        _itemsCount = (listCountcount + alpha.Count())/2; 
        var leftdiv = new HtmlGenericControl("div"); 
        var rightdiv = new HtmlGenericControl("div"); 
        //you can change this styles 
        leftdiv.Style.Add("display", "inline-block"); 
        leftdiv.Style.Add("width", "50%"); 
        leftdiv.Style.Add("float", "Left"); 
        rightdiv.Style.Add("display", "inline-block"); 
        rightdiv.Style.Add("float", "right"); 
        rightdiv.Style.Add("width", "50%"); 
        foreach (var c in alpha) 
        { 
         var lblAlphaCharacter = new Label(); 
         lblAlphaCharacter.Font.Size = 24; 
         lblAlphaCharacter.Font.Bold = true; 
         lblAlphaCharacter.Text = c.ToString(CultureInfo.InvariantCulture); 
         var control = _inserteditemCount <= _itemsCount ? leftdiv : rightdiv; 
         var paragraph = new HtmlGenericControl("p"); 
         paragraph.Style.Add("line-height", Lineheight); 
         paragraph.Controls.Add(lblAlphaCharacter); 
         control.Controls.Add(paragraph); 
         FilterOnAlphaCharacter(leftdiv, rightdiv, c.ToString()); 
         _inserteditemCount++; 
        } 
        Panel1.Controls.Add(leftdiv); 
        Panel1.Controls.Add(rightdiv); 
       } 

      private void FilterOnAlphaCharacter(Control leftctr, Control rightctr, string character) 
      { 
//you can do this query in data access layer 
        var items = new Thingsclass().GetThings().Where(c => c.chara.ToLower().Equals(character.ToLower())); 

       foreach (var item in items) 
       { 
        var paragraph = new HtmlGenericControl("p"); 
        paragraph.Style.Add("line-height", Lineheight); 
        var control = _inserteditemCount <= _itemsCount ? leftctr : rightctr; 

        var title = item.Title; 
        var description = item.Description; 
        var link = new HyperLink { Text = title }; 
        paragraph.Controls.Add(link); 

        var lblDescription = new Label { Text = string.Format(" - {0}", description) }; 
        paragraph.Controls.Add(lblDescription); 
        _inserteditemCount++; 
        control.Controls.Add(paragraph); 
       } 
      }