asp.net Repeater Control Use example detailed

Source: Internet
Author: User
Tags bind eval html tags


We will use the following XML file ("Cdcatalog.xml") in the example:

The code is as follows Copy Code

<?xml version= "1.0" encoding= "Iso-8859-1"?>

<catalog>
<cd>
<title>empire burlesque</title>
<artist>bob dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>hide your heart</title>
<artist>bonnie tyler</artist>
<country>UK</country>
<company>cbs records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>greatest hits</title>
<artist>dolly parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>still got the Blues</title>
<artist>gary moore</artist>
<country>UK</country>
<company>virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<title>Eros</title>
<artist>eros ramazzotti</artist>
<country>EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
</catalog>

Please view the XML file: Cdcatalog.xml

First, import the "System.Data" namespace. We need this namespace to work with the DataSet object. At the top of the. aspx page, include the following directive:

<%@ Import namespace= "System.Data"%> Next, create a dataset for this XML file, and load the XML file into the dataset when the page is first loaded:

The code is as follows Copy Code
<script runat= "Server" >
Sub Page_Load
If not Page.IsPostBack then
Dim mycdcatalog=new DataSet
Mycdcatalog. READXML (MapPath ("Cdcatalog.xml"))
End If
End Sub

Then we create a Repeater control in the. aspx page. The contents of the <HeaderTemplate> element appear only once in the output, and the contents of the <ItemTemplate> element are repeated to the "record" in the DataSet, and finally, the <footertemplate The content of > appears only once in the output:

The code is as follows Copy Code

<body>

<form runat= "Server" >
<asp:repeater id= "Cdcatalog" runat= "Server" >

<HeaderTemplate>
...
</HeaderTemplate>

<ItemTemplate>
...
</ItemTemplate>

<FooterTemplate>
...
</FooterTemplate>

</asp:Repeater>
</form>

</body>

Then we add the script to create the dataset and bind the Mycdcatalog dataset to the Repeater control. We also populate the Repeater control with HTML tags and bind data items to cells in <ItemTemplate> by <% #Container. DataItem ("fieldname")%> method:

The code is as follows Copy Code

<%@ Import namespace= "System.Data"%>

<script runat= "Server" >
Sub Page_Load
If not Page.IsPostBack then
Dim mycdcatalog=new DataSet
Mycdcatalog. READXML (MapPath ("Cdcatalog.xml"))
Cdcatalog. Datasource=mycdcatalog
Cdcatalog. DataBind ()
End If
End Sub
</script>

<body>

<form runat= "Server" >
<asp:repeater id= "Cdcatalog" runat= "Server" >

<HeaderTemplate>
<table border= "1" width= "100%" >
<tr>
<th>Title</th>
<th>Artist</th>
<th>Country</th>
<th>Company</th>
<th>Price</th>
<th>Year</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td><% #Container. DataItem ("title")%></td>
<td><% #Container. DataItem ("artist")%></td>
<td><% #Container. DataItem ("Country")%></td>
<td><% #Container. DataItem ("Company")%></td>
<td><% #Container. DataItem ("Price")%></td>
<td><% #Container. DataItem ("year")%></td>
</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>
</form>

</body>

Repeater Paging functionality:

Repeater Unlike other data controls that have built-in paging capabilities, if you want to implement paging in repeater, you need pageddatasource to implement pagination.

The foreground code is as follows:
Front Code

The code is as follows Copy Code

<asp:repeater id= "Repeater1" runat= "Server" >
<HeaderTemplate>
<table width= "100%" style= "Background-color: #ADD2DA" >
<tr>
&LT;TD >id</td>
&LT;TD > news name </td>
&LT;TD > Release Time </td>

&LT;TD > Publishers </td>
<td> Publish Status </td>
</tr>

</HeaderTemplate>
<ItemTemplate>
<tr style= "Background-color: #DCDCDC; color: #000000;" >


<TD style= "width: 5%" >
<asp:label id= "Idlabel" runat= "server" text= ' <%# Eval ("ID")%> '/>
</td>

<TD style= "width: 40%" >
<asp:label id= "Titlelabel" runat= "Server" text= "<%#" Eval ("Title")%> '/>
</td>

<TD style= "width: 15%" >
<asp:label id= "Datatimelabel" runat= "server" text= ' <%# Eval ("datatime")%> '/>
</td>


                <td style= " Width: "10%"
                     <asp:label id= "Usernamelabel" runat= "server" text= ' <%# ' Eval ("UserName")%> '/>
                </td>
                <td style= "width:" 10% "
                     <asp:label id= "Label1" runat= "Server" text= "<%#" Eval ("state")%> '/>
  & nbsp;             </td>


</tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>


<table border= "1" width= "100%" >
&LT;TR&GT;&LT;TD align= "center" >
<asp:linkbutton id= "Lbtnfirstpage" runat= "Server" onclick= "Lbtnfirstpage_click" > Page top </asp:LinkButton>
<asp:linkbutton id= "Lbtnpritpage" runat= "Server" onclick= "Lbtnpritpage_click" > Prev </asp:LinkButton>
<asp:linkbutton id= "Lbtnnextpage" runat= "Server" onclick= "Lbtnnextpage_click" > next page </asp:LinkButton>
<asp:linkbutton id= "Lbtndownpage" runat= "Server" onclick= "Lbtndownpage_click" > Footer </asp:LinkButton>< br/>
<asp:label id= "Labpage" runat= "Server" text= "Label" ></asp:Label> page/Total <asp:label id= "Labcountpage" runat= "Server" text= "Label" ></asp:Label>
Page
</td></tr>
</table>

Background code:
Background code

The code is as follows Copy Code

protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
This.labPage.Text = "1";
This.fenye ();
}
}

void Fenye ()
{

Get Data source
PagedDataSource PDS = new PagedDataSource ();
DataTable ds = Getalldata ();
Pds. DataSource = ds. DefaultView;
Set Allow paging
Pds. AllowPaging = true;
Set the number of bars to display on a single page
Pds. PageSize = 5;
Gets or sets the index of the current page
Pds. CurrentPageIndex = Int32.Parse (labpage.text)-1;
Data source for a bound control
Repeater1.datasource = PDS;
Repeater1.databind ();
Set the total number of pages
Labcountpage.text = PDS. Pagecount.tostring ();
To set the operability of a button
Lbtndownpage.enabled = true;
Lbtnfirstpage.enabled = true;
Lbtnnextpage.enabled = true;
Lbtnpritpage.enabled = true;
if (PDS. CurrentPageIndex < 1)
{
lbtnfirstpage.enabled = false;
lbtnpritpage.enabled = false;
}
if (PDS. CurrentPageIndex = PDS. PAGECOUNT-1)
{
lbtndownpage.enabled = false;
lbtnnextpage.enabled = false;
}



}

DataTable Getalldata ()
{

using (SqlConnection cons = new SqlConnection ("server =.; Database = Newsrelease; Integrated SECURITY=SSPI; "))
{
String liketitle = String. Format ("SELECT * from T_news");
SqlDataAdapter da = new SqlDataAdapter (Liketitle, cons);
DataTable ds = new DataTable ();
Da. Fill (DS);
if (ds!= null)
{
return DS;
}
Else
{
return null;
}

}
}

Set Homepage button function
protected void Lbtnfirstpage_click (object sender, EventArgs e)
{
Labpage.text = "1";
This.fenye ();
}
Set the previous Page button function
protected void Lbtnpritpage_click (object sender, EventArgs e)
{
Labpage.text = (Int32.Parse (labpage.text)-1). ToString ();
This.fenye ();
}
Set Next page button function
protected void Lbtnnextpage_click (object sender, EventArgs e)
{
Labpage.text = (Int32.Parse (labpage.text) + 1). ToString ();
This.fenye ();
}
Set Last button function
protected void Lbtndownpage_click (object sender, EventArgs e)
{
Labpage.text = Labcountpage.text;
This.fenye ();
}

How to make repeater data appear horizontally:

Only need to set repeater ItemTemplate binding data in the form of UL, and set its CSS style floating to the left, the code is as follows:
Show Repeater data horizontally

The code is as follows Copy Code
<asp:repeater id= "Repeater1" runat= "Server" datasourceid= "ObjectDataSource1" >
<ItemTemplate>
<a href= "typenews.aspx?type=<% #Eval (" id ")%>" class= "Home >
<ul style= "Float:left;font-size:x-large" >
<li><% #Eval ("TypeName")%>&nbsp</li>
</ul>
</a>
</ItemTemplate>
</asp:Repeater>



Cases. Repeater display of parent-child table data, no flicker

The code is as follows Copy Code

Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;

Namespace Repeatertest
{
public partial class Repeater:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (! Page.IsPostBack)
{
This.bind ();
}
}

Private DataSet DataSource
{
Get
{
if (viewstate["DS"]!= null)
{
Return (DataSet) viewstate["DS"];
}
Else
{
DataSet ds = Datasource.getdataset ();
Viewstate["ds"] = ds;
return DS;
}
}
}

private void Bind ()
{
DataSet ds = This.datasource;

This. Repeater1.datasource = ds. tables["Tbclass"];
This. Repeater1.databind ();

This. Repeater2.datasource = ds. tables["Tbclass"];
This. Repeater2.databind ();
}

protected void Repeater1_itemcommand (object source, RepeaterCommandEventArgs e)
{
if (E.commandname = = "expand")
{
LinkButton btn = (LinkButton) E.commandsource;
GridView Grid = (GridView) btn. Parent.findcontrol ("Grid1");
if (btn. Text = = "+")
{
Btn. Text = "-";
Grid. Visible = true;
}
Else
{
Btn. Text = "+";
Grid. Visible = false;
}
}
}

protected void Repeater1_itemdatabound (object sender, RepeaterItemEventArgs e)
{
DataTable dt = this.datasource.tables["Tbstu"];
LinkButton Objbutton = (LinkButton) e.item.findcontrol ("Btnexpand");
if (Objbutton!= null && objbutton.commandargument!= "")
{
string ClassID = Objbutton.commandargument;
Dt. Defaultview.rowfilter = "classid=" + ClassID;
DataTable dtstu = dt. Defaultview.totable ();

GridView Grid = (GridView) E.item.findcontrol ("Grid1");
Grid. DataSource = Dtstu;
Grid. DataBind ();
}
}

protected void Repeater2_itemdatabound (object sender, RepeaterItemEventArgs e)
{
DataTable dt = this.datasource.tables["Tbstu"];
Label OBJLBL = (label) E.item.findcontrol ("Lblclassid");
if (objlbl!= null && objlbl. Text!= "")
{
String ClassID = Objlbl. Text;
Dt. Defaultview.rowfilter = "classid=" + ClassID;
DataTable dtstu = dt. Defaultview.totable ();

GridView Grid = (GridView) E.item.findcontrol ("Grid2");
Grid. DataSource = Dtstu;
Grid. DataBind ();
}
}
}
}

Related Article

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.