How to load a template into datalist at runtime?

I

Isz

PROBLEM:
This problem is related to the postback and handling events correctly.


BACKGROUND:
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
follows:

tab.TabName
tab.TabTemplate
tab.TabIndex

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.

BEHAVIOR:
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
datalist.

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.


QUESTION:
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?

CODE:
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)
{
if(!Page.IsPostBack)
{
dl_tabs.SelectedIndex = INTIALIZE_TAB_INDEX;
BindData();
}
}

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

private void dl_tabs_ItemDataBound(object sender, DataListItemEventArgs
e)
{

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)LoadControl(TabsList
[e.Item.ItemIndex].TabTemplate);
template.CourseCategoryIndexChanged += new
CommandEventHandler(template_CourseCategoryIndexChanged);
template.CourseLevelIndexChanged += new CommandEventHandler
(template_CourseLevelIndexChanged);
panel_selectedTab.Controls.Add((System.Web.UI.UserControl)
template);
if(panel_selectedTab.Controls.Count > 1)
panel_selectedTab.Controls.RemoveAt(0);
}
}

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

+++++++++++++++++++++++++++++++++++++++++++


+++++++++++++++++++++++++++++++++++++++++++
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)
{
BindData();
}

public void BindData()
{
// DropDown Lists
MyNameSpace.DataEntities.CollectionClasses.CourseCategoryCollectio
n courseCategoryCollection = new CourseCategoryCollection();
courseCategoryCollection = FacultyCourseServer.GetCourseCategories
(FacultyCourseServer.DEFAULT_FACULTY_ID);
this.ddl_courseCategory.DataSource = courseCategoryCollection;
this.ddl_courseCategory.DataTextField = EntityFieldFactory.Create
(CourseCategoryFieldIndex.CourseName).Name;
this.ddl_courseCategory.DataValueField = EntityFieldFactory.Create
(CourseCategoryFieldIndex.CourseCategoryId).Name;
this.ddl_courseCategory.DataBind();
}

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

}
+++++++++++++++++++++++++++++++++++++++++++


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

Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top