How can I draw iterating ul-li List pattern?
I want to draw the Html like this
<ul>
<li>1
<ul>
<li>1</li>
</ul>
</li>
<li>2
<ul>
<li>1</li>
<li>2</li>
</ul>
</li>
<li>3
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</li>
</ul>
For this I wrote a method shown below
private void DrawTree(HtmlGenericControl ParentULTag, int count)
{
for (int i = 0; i < count; i++)
{
HtmlGenericControl ChildLi = new HtmlGenericControl("li");
HtmlGenericControl ChildUl = new HtmlGenericControl("ul");
ChildLi.Controls.Add(ChildUl);
ParentULTag.Controls.Add(ChildLi);
ChildLi.InnerText = i.ToString();
DrawTree(ChildUl, i);
}
}
But it Draw it like this
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
I Initiated the method in the pageload as shown
HtmlGenericControl Initial = new HtmlGenericControl("ul");
DrawTree(Initial,4);
Whats the mistake in this code?
EDIT 1:
I don't want a two for loop method. I want to draw it recursively. This is
not the real case I need.
No comments:
Post a Comment