2
我有一個外部列表視圖,並在其itemtemplate裏面有一個內部列表視圖。每個都有自己的SQLDataSource。外部列表視圖中的Id字段用作第二個列表視圖的選擇參數。ASP.NET VB嵌套列表視圖
當使用調試器時,它完美地工作,不幸的是渲染的頁面只包含來自外部列表視圖的數據。在代碼隱藏之前,外部列表視圖的數據綁定會觸發並呈現頁面(包括內部列表視圖),從而可以向第二個列表視圖提供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.
微軟提出的觀點在一箇舊的文章指出:
理論上,您可以攔截父控件 ListView的 ItemDataBound事件,步行通過 控制樹,獲取子控件ListView的引用,並以編程方式將其 綁定到數據。如果你這樣做, 你不會拋出異常,但內部ListView 上的 綁定命令丟失,因爲它觸發太遲而不能 影響渲染。
不幸的是,他們不告訴我如何解決它。
如果有任何天才誰可以告訴我如何得到這個工作,將不勝感激。
輝煌,它的工作的任何代碼。做得好DavRob60。我會把你的名字帶到新年的榮譽 - 騎士好嗎? – CoolestCoder 2011-03-31 14:24:56