Search code examples
c#asp.netlistviewobjectdatasourcefindcontrol

Find ListView, DataPager, ObjectDataSource in Different Content Controls


I find few things as frustrating as trying to find my ASP .NET controls in different content controls. For layout, I need to place a ListView in one content control and a DataPager and ObjectDataSource in a different content control.

<asp:Content ID="cPagedContent" ContentPlaceHolderID="cphPagedContent" runat="Server">
<asp:MultiView ID="mvPagedContent" runat="server">
    <asp:View ID="mvPagedContentDefault" runat="server">
        <asp:ListView ID="lvPagedContent" runat="server" ItemPlaceholderID="itemPlaceholder">
            <LayoutTemplate>
                <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
            </LayoutTemplate>
            <ItemTemplate>
                <div class="hp-post-even">
                    <h2>
                        <a href="<%# Eval("QuickLink") %>">
                            <%# Eval("Title") %></a></h2>
                    <p>
                        <%# Eval("Html") %></p>
                </div>
            </ItemTemplate>
            <AlternatingItemTemplate>
                <div class="hp-post">
                    <h2>
                        <a href="<%# Eval("QuickLink") %>">
                            <%# Eval("Title") %></a></h2>
                    <p>
                        <%# Eval("Html") %></p>
                </div>
            </AlternatingItemTemplate>
        </asp:ListView>
    </asp:View>
    <asp:View ID="mvPagedContentTaxonomy" runat="server">
    </asp:View>
    <asp:View ID="mvPagedContentSearch" runat="server">
    </asp:View>
</asp:MultiView>
</asp:Content>
<asp:Content ID="cAboveFooter" ContentPlaceHolderID="cphAboveFooter" runat="Server">
<asp:MultiView ID="mvPager" runat="server">
    <asp:View ID="mvPagerDefault" runat="server">
        <asp:DataPager ID="dpContentPager" runat="server"
            PageSize="6">
            <Fields>
                <asp:NextPreviousPagerField ButtonType="Link" FirstPageText="[First]" PreviousPageText="[Prev]"
                    ShowNextPageButton="false" ShowLastPageButton="false" RenderDisabledButtonsAsLabels="true"
                    ShowFirstPageButton="true" />
                <asp:NumericPagerField ButtonCount="4" />
                <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="false"
                    NextPageText="[Next]" LastPageText="[Last]" RenderDisabledButtonsAsLabels="true"
                    ShowLastPageButton="true" />
            </Fields>
        </asp:DataPager>
        <asp:ObjectDataSource ID="odsContent" runat="server" EnablePaging="true" MaximumRowsParameterName="maximumRows"
            SelectCountMethod="GetContentCount" SelectMethod="GetContent" StartRowIndexParameterName="startRowIndex" />
    </asp:View>
    <asp:View ID="mvPagerTaxonomy" runat="server">
    </asp:View>
</asp:MultiView>
</asp:Content>

In my C# file, I can locate the ListView control. But I can't seem to find the DataPager and ObjectDataSource to associate with the ListView.

Here is a snippet from Page_Load.

    ListView listView;
    ObjectDataSource objectDataSource;
    DataPager dataPager;

    listView = mvPagedContentDefault.Controls[0].FindControl("lvPagedContent") as  ListView;

    objectDataSource = mvPagerDefault.Controls[0].FindControl("odsContent") as ObjectDataSource;
    objectDataSource.TypeName = this.GetType().AssemblyQualifiedName;
    listView.DataSourceID = objectDataSource.ID;

    dataPager = mvPagerDefault.Controls[0].FindControl("dpContentPager") as DataPager;
    dataPager.PagedControlID = listView.ID;

Any ideas on how I might find these suckers? It's driving me a bit nuts.


Solution

  • I don't understand where the problem is. You have a reference to the MultiView control, haven't you? It has a property Views which returns a collection of View controls in the MultiView control. The DataPager and ObjectDataSource are both in the first view.

    var dpContentPager = (DataPager)mvPager.Views[0].FindControl("dpContentPager");
    var odsContent = (ObjectDataSource)mvPager.Views[0].FindControl("odsContent");
    

    From MSDN:

    Use the PagedControlID property to specify the ID of the control that contains the data that will be paged by the DataPager control. The specified control must implement the IPageableItemContainer interface and must use the same naming container as the DataPager control. An example of a control that you can specify is the ListView control.

    You can try to create a custom pager that inherits from DataPager which overrides FindPageableItemContainer to find the ListView even if it's in a different NamingContainer.

    Have a look here: http://forums.asp.net/t/1458342.aspx/1