apply an ItemTemplate dinamically

T

Trapulo

I need to show every row of a datalist with a template, based on the
dataitem's value.
I've tried with this code:
Protected Sub dlZones_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles dlZones.ItemCreated

Select Case e.Item.ItemType

Case ListItemType.Item, ListItemType.AlternatingItem

Select Case DirectCast(e.Item.DataItem,
BusinessEntities.NewsletterZone).Zone.TemplateZone.ClassName

Case BusinessEntities.TemplateZone.InlineImage.ClassName

dlZones.ItemTemplate = Me.LoadTemplate("ItemTemplates/Image.ascx")

dlZones.AlternatingItemTemplate =
Me.LoadTemplate("ItemTemplates/Image.ascx")

Case BusinessEntities.TemplateZone.Html.ClassName

dlZones.ItemTemplate = Me.LoadTemplate("ItemTemplates/Html.ascx")

dlZones.AlternatingItemTemplate = Me.LoadTemplate("ItemTemplates/Html.ascx")

End Select

End Select

End Sub

It works quite good, but the itemplate is applied to the row after the one
that is processing. So, if it's processing a row and the code selects a
template, the next row uses it. How can I change dinamically the template of
every row?

thanks
 
S

Steven Cheng[MSFT]

Hello Trapulo,

Glad to hear from you.

As for the dynamic template for DataList issue you met here, it is due to
the design of the ITemplate property(Column, Fields...) of template
databound control. Such ITemplate property is designed to be configured
statically at design-time or only once (when intializing the columns) at
runtime.

for your scenario, I think the recommended way is to directly load the
usercontrol (no longer use ITemplate) and add the usercontrol into
DataBoundItem (RepeaterItem, DataListItem...)

e.g.

the following "ItemCreated" event of DataList dynamically load a
Usercontrol and add into the DataListItem(row), you can add code logic to
load different controls according to the databound value(or other
criteria). BTW, the "e.Item.DataItem" is only avaiable in the request when
you perform databinding, in other sequential postback request, this
property will be nothing, you need to take care of this.

======================
Protected Sub DataList1_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles
DataList1.ItemCreated

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then

Dim ctrl As Control =
Page.LoadControl("~/usercontrols/usercontrol1.ascx")

e.Item.Controls.Add(ctrl)

End If
=====================

Also, any databinding expression in usercontrol will also be evaluated and
executed even if you dynamically add them into parent control .e.g.

the following expression will also get executed when I add this usercontrol
dynaically into DataListItem as above:

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="usercontrol1.ascx.cs" Inherits="usercontrols_usercontrol1" %>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("CategoryID")
%>'></asp:Label>


In addition, do you think it possible that you use a custom
DataControlField(column) or ITemplate class(dervied one) here instead of
using usercontrol? Sometimes, you can define a custom DataControlField(like
the built-in GridView columns type) and use in databound controls. Here are
some web article and former thread in newsgroup which has mentioned
creating a custom DatacontrolField or ITemplate (for GridView or DataBound
control)

http://www.codeproject.com/useritems/How_to_create_custom_boun.asp

http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet.webc
ontrols/browse_thread/thread/652359e1358ecf6c/834ed1c6afdaa69b

Hope this helps. If there is any further questions or anything else we can
help, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
T

Trapulo

Steven Cheng said:
Hello Trapulo,

Glad to hear from you.

Hello Steven, I'm happy to receive your assistance an other time :)
As for the dynamic template for DataList issue you met here, it is due to
the design of the ITemplate property(Column, Fields...) of template
databound control. Such ITemplate property is designed to be configured
statically at design-time or only once (when intializing the columns) at
runtime.
Ok

for your scenario, I think the recommended way is to directly load the
usercontrol (no longer use ITemplate) and add the usercontrol into
DataBoundItem (RepeaterItem, DataListItem...)

e.g.

the following "ItemCreated" event of DataList dynamically load a
Usercontrol and add into the DataListItem(row), you can add code logic to
load different controls according to the databound value(or other
criteria). BTW, the "e.Item.DataItem" is only avaiable in the request
when
you perform databinding, in other sequential postback request, this
property will be nothing, you need to take care of this.

======================
Protected Sub DataList1_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles
DataList1.ItemCreated

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then

Dim ctrl As Control =
Page.LoadControl("~/usercontrols/usercontrol1.ascx")

e.Item.Controls.Add(ctrl)

End If
=====================

That's very good. I've changed my code to load the control instead of change
the template, and now all woks great.
In addition, do you think it possible that you use a custom
DataControlField(column) or ITemplate class(dervied one) here instead of
using usercontrol?

No: templates are quite complex, and I think that I need to have "clear"
ascx file to manage them. To create an ITemplate class I think will require
a lot of work to design and manage layout's rendering. However the solution
with a control dinamically loaded is great, because databinding is working
well, so it is in pratice as if I have templates separate from the aspx
page.


Thank you!
 
S

Steven Cheng[MSFT]

Thanks for your quick resposne :),

Well, I agree that dynamically loading usercontrol will be the proper
appraoch here since you've complex template based UI.

Have a nice day!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top