The method of repeater in asp.net

Source: Internet
Author: User

ASP Tutorial method of repeater in. Net

Foreground code:

<asp:repeater id= "Repeater1" runat= "Server" onitemdatabound= "Repeater1_itemdatabound" >
<ItemTemplate>
<div class= "Nav_title" >
<% #Eval ("class")%></div>
<ul class= "NAV" >
<asp:repeater id= "Repeater2" runat= "Server" >
<ItemTemplate>
<li><a class= "<% #Convert. ToInt32 (Eval (" id ")) ==sortid?" Select ":" "%>" href= "line.aspx?sort_id=<% #Eval (" id ")%>" ><% #Eval ("sort")%></a></li>
</ItemTemplate>
</asp:Repeater>
</ul>
</ItemTemplate>
</asp:Repeater>
Background code (repeater is not written, directly to the second):


protected void Repeater1_itemdatabound (object sender, RepeaterItemEventArgs e) {
if (E.item.itemtype = = ListItemType.Item | | e.item.itemtype = = listitemtype.alternatingitem) {
Repeater rptproduct = (Repeater) e.item.findcontrol ("Repeater2");
Find the data item that the taxonomy repeater associated with
DataRowView ROWV = (DataRowView) e.Item.DataItem;
Extract Category ID
String classkey = rowv["Class". ToString ();
According to the classification ID query the products under this category, and binding products repeater
DataSet st = Accesshelper.executedataset ("SELECT DISTINCT [Sort],[id] from [Line_sort] where [class]= @class", Accesshelper.parameter ("@class", ClassKey));
Rptproduct.datasource = St. Tables[0];
Rptproduct.databind ();
}
}


First, the procedure function: realizes the paging for the repeater

Second, form design:

1, new video tutorial ' >asp.net Web application, named Repeater2, save path for Http://192.168.0.1/Repeater2 (note: The IP of the website on my machine is the 192.168.0.1 main directory is d: Web folder) and click OK.

2. Add a 3-row table to the form, add a repeater control to the first row of the table, add two label controls to the second row of the table, and add four button buttons to the third row of the table.

3. Switch to the HTML code window and add the following code between the <asp:repeater id= "Repeater1" runat= "Server" > and </asp:Repeater>:

<ItemTemplate>
<table id= "Table2" style= "Font-size:x-small" width= "498" >
<tr>
<td><% #DataBinder. Eval (Container, "Dataitem.employeeid")%></td>
<td><% #DataBinder. Eval (Container, "Dataitem.lastname")%></td>
</tr>
</table>
</ItemTemplate>

Third, code design:

Imports System.Data.sqlClient
Public Class WebForm1
Inherits System.Web.UI.Page

Dim Scon as New SqlConnection ("server=localhost;database=northwind;uid=sa;pwd=123")
Dim SDA as SqlDataAdapter
Dim DS as DataSet
Dim currentpage as Integer ' records which page is currently on
Dim Maxpage as Integer ' total number of pages
Const RowCount as Integer = 3 ' page How many rows
Dim rowsum as Integer ' total number of rows

' Form code omitted

Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load

If not Page.IsPostBack Then
SDA = New SqlDataAdapter ("SELECT EmployeeID, LastName FROM Employees ORDER by EmployeeID", Scon)
ds = New DataSet
Try
Sda.fill (ds, "Employees")
' Get a total of how many rows
Rowsum = ds. Tables (0). Rows.Count
Catch ex as Exception
Rowsum = 0
End Try

' If there is no data, exit the process
If rowsum = 0 Then Exit Sub
' Calculates the total number of pages to browse the data
If rowsum Mod rowcount > 0 Then
' More than 1 of the surplus
Maxpage = rowsum RowCount + 1
Else
' Just to do
Maxpage = Rowsum RowCount
End If

CurrentPage = 1
' Call the bound data procedure
Readpage (CurrentPage)
Binddata ()
Label2.Text = Maxpage
' Home page and Previous button are not visible
Button1.visible = False
Button2.visible = False
End If
End Sub

' Create a process that binds data
Sub Binddata ()
Repeater1.datasource = ds
Repeater1.databind ()
Label1.Text = CurrentPage
End Sub

' Create a process to populate a dataset
Sub Readpage (ByVal N as Integer)
SDA = New SqlDataAdapter ("SELECT EmployeeID, LastName FROM Employees ORDER by EmployeeID", Scon)
ds = New DataSet
Ds. Clear ()
Sda.fill (ds, (n-1) * rowcount, ROWCOUNT, "employees")
End Sub

' Home button
Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click

CurrentPage = 1
' Call the Fill dataset procedure
Readpage (CurrentPage)
' Bind data
Binddata ()
' Set homepage, first page button not visible, show next Page last button
Button1.visible = False
Button2.visible = False
Button3.visible = True
Button4.visible = True

End Sub

' Previous page ' button
Private Sub button2_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button2.click
' If the page is now the second page, setting the home page and the previous button are not visible
If Label1.Text > 2 Then
Button3.visible = True
Button4.visible = True
Else
Button1.visible = False
Button2.visible = False
Button3.visible = True
Button4.visible = True
End If
CurrentPage = label1.text-1
Readpage (CurrentPage)
Binddata ()
End Sub

' Next page ' button
Private Sub button3_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button3.click
' If the page is now the penultimate page, set the last page and the next page button invisible
If Label1.Text < label2.text-1 Then
Button1.visible = True
Button2.visible = True
Else
Button1.visible = True
Button2.visible = True
Button3.visible = False
Button4.visible = False
End If
CurrentPage = Label1.Text + 1
Readpage (CurrentPage)
Binddata ()
End Sub

' Last button
Private Sub button4_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button4.click
' Set the current page to the maximum number of pages
CurrentPage = Label2.Text
Readpage (CurrentPage)
Binddata ()
Button1.visible = True
Button2.visible = True
Button3.visible = False
Button4.visible = False
End Sub
End Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.