Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I would like to have a slider content on a vb.net site.
I have set up a repeater which bring me data from the last 5 articles from my database.
date, title, image

I need to show all my last articles but i dont want to be shown in a long list. I would like to have the last 5, and under this list to have
Latest News

id1
id2
id3
id4
id5

"prev" 1,2,3 "next"

so the visitor can see the next 5 articles, etc
I dont want an image slider, I need to bring my data from my database. I dont want to use a datagridvriew. Is that possible to do it in asp.net with vb, ajax? I use visual studio 10, SQL Server 2008.

Thank you
Posted

What you're talking about is simply "paging". The exact same concepts apply to your situation as they do to a DataGridView.

Google results for "ASP.NET Paging"[^]
 
Share this answer
 
Comments
mtouinta 11-Jun-12 10:45am    
Can you see what I have done so far. and if you could help me with the paging?
I use a repeater control inside in upadte panel to bring some data from my db.
ASP.NET
<asp:updatepanel id="UpdatePanel1" runat="server" updatemode="Conditional" xmlns:asp="#unknown">
        <contenttemplate>
    
    <asp:hyperlink id="linkPrev" runat="server"><< </asp:hyperlink>
    <asp:hyperlink id="linkNext" runat="server"> >></asp:hyperlink>
    
            <asp:repeater id="Repeater1" runat="server">
                <itemtemplate>
                    <%#DataBinder.Eval(Container.DataItem, "date")%><br />
                    Title:<br />
                    <%#DataBinder.Eval(Container.DataItem, "title")%>
                    <asp:hyperlink id="lnkDetails" runat="server" navigateurl="<%# Eval("item_ID", "~/Details.aspx?ID={0}") %>">more</asp:hyperlink>
                   
                    <br />
                    <br />
                    <hr width="100px" />
                    <br />
                </itemtemplate>
            </asp:repeater>
            </contenttemplate>

</asp:updatepanel>


In my code behind I have

VB
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim strsql As String = "SELECT  * FROM news ORDER BY news.item_ID DESC"
            Dim sqlconn As New SqlConnection

            sqlconn.ConnectionString = ConfigurationManager.ConnectionStrings("mycon").ToString
            sqlconn.Open()
            Dim cmd As New SqlCommand(strsql, sqlconn)

            Dim da As New SqlDataAdapter(strsql, sqlconn)
            Dim table As New DataTable()
            da.Fill(table)

            Dim pds As New PagedDataSource()
            pds.DataSource = table.DefaultView
            pds.AllowPaging = True
            pds.PageSize = 5

            Dim currentPage As Integer

            If Request.QueryString("page") IsNot Nothing Then
                currentPage = Int32.Parse(Request.QueryString("page"))
            Else
                currentPage = 1
            End If

            pds.CurrentPageIndex = currentPage - 1


            If Not pds.IsFirstPage Then

                linkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" & (currentPage - 1)
            End If

            If Not pds.IsLastPage Then
                linkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" & (currentPage + 1)
            End If

            Repeater1.DataSource = pds
            Repeater1.DataBind()

            cmd.Connection.Close()
            cmd.Connection.Dispose()
        End If
    End Sub


The paging works fine, but it reloads the hole page.
I dont want to use a gridview because I need to display my data in a certain way.

Do I have to use triggers and link buttons? Do you know how I have to transform my code?

thank you
 
Share this answer
 
Hello
I did it with link buttons
so in my markup
I have

<asp:updatepanel id="UpdatePanel1" runat="server" xmlns:asp="#unknown">
                                        <contenttemplate>
                                            <asp:linkbutton id="btnPrev" runat="server" onclick="btnPrev_Click">PrevButton</asp:linkbutton>
                                           <asp:textbox id="txtHidden" style="width: 28px" value="1" runat="server" />
                                            <asp:linkbutton id="btnNext" runat="server" onclick="btnNext_Click">NextButton</asp:linkbutton>
                                            <asp:repeater id="Repeater1" runat="server">
                                                <itemtemplate>
                                                    <div class="latnewstitle">
                                                        Date:</div>
                                                    <%#DataBinder.Eval(Container.DataItem, "date")%><br />
                                                    <div class="latnewstitle">
                                                        Title:</div>
                                                    <div class="latnewscontent">
                                                        <%#DataBinder.Eval(Container.DataItem, "title")%></div>
                                                    <asp:hyperlink id="lnkDetails" runat="server" navigateurl="<%# Eval("item_ID", "~/Details.aspx?ID={0}") %>">See Details</asp:hyperlink>
                                                   
                                                    <br />
                                                    <hr width="100px" />
                                                    <br />
                                                </itemtemplate>
                                            </asp:repeater>
                                            
                                            <br />
                                            <br />
                                        </contenttemplate>
                                    </asp:updatepanel> 




in my code behind


VB
Public Property PgNum() As Integer
        Get
            If ViewState("PgNum") IsNot Nothing Then
                Return Convert.ToInt32(ViewState("PgNum"))
            Else
                Return 0
            End If
        End Get
        Set(value As Integer)
            ViewState("PgNum") = value

        End Set
    End Property

    Protected Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
        bindrepeater()
    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs)
        If Not Page.IsPostBack Then
            bindrepeater()
        End If
    End Sub
    Public Sub bindrepeater()
        Dim cnt As Integer
        Dim strsql As String = "SELECT  * FROM news ORDER BY news.item_ID DESC"
        Dim sqlconn As New SqlConnection
        sqlconn.ConnectionString = ConfigurationManager.ConnectionStrings("mycon").ToString
        sqlconn.Open()
        Dim cmd As New SqlCommand(strsql, sqlconn) 

        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet
        da.Fill(ds)
        cnt = ds.Tables(0).Rows.Count
       
        Dim pds As New PagedDataSource()
        pds.DataSource = ds.Tables(0).DefaultView
        pds.AllowPaging = True
        pds.PageSize = 5
        pds.CurrentPageIndex = PgNum

        txtHidden.Text = PgNum
        Dim vcnt As Integer = cnt / pds.PageSize
        If PgNum < 1 Then
            btnPrev.Visible = False
        ElseIf PgNum > 0 Then
            btnPrev.Visible = True
        End If
        If PgNum = 1 Then
            btnPrev.Visible = False
        End If


        If PgNum = vcnt Then
            btnNext.Visible = False
        ElseIf PgNum < vcnt Then
            btnNext.Visible = True
        End If
        cmd.Connection.Close()
        cmd.Connection.Dispose()
        Repeater1.DataSource = pds
        Repeater1.DataBind()


    End Sub
    Protected Sub btnNext_Click(sender As Object, e As System.EventArgs) Handles btnNext.Click
        PgNum += 1
        bindrepeater()
    End Sub

    Protected Sub btnPrev_Click(sender As Object, e As System.EventArgs) Handles btnPrev.Click
        PgNum -= 1
        bindrepeater()
    End Sub



My problem is that my next button goes always +2 and the previous button -2. I tried without update panel but it same always goes +2 pages.

My code is similar to this
Pagination with Repeater Control[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900