Ajax中不要动态加载ListView.LayoutTemplate
Protected Sub Page_InitComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.InitComplete
'这里是动态 LayoutTemplate 的问题说明,供大家讨论
'aspx 页面中没有添加 LayoutTemplate,必须要在此加载 LayoutTemplate。
'发现 ListView 控件内部机制作用仅在此刻初始化,或者后来绑定数据后其头部发生变化(如上次无数据,本次有数据时)
'LayoutTemplate 才引发 InstantiateIn(自定义 MyLayoutTemplate.InstantiateIn),加载 LayoutTemplate 模板
'如果 aspx 页面中添加 LayoutTemplate,也是在此刻加载静态 LayoutTemplate
'ListView 必须要有 LayoutTemplate,如果不添加静态 LayoutTemplate、或不人工加载 LayoutTemplate 将引发错误
'而在 Ajax 应用中,只有在 Page_Load 以后才能得到回传参数,项目需要回传参数来确定加载LayoutTemplate的参数
'但如上所述,LayoutTemplate 不会再度引发 InstantiateIn,造成模板不更新。
'强制调用 InstantiateIn,会出现两个模板,数据只能在第一个我们不需要的模板中呈现,
''lstViewItem.LayoutTemplate = New MyLayoutTemplate(mUnitId)
''lstViewItem.LayoutTemplate.InstantiateIn(lstViewItem) *考虑强制引发 InstantiateIn
''lstViewItem.DataSource = ...
''lstViewItem.DataBind()
'第二个模板空数据紧随其后,完全背离设计初衷,为此只能放弃 Ajax 利用 Request.QueryString 来传递参数
mUnitId = Val(Request.QueryString("id"))
lstViewItem.LayoutTemplate = New MyLayoutTemplate(mUnitId)
End Sub
Public Class MyLayoutTemplate
Implements System.Web.UI.ITemplate
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
......
End Sub
Public Sub New(ByVal typeId As Integer)
......
End Sub
End Class