Random Rows with the DataPager and ListView?

Discussion in 'ASP .Net' started by MU, Oct 28, 2008.

  1. MU

    MU Guest

    Hello

    I would like to implement a datapager control on my listview and
    return the rows randomly. I am returning the rows with:
    ORDER BY NEWID()
    which gives me randomized rows but when used with the datapager, when
    the user clicks on the Next button, the rows are returned randomized
    again, giving me rows that were on the first page.

    Is there a way to use the original random dataset instead of it
    randomizing again each time the user clicks Next?

    Thanks!
    MU
    MU, Oct 28, 2008
    #1
    1. Advertising

  2. Sure. You cache the dataset you originally retrieved and create a custom
    pager. If you are generating IDs, you will have to make sure those ids are
    added to your data objects (DataSet, LINQ, etc.). There are probably other
    ways to skin this cat, as well.

    --
    Gregory A. Beamer
    MVP, MCP: +I, SE, SD, DBA

    Subscribe to my blog
    http://feeds.feedburner.com/GregoryBeamer#

    or just read it:
    http://feeds.feedburner.com/GregoryBeamer

    ********************************************
    | Think outside the box! |
    ********************************************
    "MU" <> wrote in message
    news:...
    > Hello
    >
    > I would like to implement a datapager control on my listview and
    > return the rows randomly. I am returning the rows with:
    > ORDER BY NEWID()
    > which gives me randomized rows but when used with the datapager, when
    > the user clicks on the Next button, the rows are returned randomized
    > again, giving me rows that were on the first page.
    >
    > Is there a way to use the original random dataset instead of it
    > randomizing again each time the user clicks Next?
    >
    > Thanks!
    > MU
    Cowboy \(Gregory A. Beamer\), Oct 28, 2008
    #2
    1. Advertising

  3. MU

    thealps

    Joined:
    Jun 20, 2013
    Messages:
    2
    Can you provide any sample code for this? im working in C#/asp.net 3.5 and using an UpdatePanel and a SqlDataSource to spew out results in a ListView control with a DataPager control. What should seemingly be a simple task isnt so. I want to randomly show results in the dataset from my sql call with ORDER BY NewID() and then have the pager only page with the results from *that* query and not randomize every next/prev page

    Thanks!

    Chris

    thealps, Jun 20, 2013
    #3
  4. MU

    thealps

    Joined:
    Jun 20, 2013
    Messages:
    2
    Here is the code im working with

    Code:
    <div id="columnsContainer">
        <div class="full-width grey">
        <asp:Panel ID="pnlShowAlbumsHomepage" runat="server" Height="240" Width="960" BackColor="#121B32" BackImageUrl="~/img/bg/bg-fade-drkblue.png" Visible="true">
            <div>
                <div class="blankSeparator4"><!-- --></div>
                <div class="PanelHeader960"><h2 class="LinerNotesHdr">Current releases from BeHip Records</h2></div>
                <div class="clearSep"><!-- --></div>
            <asp:UpdatePanel runat="server" ID="upShowAlbumsHomepage" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:ListView runat="server" ID="lvShowAlbumsHomepage"
                        GroupItemCount="6"
                        DataSourceID="sqlGetCurrentAlbums" DataKeyNames="BHPCollectionID">
                        <LayoutTemplate>
                        <table cellpadding="2" runat="server"
                                id="tblProducts" class="HomePageBoxes">
                            <tr runat="server" id="groupPlaceholder">
                            </tr>
                        </table>
                        <div class="divShowAlbPager">
                            <asp:DataPager runat="server" ID="dpShowAlbumsHomepage" PageSize="6">
                                <Fields>
                                <asp:TemplatePagerField>
                                    <PagerTemplate>
                                        <div class="divShowAlbumsHomepagePager">
                                    &nbsp;
                                    <asp:TextBox ID="CurrentRowTextBox" runat="server"
                                            AutoPostBack="true"
                                            Text="<%# Container.StartRowIndex + 1%>" 
                                            Columns="1" 
                                            style="text-align:right" 
                                            OnTextChanged="CurrentRowTextBox_OnTextChanged" />
                                    to
                                    <asp:Label ID="PageSizeLabel" runat="server" Font-Bold="true"
                                            Text="<%# Container.StartRowIndex + Container.PageSize > Container.TotalRowCount ? Container.TotalRowCount : Container.StartRowIndex + Container.PageSize %>" />
                                    of
                                    <asp:Label ID="TotalRowsLabel" runat="server" Font-Bold="true"
                                            Text="<%# Container.TotalRowCount %>" />
                                            </div>
                                    </PagerTemplate>
                                </asp:TemplatePagerField>
                                <asp:NextPreviousPagerField ButtonCssClass="divShowAlbumsHomepagePager" 
                                        ShowFirstPageButton="true" ShowLastPageButton="true"
                                        FirstPageText="<< " LastPageText=" >>"
                                        NextPageText=" > " PreviousPageText=" < " />
                                </Fields>
                            </asp:DataPager>
                        </div>
                        </LayoutTemplate>
                        <GroupTemplate>
                        <tr runat="server" id="productRow"
                            style="height:150px">
                            <td runat="server" id="itemPlaceholder">
                            </td>
                        </tr>
                        </GroupTemplate>
                        <ItemTemplate>
                        <td id="Td1" valign="top" align="center" style="width:150px" runat="server" class="HomePageBoxes">
                            <a href='<%# Page.ResolveClientUrl("~/") + Eval("URLBeHipProfile")%>'><asp:Image ID="imgAlbumImage" runat="server"
                                ImageUrl='<%#"~/img/release-covers/" + Eval("BHPCollectionID") + "_127x127.png" %>'
                                Height="127"  class="poshytip" title='<% #Eval("CollectionName") %>' /></a><br />
                            <asp:HyperLink ID='hlAlbumLink' runat='server' Text='<% #Eval("CollectionName") %>'
                                NavigateUrl='<%#"~/" + Eval("URLBeHipProfile") %>' />
                        </td>
                        </ItemTemplate>
                    </asp:ListView>
                    <asp:SqlDataSource ID="sqlGetCurrentAlbums" Runat="server" 
    		            SelectCommand="SELECT * FROM BHPCollections WHERE (isActive = '1') AND (isLive = '1') ORDER BY NewID() "
    		            ConnectionString="<%$ ConnectionStrings:conBHP %>">
                    </asp:SqlDataSource>
                 </ContentTemplate>
    
            </asp:UpdatePanel>
                </div>
            </asp:Panel>
            <asp:RoundedCornersExtender ID="rceShowAlbumsHomepage" runat="server" TargetControlID="pnlShowAlbumsHomepage" Radius="10" Corners="ALL" />
        </div>
        </div>
    thealps, Jun 20, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Olbert

    DataPager and ListView: Custom Datasource

    Mark Olbert, Jan 5, 2008, in forum: ASP .Net
    Replies:
    5
    Views:
    5,678
    Walter Wang [MSFT]
    Jan 9, 2008
  2. MU
    Replies:
    0
    Views:
    369
  3. .nLL
    Replies:
    1
    Views:
    1,026
  4. .
    Replies:
    0
    Views:
    720
  5. VK
    Replies:
    15
    Views:
    1,159
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page