ASP.NET VB嵌套列表视图 [英] ASP.NET VB Nested Listviews

查看:216
本文介绍了ASP.NET VB嵌套列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个外部列表视图和ItemTemplate模板内的内部列表视图。每个人都有自己的SqlDataSource。从outerlistview一个Id字段可以作为第二个列表视图中选择一个参数。

在与它完美的调试器逐句通过,不幸的是,呈现的页面只包含从外部列表视图的数据。它出现在列表视图外火灾的数据绑定和呈现codebehind之前的页面(包括内列表视图),可以提供一个where参数到第二列表视图。

ASPX页面如下。

 <%@页标题=嵌套的ListViewLANGUAGE =VB的MasterPageFile =〜/ MasterPages / SimpleMasterPage.master
    codeFILE =NLV2PA_A.aspx.vb继承=Demos_NLV2PA_A%GT;
< ASP:内容ID =内容1ContentPlaceHolderID =头=服务器>
< / ASP:内容>< ASP:内容ID =内容2ContentPlaceHolderID =ContentPlaceHolder1=服务器>    < ASP:ListView控件ID =ListView1的=服务器ItemPlaceholderID =PLACEHOLDER1的DataKeyNames =ID
        的DataSourceID =SqlDataSource1>        <&LayoutTemplate模板GT;
            < ASP:占位符=服务器ID =PLACEHOLDER1>< / ASP:占位符>
        < / LayoutTemplate模板>
        < EmptyDataTemplate>
            <跨度>的ListView 1无数据返回。 < / SPAN>
        < / EmptyDataTemplate>
        <&ItemTemplate中GT;
            <跨度风格=>编号:
                < ASP:标签ID =IdLabel=服务器文本='<%#的eval(ID)%GT;' />
                < BR />
                名称:
                < ASP:标签ID =NameLabel=服务器文本='<%#的eval(姓名)%>' />
                < BR />
                < BR />
            < / SPAN>
            < ASP:ListView控件ID =ListView2=服务器ItemPlaceholderID =PLACEHOLDER2的DataSourceID =SqlDataSource2>
                <&LayoutTemplate模板GT;
                    < ASP:占位符=服务器ID =PLACEHOLDER2>< / ASP:占位符>
                < / LayoutTemplate模板>
                < EmptyDataTemplate>
                    <跨度> ListView2没有数据返回。 < / SPAN>
                < / EmptyDataTemplate>
                <&ItemTemplate中GT;
                    <跨度风格=>标题:
                        < ASP:标签ID =TitleLabel=服务器文本='<%#的eval(标题)%GT;' />
                        < BR />
                        < H1>
                            <%#的eval(标题)%GT;< / H1>
                        < BR />
                        概要:
                        < ASP:标签ID =SummaryLabel=服务器文本='<%#的eval(摘要)%GT;' />
                        < BR />
                        PhotoAlbumID:
                        < ASP:标签ID =PhotoAlbumIdLabel=服务器文本='<%#的eval(PhotoAlbumId)%>' />
                        < BR />
                        < BR />
                    < / SPAN>
                < / ItemTemplate中>
            < / ASP:的ListView>
        < / ItemTemplate中>
    < / ASP:的ListView>
    < ASP:SqlDataSource的ID =SqlDataSource1=服务器的ConnectionString =<%$的ConnectionStrings:RenaissanceConnectionString1%>中
        的SelectCommand =SELECT [ID],[名] FROM [天地我的相簿] ORDER BY [ID]>< / ASP:SqlDataSource的>
    < ASP:SqlDataSource的ID =SqlDataSource2=服务器的ConnectionString =<%$的ConnectionStrings:RenaissanceConnectionString1%>中
        的SelectCommand =SELECT [标题],[摘要],[PhotoAlbumId] FROM [公寓] WHERE([PhotoAlbumId] = @PAId)>
        < SelectParameters>
            < ASP:ControlParameter控件ID =ListView1的默认值=0NA​​ME =有偿属性名=的SelectedValue
                TYPE =的Int32/>
        < / SelectParameters>
    < / ASP:SqlDataSource的>
< / ASP:内容>

在code的背后,是

 保护小组ListView1_ItemDataBound(BYVAL发件人为对象,BYVAL E上System.Web.UI.WebControls.ListViewItemEventArgs)处理ListView1.ItemDataBound    如果e.Item.ItemType = ListViewItemType.DataItem然后
        SqlDataSource2.SelectParameters(支付)。默认值= DirectCast(e.Item.FindControl(IdLabel),标签)。文本
    万一
结束小组

输出到浏览器是:

 编号:8
名称:第一套照片ListView2返回无数据。编号:9
名称:第二套照片ListView2返回无数据。等等


微软提出的观点在一个古老的一篇文章:


  

您可以在理论上,拦截
  父ItemDataBound事件
  ListView的,走你的方式,通过
  控件树,抢的参考
  孩子的ListView,并将其绑定
  编程数据。如果你这样做,
  你会不会抛出异常,但
  在内部的ListView绑定命令
  丢失,因为它激发来不及
  影响渲染。


不幸的是,他们不告诉我怎么解决它。

如果有任何天才在那里谁可以告诉我怎么得到这个工作,这将是更AP preciated。


解决方案

  1. 把你的 SqlDataSource2 的在的ItemTemplate ListView1的
  2. 里面
  3. 您的 ControlParameter ,将控件ID =IdLabel属性名=TEXT

您不会需要背后

任何code

I have an outer listview and inside its itemtemplate an inner listview. Each has its own SQLDataSource. A Id field from the outerlistview works as a select parameter for the second listview.

When stepping through with the debugger it works perfectly, unfortunately the rendered page only contains data from the outer listview. It appears the databinding of the outer listview fires and renders the page (including the inner listview) before the codebehind can provide a where parameter to the second listview.

The aspx page is below.

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

The code behind is

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

The output to the browser is:

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.


Microsoft make the point in an old article that:

You could, in theory, intercept the ItemDataBound event of the parent ListView, walk your way through the control tree, grab a reference to the child ListView, and bind it programmatically to data. If you do, you won't throw an exception, but the binding command on the inner ListView is lost because it fires too late to affect the rendering.

Unfortunately, they don't tell me how to fix it.

If there is any genius out there who could tell me how to get this to work it would be much appreciated.

解决方案

  1. Put your SqlDataSource2 inside the ItemTemplate of ListView1.
  2. for your ControlParameter, set ControlID="IdLabel" and PropertyName="Text"

You wont need any code behind

这篇关于ASP.NET VB嵌套列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆