我有一个外部列表视图,在其itemtemplate中有一个内部列表视图。每个都有自己的SQLDataSource。来自outerlistview的Id字段用作第二个listview的select参数。
当使用调试器时,它可以完美地工作,遗憾的是,渲染页面只包含外部列表视图中的数据。看起来外部列表视图的数据绑定触发并在代码隐藏之前呈现页面(包括内部列表视图)可以为第二个列表视图提供where参数。
aspx页面如下。
<%@ Page Title="Nested ListView" Language="VB" MasterPageFile="~/MasterPages/SimpleMasterPage.master"
CodeFile="NLV2PA_A.aspx.vb" Inherits="Demos_NLV2PA_A" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="PlaceHolder1" DataKeyNames="Id"
DataSourceID="SqlDataSource1">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
</LayoutTemplate>
<EmptyDataTemplate>
<span>ListView 1 No data was returned. </span>
</EmptyDataTemplate>
<ItemTemplate>
<span style="">Id:
<asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
<br />
Name:
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
<br />
<br />
</span>
<asp:ListView ID="ListView2" runat="server" ItemPlaceholderID="PlaceHolder2" DataSourceID="SqlDataSource2">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="PlaceHolder2"></asp:PlaceHolder>
</LayoutTemplate>
<EmptyDataTemplate>
<span>ListView2 No data was returned. </span>
</EmptyDataTemplate>
<ItemTemplate>
<span style="">Title:
<asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
<br />
<h1>
<%# Eval("Title") %></h1>
<br />
Summary:
<asp:Label ID="SummaryLabel" runat="server" Text='<%# Eval("Summary") %>' />
<br />
PhotoAlbumID:
<asp:Label ID="PhotoAlbumIdLabel" runat="server" Text='<%# Eval("PhotoAlbumId") %>' />
<br />
<br />
</span>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:RenaissanceConnectionString1 %>"
SelectCommand="SELECT [Id], [Name] FROM [PhotoAlbum] ORDER BY [Id]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:RenaissanceConnectionString1 %>"
SelectCommand="SELECT [Title], [Summary], [PhotoAlbumId] FROM [Apartments] WHERE ([PhotoAlbumId] = @PAId)">
<SelectParameters>
<asp:ControlParameter ControlID="ListView1" DefaultValue="0" Name="PAId" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Content>
背后的代码是
Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView1.ItemDataBound
If e.Item.ItemType = ListViewItemType.DataItem Then
SqlDataSource2.SelectParameters("PAId").DefaultValue = DirectCast(e.Item.FindControl("IdLabel"), Label).Text
End If
End Sub
浏览器的输出是:
Id: 8
Name: First set of Photos
ListView2 No data was returned.
Id: 9
Name: Second set of Photos
ListView2 No data was returned.
etc.
微软在一篇旧文章中指出:
理论上,你可以拦截 父项的ItemDataBound事件 ListView,走过你的路 控制树,抓住一个引用 子ListView,并绑定它 以编程方式处理数据。如果你这样做, 你不会抛出异常,但是 内部ListView上的绑定命令 失去了,因为它太晚了 影响渲染。
不幸的是,他们没有告诉我如何修复它。
如果有任何天才可以告诉我如何让它工作,我将不胜感激。
答案 0 :(得分:4)
你不需要任何代码