Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Paging Datalist

Status
Not open for further replies.

primagic

IS-IT--Management
Jul 24, 2008
476
GB
I have the following code generating a datalist:

Code:
<asp:DataList ID="DataList1" runat="server" CssClass="offers-table"
                                DataKeyField="OfferID" DataSourceID="GetNonExclusiveOffers" RepeatColumns="3" RepeatDirection="Horizontal" >
                            <ItemTemplate>
                                <img src= '<%#DataBinder.Eval(Container.DataItem, "OfferImage") %>' style="height:105px;width:150px;"/>
                                <br></br>
                                <br>
                                </br>
                                Started:
                                <asp:Label ID="OfferPostedLabel" runat="server" 
                                    Text='<%# Eval("OfferPosted","{0:dd/MM/yyyy}") %>' />
                                <br />
                                Ends:
                                <asp:Label ID="OfferEndsLabel" runat="server" Text='<%# Eval("OfferEnds","{0:dd/MM/yyyy}") %>' />
                                <br />
                                
                            </ItemTemplate>
                            </asp:DataList>
And the datasource for the datalist
Code:
<asp:SqlDataSource ID="GetNonExclusiveOffers" runat="server" 
                                ConnectionString="<%$ ConnectionStrings:StudyBuddyConnectionString %>" 
                                SelectCommand="SELECT [OfferID], [OfferTitle], [OfferPosted], [OfferEnds], [ExclusiveOffer], [OfferImage] FROM [Offers] WHERE ([ExclusiveOffer] = @ExclusiveOffer)">
                                <SelectParameters>
                                    <asp:Parameter DefaultValue="False" Name="ExclusiveOffer" Type="Boolean" />
                                </SelectParameters>
                            </asp:SqlDataSource>

In the codebehind I have the following to create the pages:
Code:
Imports System.Data.SqlClient
Imports System.Data


Partial Class index
    Inherits System.Web.UI.Page

    Protected lblCounts As System.Web.UI.WebControls.Label

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.pnlMessageSent.Visible = False

        If Not Page.IsPostBack() Then
            BindTheData()
        End If

    End Sub

    Private Sub BindTheData()

        Dim ConnString As [String] = ConfigurationManager.ConnectionStrings("StudyBuddyConnectionString").ConnectionString
        Dim objConn As New SqlConnection(ConnString)
        Dim strSql As String = "SELECT CompanyName, ContactName, ContactTitle, Country, Phone FROM Customers"
        Dim dataAdapter As New SqlDataAdapter(strSql, objConn)
        Dim dataSet As New DataSet()
        If Not Page.IsPostBack() Then
            dataAdapter.Fill(dataSet)
            lblRecordCount.Text = CStr(dataSet.Tables(0).Rows.Count)
            dataSet = Nothing
            dataSet = New DataSet()
        End If
        dataAdapter.Fill(dataSet, CInt(lblCurrentIndex.Text), CInt(lblPageSize.Text), "Customers")
        DataList1.DataSource = dataSet.Tables("Customers").DefaultView
        DataList1.DataBind()
        objConn.Close()
        ShowCounts()
    End Sub

    Public Sub ShowFirstPage(ByVal s As System.Object, ByVal e As System.EventArgs)
        lblCurrentIndex.Text = "0"
        BindTheData()
    End Sub

    Public Sub ShowPreviousPage(ByVal s As System.Object, ByVal e As System.EventArgs)
        lblCurrentIndex.Text = CStr(CInt(lblCurrentIndex.Text) - CInt(lblPageSize.Text))
        If CInt(lblCurrentIndex.Text) < 0 Then
            lblCurrentIndex.Text = "0"
        End If
        BindTheData()
    End Sub

    Public Sub ShowNextPage(ByVal s As System.Object, ByVal e As System.EventArgs)
        If CInt(CInt(lblCurrentIndex.Text) + CInt(lblPageSize.Text)) < CInt(lblRecordCount.Text) Then
            lblCurrentIndex.Text = CStr(CInt(lblCurrentIndex.Text) + CInt(lblPageSize.Text))
        End If
        BindTheData()
    End Sub

    Public Sub ShowLastPage(ByVal s As System.Object, ByVal e As System.EventArgs)
        Dim intMod As Integer
        intMod = CInt(lblRecordCount.Text) Mod CInt(lblPageSize.Text)
        If intMod > 0 Then
            lblCurrentIndex.Text = CStr(CInt(lblRecordCount.Text) - intMod)
        Else
            lblCurrentIndex.Text = CStr(CInt(lblRecordCount.Text) - CInt(lblPageSize.Text))
        End If
        BindTheData()
    End Sub

    Private Sub ShowCounts()
        lblCounts.Text = "|Total Rows: <b>" & lblRecordCount.Text
        lblCounts.Text += "</b> | Page:<b> "
        lblCounts.Text += CStr(CInt(CInt(lblCurrentIndex.Text) / CInt(lblPageSize.Text) + 1))
        lblCounts.Text += "</b> of <b>"
        If (CInt(lblRecordCount.Text) Mod CInt(lblPageSize.Text)) > 0 Then
            lblCounts.Text += CStr(Fix(CInt(lblRecordCount.Text) / CInt(lblPageSize.Text) + 1))
        Else
            lblCounts.Text += CStr(Fix(lblRecordCount.Text) / CInt(lblPageSize.Text))
        End If
        lblCounts.Text += "</b> |"
    End Sub
End Class

What I want to know is how do I modify these lines:

Code:
Private Sub BindTheData()

        Dim ConnString As [String] = ConfigurationManager.ConnectionStrings("StudyBuddyConnectionString").ConnectionString
        Dim objConn As New SqlConnection(ConnString)
        Dim strSql As String = "SELECT CompanyName, ContactName, ContactTitle, Country, Phone FROM Customers"
        Dim dataAdapter As New SqlDataAdapter(strSql, objConn)
        Dim dataSet As New DataSet()
        If Not Page.IsPostBack() Then
            dataAdapter.Fill(dataSet)
            lblRecordCount.Text = CStr(dataSet.Tables(0).Rows.Count)
            dataSet = Nothing
            dataSet = New DataSet()
        End If
        dataAdapter.Fill(dataSet, CInt(lblCurrentIndex.Text), CInt(lblPageSize.Text), "Customers")
        DataList1.DataSource = dataSet.Tables("Customers").DefaultView
        DataList1.DataBind()
        objConn.Close()
        ShowCounts()
    End Sub

To use the datasource specified rather than a direct sql statement.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top