在回发时ListView的LayoutTemplate中的数据绑定表达式

时间:2012-11-22 06:45:45

标签: asp.net listview data-binding

我正在尝试在listview的LayoutTemplate中使用数据绑定表达式(例如<%# PageProperty %>)。

资源我看过:

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

我可以使用以下两种方法成功地将数据绑定表达式用于初始数据绑定:

protected void lvwExample_OnLayoutCreated(object sender, EventArgs e) {
       lvwExample.Controls[0].DataBind();          
}

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);           

}

问题是当列表视图在回发中被反弹(listView.DataBind())时,数据绑定表达式将从布局模板中丢失。 之前的数据绑定表达式现在是模板中的静态文本。

当为列表视图禁用viewstate时它工作正常但在这种情况下需要viewstate来维护分页和排序(我相信viewstate是必需的,但我还没有真正研究它,可能处于控制状态)

我能做些什么才能让它发挥作用?

1 个答案:

答案 0 :(得分:2)

我有一个解决方案/解决方法似乎没问题。

我所做的是将布局模板内容包装在2个服务器端控件(占位符)

             <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>

在列表视图上调用databind()后,我在这两个控件(phHeader和phFooter)上调用了databind(),这是listview的FindControl方法。

数据绑定conrols在onLayoutCreated事件处理程序中不起作用。通常它会导致listview的布局模板内容的状态为1回发过期。

我不认为viewstate开启或关闭对此解决方案/解决方法有任何影响。