I'm attempting to use databinding expressions (such as <%# PageProperty %>
) within the LayoutTemplate of a listview.
Resources i've looked at:
Access a control inside a the LayoutTemplate of a ListView
Get public string in codebehind into LayoutTemplate of ListView
http://forums.asp.net/p/1201992/3319344.aspx
http://www.codeproject.com/Articles/42962/Databind-Expression-in-ListView-LayoutTemplate
I can successfully get the databinding expressions to work on initial databind using both of the following methods:
protected void lvwExample_OnLayoutCreated(object sender, EventArgs e) {
lvwExample.Controls[0].DataBind();
}
and
protected void lvwExample_OnLayoutCreated(object sender, EventArgs e) {
ListView lv = lvwExample ;
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 problem is that when the listview is rebound (listView.DataBind()
) in a postback the databinding expressions are lost from the layout template.
What were previously databinding expressions are now static text within the template.
it works fine when viewstate is disabled for the listview but viewstate is required in this case to maintain paging and sorting ( i believe that viewstate is required for these but i haven't really looked into it, could be in control state).
What can i do to get this working?