Random Rows with the DataPager and ListView?

M

MU

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
 
C

Cowboy \(Gregory A. Beamer\)

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.
 
Joined
Jun 20, 2013
Messages
2
Reaction score
0
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

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


********************************************
| Think outside the box! |
********************************************
"MU" <[email protected]> wrote in message
news:[email protected]...
> 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
 
Joined
Jun 20, 2013
Messages
2
Reaction score
0
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top