471,082 Members | 812 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,082 software developers and data experts.

How to load a template into datalist at runtime?

This problem is related to the postback and handling events correctly.
I have a datalist which acts as a tabbes list with horizontal layout.
This datalist is bound to a strogly typed collection I called
TabCollection which is a collevtion of items called tab. Each tab is as


TabTemplate is a string which holds the location of a user control which
inherits from ITemplate. Ideally, each tab instance within
TabCollection would contain a reference to a different template such
that when a user clicks on any of the horizontally aligned tabs, the
datalist would load the corresponding template. Each template is a user
control (.ascx) and contains an assortment of server controls such as
the dropdownlist, listbox, button, texbox, etc.

on Page_Load I am successfully able to load each template, as it is
defined by the instance of tab, int the item selected as the
SelectedItemTemplate. While the page looks nice, and all controls are
visible, invoking any of them that depend upon a postback results in one
of several tested outcomes:

1. The template disappears - this occurs if I select an item is the
drop-down list but do not bubble its event up to the page containing the

2. The dropdown lists contents are reset upon a selection - this occurs
if I do bubble the selectedIndeChanged event up to the page with the
datalist and call ReBind(). The selected template remains visible.
How is it handled that a datalist's SelectedItemTemplate loaded template
is retained while also being able to appropriately use controls within
the custom template?

Though there might be a bit of code posted here, it might be useful for
some even in its current broken state.

Tabs.ascx - this file contains the datalist

private void Page_Load(object sender, System.EventArgs e)
dl_tabs.SelectedIndex = INTIALIZE_TAB_INDEX;

public void BindData()
dl_tabs.DataSource = tabsList;
dl_tabs.DataKeyField = "TabIndex";

private void dl_tabs_ItemDataBound(object sender, DataListItemEventArgs

if(e.Item.ItemType == ListItemType.SelectedItem)
int tabId = (int)dl_tabs.DataKeys[e.Item.ItemIndex];
MyNameSpace.Web.Controls.Templates.ITemplate template =
(MyNameSpace.Web.Controls.Templates.ITemplate)Load Control(TabsList
template.CourseCategoryIndexChanged += new
CommandEventHandler(template_CourseCategoryIndexCh anged);
template.CourseLevelIndexChanged += new CommandEventHandler
panel_selectedTab.Controls.Add((System.Web.UI.User Control)
if(panel_selectedTab.Controls.Count > 1)

private void dl_tabs_ItemCommand(object source, DataListCommandEventArgs
dl_tabs.SelectedIndex = e.Item.ItemIndex;

MyTemplate1.ascx - this file is a user control template that gets loaded
in the dl_tabs_ItemDataBound of the Tabs.ascx datalist event.

// Events
public event CommandEventHandler CourseCategoryIndexChanged;

private void Page_Load(object sender, System.EventArgs e)

public void BindData()
// DropDown Lists
MyNameSpace.DataEntities.CollectionClasses.CourseC ategoryCollectio
n courseCategoryCollection = new CourseCategoryCollection();
courseCategoryCollection = FacultyCourseServer.GetCourseCategories
this.ddl_courseCategory.DataSource = courseCategoryCollection;
this.ddl_courseCategory.DataTextField = EntityFieldFactory.Create
this.ddl_courseCategory.DataValueField = EntityFieldFactory.Create

private void ddl_courseCategory_SelectedIndexChanged(object sender,
System.EventArgs e)
// Do stuff like bubble up event.
if(CourseCategoryIndexChanged != null)
CommandEventArgs ev = new CommandEventArgs(null,
CourseCategoryIndexChanged(this, ev);

If anyone can tackle thi, please feel free to ask if more code is needed
or additional information.

Thank you.
Dec 29 '05 #1
0 3809

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Fabrizio | last post: by
9 posts views Thread by John Kirksey | last post: by
reply views Thread by Barry | last post: by
3 posts views Thread by moondaddy | last post: by
1 post views Thread by DotNetJunkies User | last post: by
reply views Thread by Richard Steele | last post: by
1 post views Thread by Benton | last post: by
3 posts views Thread by Crazy Cat | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.