问题描述
你好,
我想在vb.net网站上有一个滑块内容.
我设置了一个转发器,将数据库中最近5篇文章中的数据带给我.
日期,标题,图像
我需要显示我所有的最后一篇文章,但我不想在冗长的列表中显示.我想拥有最后5个,并且在此列表下具有
最新新闻
id1
id2
id3
id4
id5
上一个" 1,2,3下一个"
这样访客就可以看到接下来的5篇文章,等等
我不需要图像滑块,我需要从数据库中获取数据.我不想使用datagridvriew.有可能用vb,ajax在asp.net中做到吗?我使用Visual Studio 10,SQL Server2008.
谢谢
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
推荐答案
<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
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
分页工作正常,但它会重新加载孔页面.
我不想使用gridview,因为我需要以某种方式显示数据.
我必须使用触发器和链接按钮吗?您知道我必须如何转换代码吗?
谢谢
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
<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
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
我的问题是我的下一个按钮始终为+2,上一个按钮为-2.我尝试了没有更新面板,但它总是可以显示+2页.
我的代码与此类似
使用中继器控制进行分页 [ ^ ]
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[^]
这篇关于带有Repeater控件的幻灯片内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!