473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 3913

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Fabrizio | last post by:
Hi, I'm using a datalist with a LoadTemplate method from an ascx(text format) file. Everything is working fine till i need to find a control within the datalist in the datalist_itemdatabound event....
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
by: Barry | last post by:
Hi, I have a template in a DataList. The template also contains a DataList. I'd like to bubble the OnItemCommand event from the template's DataList up the hierarchy. Here's what I've tried so...
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue...
by: moondaddy | last post by:
using vb.net 1.1 I have a datalist and in it's item template I have 2 buttons, 'edit' and 'ShipToThisAddress'. The edit button works fine and it opens up the edit template as it works with the...
by: DotNetJunkies User | last post by:
Hi, I was wondering if it is possible to load datagrid into datalist programatically using ITemplate interface with the following structure and place intp placeholder control. PlaceHolder -...
by: Richard Steele | last post by:
I have a default page that I load usercontrols depending on requirements through a master template using a PlaceHolder i.e. //some logic here..... Control UCUsers =...
by: Benton | last post by:
Hi there, I have a HyperLink control inside a DataList's ItemTemplate: <asp:DataList ID="lstDatos" runat="server"> <ItemTemplate> <asp:HyperLink ID="lnkName" runat="server" CssClass="Link"...
by: Crazy Cat | last post by:
Hi all, I am developing an asp.net 2.0 application in Visual Studio 2005. On my page I have a simple datalist that is bound programmatically to a collection of simple objects. On this page I...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.