Click here to Skip to main content
Click here to Skip to main content

Databind Expression in ListView LayoutTemplate

By , 10 Oct 2009
 

Introduction

This article pretends to show a possible solution for a common problem when asp.net developers work with ListView: databind expression (<%# %>) in the LayoutTemplate does not work.

My English is not the best so all suggestions and fixes are welcome. 

Solution  

Sometime ago I had to face this problem, I google it and found a possible solution: extend ListView class and do the databind in the CreateLayoutTemplate. In my case was not the best solution because we had a lot of code done and change asp listview tag for the new custom listview tag was simply unacceptable.

I don’t want to extend me (because my english) but basically what I did was use Adapaters. I previously worked with css adapters so I used that code like a base. After some research I found that correct adapter for ListView is DataBoundControlAdapter ( in System.Web.UI.WebControls.Adapters namespace). 

public class ListViewAdapter : DataBoundControlAdapter
{}		

The next that I had to decide was where and how to do the LayoutTemplate DataBind, the first (and the unique) option that I found was LayoutCreated event. Now, I had‘where’ but left how. Checking the ListView inside handler of LayoutCreated event I found that all databind expressions were removed, also I notice that Listview only had one control (the rendered LayoutTemplate) so the solution was instance in a temporal control the LayoutTemplate and then do the databind to it, after that, remove the current rendered layoutTemplate from listview and replace it by the new databound control. 

void AdaptedControl_LayoutCreated(object sender, EventArgs e)
{
    ListView lv = AdaptedControl;
    
    Control template = new Control();
    lv.LayoutTemplate.InstantiateIn(template);
    template.DataBind(); // resolve the problem
    // remove current layout (without databind expressions)
    lv.Controls.RemoveAt(0);
    //add again the layout but with databound content
    lv.Controls.Add(template);
} 

The last step was add the Broswer file to project and the custom adapter for the listview. 

 <adapter controlType="System.Web.UI.WebControls.ListView" adapterType="Adapters.ListViewAdapter" /> 

And that’s all. Now you can use databind expressions (<%# %>) inside LayoutTemplate (obviously no data from datasource).  

History

Creation: 10/10/2009 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

dolcalmi
Colombia Colombia
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionExacto!!memberrcanqui28 Mar '12 - 5:03 
Era justo lo que necesitaba, muchas graciassss
QuestionWhy you need the adapter?memberDavidpw23 Oct '10 - 3:59 
I do not understand why you need the adapter. The only thing I did was hook up the create layout event and it worked fine.
 
This is on Visual Studio 2008 using 3.5.net framework.
 
Thanks for the solution!
GeneralPerfect!memberMR_SAM_PIPER12 May '10 - 16:39 
Exactly the solution I needed - very elegant. Do you know if this is fixed in ListView for ASP.NET 4.0?
 
Thanks!
GeneralRe: Perfect!memberdolcalmi14 May '10 - 10:03 
thanks for the comment, just tested in asp.net 4.0 and <%= %> works but <%# %> does not work.
 
Regards, Juan
GeneralRe: Perfect! [modified]memberRadu Martin28 Jan '11 - 12:48 
just tested right now in asp.net 4.0 and <%# %> does not work, <%= %> throw an exception (Yellow Screen of Death)
 
PS: this solution dont solve binding problem in EmptyDataTemplate

modified on Saturday, January 29, 2011 7:04 AM

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 10 Oct 2009
Article Copyright 2009 by dolcalmi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid