开发者

Displaying Parent/Children name with indent

i have an object that have parent/children relationship.

below is shorter version of what i have but the below is the core props

public class Company
{
  Name {get;set;}
  List<Company> ChildCompany {get;set;} 
}

displaying something like this:

-----ABC Corporation (in this chas ABC has child company so it will display right indent)
----------ABC Child
----------ABC One
----------ABC Two
-----BBC Corporation (in this case no child company)
-----CBS

-----CNN Corporation
----------ABC
----------BBC
----------NBC

my code:

Company company = new Company();
company = GetDataForCompany(); //DAL

if(!string.IsNullEmpty(company.Nam开发者_Go百科e))
{

     //dispaly Name
     PlaceHolder ph;
     l = new Literal();
     l.Text = "<ul>" + Environment.NewLine;
     ph.Controls.Add(l);
     l = new Literal();
     l.Text = "<li">";
     ph.Controls.Add(l);
     hl = new HyperLink();
     hl.Text = company.Name;
     ph.Controls.Add(hl);


    foreach (Company item in company)
    {
          l = new Literal();
          l.Text = "<li">";
          ph.Controls.Add(l);
          hl = new HyperLink();
          hl.Text = item.Name;
          ph.Controls.Add(hl);
    }

}

the above code does not seems to rendering exactly what i wanted as shown above.


First of all, literals aren't intended for such task, but you have for example HtmlGenericControl which fits better your goal. And same for place holders - these are for templating -.

HtmlGenericControl unorderedList = new HtmlGenericControl("ul");

HtmlGenericControl tempItem = null;
HtmlGenericControl tempAnchor = null;

foreach (Company item in company)
{
     tempItem = new HtmlGenericControl("li");
     unorderedList.Controls.Add(tempItem);

     tempAnchor = new HtmlGenericControl("a");
     tempAnchor.Controls.Add(new Literal { Text = item.Name });
     tempItem.Controls.Add(tempAnchor);
}

If you need further features, you can use System.Web.HtmlControls namespace which has web controls for any of common (X)HTML forms' elements and more:

  • http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols(v=VS.100).aspx

About literals, these are for localization or a good way of representing literal text in server controls, since literals have an identifier like any other control, easing text substitution and other tasks.

Another important point is indenting will be achieved by overriding server controls' methods like Render, RenderChildren and so on, which provides you access to the HtmlTextWriter, the text stream writer later putting its output to the HTTP response stream.


You aren't nesting the Child companies within another unordered list. You need to check for child companies and nest those list items in another list to get the HTML to look like you need it.

// Code to display name here....
if (company.ChildCompany != null && company.ChildCompany.Count > 0)
{
    l = new Literal();
    l.Text = "<ul>";
    ph.Controls.Add(l);


    foreach (Company item in company.ChildCompany)
    {
        // Code for children.
    }

    l = new Literal();
    l.Text = "</ul>";
    ph.Controls.Add(l);
}

The output should end up looking something like:

<ul>
     <li>Company
         <ul>
             <li>Child Company #1</li>
             <li>Child Company #2</li>
         </ul>
     </li>
 </ul>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜