Reputation: 475
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?
Upvotes: 2
Views: 1838
Reputation: 475
I have a solution/workaround that seems to work okay.
What i did was wrap the layout template contents inside of 2 server side controls (placeholders)
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="phHeader">
...
</asp:PlaceHolder>
<asp:PlaceHolder runat="server" ID="itmPlaceholder"></asp:PlaceHolder> <!-- ItemTemplate holder-->
<asp:PlaceHolder runat="server" ID="phFooter">
...
</asp:PlaceHolder>
</LayoutTemplate>
after calling databind()
on the listview i called databind()
on both of these controls (phHeader and phFooter) ising the listview's FindControl
method.
Databinding the conrols did not work in the onLayoutCreated
event handler. Usually it results in the state of the listview's layout template content to be 1 postback out of date.
I don't think viewstate being on or off has any affect on this solution/workaround.
Upvotes: 2