468,554 Members | 1,946 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,554 developers. It's quick & easy.

How to load a template into datalist at runtime?

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)Load Control(TabsList
[e.Item.ItemIndex].TabTemplate);
template.CourseCategoryIndexChanged += new
CommandEventHandler(template_CourseCategoryIndexCh anged);
template.CourseLevelIndexChanged += new CommandEventHandler
(template_CourseLevelIndexChanged);
panel_selectedTab.Controls.Add((System.Web.UI.User Control)
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.CourseC ategoryCollectio
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.
Dec 29 '05 #1
0 3726

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 this site, you agree to our Privacy Policy and Terms of Use.