Programatically adding nested repeater controls upto N-levels?
I'm creating a navigation menu. I've to render repeater control ul-li tags and menuitems can range upto N levels. I need to add a child repeater control dymnamically to parent control?
EDIT: Example - ul-li can goto n levels
<ul>
<li>
<ul>
<li>
<ul>
<li></l开发者_StackOverflow社区i>
<li></li>
<li></li>
</ul>
</li>
<li></li>
<li></li>
</ul>
</li>
<li></li>
<li></li>
</ul>
The repeater is not really designed for N nesting. The main problem is that you'd be creating them in the ItemDataBound event, and it would get messy trying to declare new event handlers for new controls.
The best thing is to use the Tree control.
Alternatively, if you really need to specify your template declarative, that is, define it in the aspx, then write your own control, which exposes templates for "nodes", but behaves like exactly like a Tree control.
If your data is dynamic, that's one way of doing it. Except I would not use a repeater I'd use a ListView if it's .Net 3.5 or greater. They're newer and often more flexible than repeaters.
Or you can use the Menu dynamically bound directly to your data.
You can also retrieve the entire menu hierarchy from where it's stored and simply output all the formatting your self.
Adding dynamic controls to a repeater is tricky. I would definitely go for some all-at-once format, as Kevin suggests.
Alternatively, define a reasonable upper maximum for your N levels and delcaratively nest the controls. Do you really need more than THREE menu levels? From a UI perspective, that's a nightmare. ("now WHERE is this tool again??? I just used it 5 minutes ago...")
精彩评论