In my ascx markup, I have the below code that I am trying to move to the code-behind:
<fs3:LanguageBar CssClass="setIn" ID="languageBar" PostBack="True" runat="server">
<LayoutTemplate>
<fs3:LanguageList ID="languageList" runat="server" CssClass="setIn">
<ItemTemplate>
<asp:HyperLink ID="listItem" CssClass="emptyLang" runat="server" />
</ItemTemplate>
<CurrentItemTemplate>
<asp:Label ID="listItem" CssClass="currentLang" runat="server" />
</CurrentItemTemplate>
<SelectedItemTemplate>
<asp:HyperLink ID="listItem" CssClass="filledLang" runat="server" />
</SelectedItemTemplate>
</fs3:LanguageList>
</LayoutTem开发者_Python百科plate>
</fs3:LanguageBar>
How do I create this programmatically so I can add it to the page?
You would wire up the Control.Init
event to create the controls you need (your LanguageBar
in this case), and then add them to the Control.Controls
collection in the order you wish them to appear.
Then, you would set properties, and add to the appropriate collections what ever belongs in the LanguageBar object itself, too. There will be member properties for the various templates; you'd have to learn specifically about the control's API, but it should be fairly straightforward once you are used to it.
The important thing to keep in mind is that you must re-create these controls on every load of the control - whether postback or not. You probably want a Control-level variable to hold the controls you expect to insert directly into the ascx control itself, so that you can then refer to it in the ascx control's Load and other events.
Working on the templates themselves is a little bit more involved, but here's some info to get you started along those lines generally:
http://iridescence.no/post/Using-Templated-Controls-Programmatically.aspx
http://forums.asp.net/p/1589688/4026373.aspx
http://msdn.microsoft.com/en-us/library/aa289501%28VS.71%29.aspx
精彩评论