I've tasked myself with creating a relatively simple user control (a bootstrap nav bar with some extra JS thrown in) just to increase my handle on how such things function. Ideally, it would look as follows:
<abc:NavBar ID="nvbTest" runat="server">
<NavButtons>
<abc:NavButton Target="Target1" />
<abc:NavButton Target="Target2" />
<abc:NavButton Target="Target3" />
</NavButtons>
</abc:NavBar>
I've gotten to the point where I have the <NavButtons> section recognized by the compiler, but can't figure out how to get the inner <abc:NavButton> entries to work. Here's the code so far:
<div id="select" class="row">
<ul class="nav nav-pills nav-justified">
<asp:Literal ID="ltlNavButtons" runat="server" />
</ul>
</div>
[ParseChildren(true),PersistChildren(true),
ToolboxData("<{0}:NavBar runat=server></{0}:NavBar>")]
public partial class NavBar : System.Web.UI.UserControl, INamingContainer {
/// <summary>
/// Private collection of nav buttons.
/// </summary>
private NavButtonCollection _navButtons;
/// <summary>
/// Singleton acquisition of nav button List.
/// </summary>
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public NavButtonCollection NavButtons {
get {
if (_navButtons == null)
_navButtons = new NavButtonCollection();
return
_navButtons;
}
}
protected void Page_Load(object sender, EventArgs e) {
foreach (NavButton nb in NavButtons) {
ltlNavButtons.Text += nb.ToString() + Environment.NewLine;
}
}
}
public class NavButtonCollection : List<NavButton> { }
public class NavButton {
/// <summary>
/// The target string for the nav button
/// </summary>
public string Target { get; set; }
public NavButton() {
Target = string.Empty;
}
public override string ToString() {
return
$@"<li role='presentation'>
<a id='a{Target}' data-toggle='div{Target}' class='btn btn-default'>
<b>{Target}</b>
</a>
</li>";
}
}
I've been through many other answers here to get to this point, but all of the answers were rather old. I'm not sure if I'm hitting dead-ends because those pieces of code just aren't viable any more. I've also been attempting to use the MSDN for this, but the articles I've seen on 'template controls' either don't seem to cover what I'm looking to do, or are so esoteric in their presentation that the information they contain slides off my brain.
A direct answer on how to get the inner section to function would be great, though any direction from here would be very appreciated as well.



