ASP. NET data binding Repeater control, asp. netrepeater

Source: Internet
Author: User

ASP. NET data binding Repeater control, asp. netrepeater

In ASP. NET, the learning and use of its controls account for a large part of the learning process, this article introduces the use of the control Repeater control, use it to bind background data, then displayed on the client (browser!
1. Repeater control

1. Purpose: Use a template to display data cyclically.
2. Included templates:

  • <ItemTemplate> </ItemTemplate> Project template (the data in the template is displayed normally)
  • <AlternatingItemTemplate> </AlternatingItemTemplate> staggered display template (the data bound to it is displayed in an staggered manner) <FooterTemplate> </FooterTemplate> footer template (edit footer)
  • <HeaderTemplate> </HeaderTemplate> header template (edit header)
  • <SeparatorTemplate> </SeparatorTemplate> interval template (insert an interval in the displayed data, such as a horizontal line or special symbol)

Ii. Example

1. Content
Select the information in the Person table in the database and use the Repeater control to display it on the client. Is the information in the person table in my Sqlser database.

1) Select the information in the database and bind it to the background: create a Web form application, add a form, and add the following code in the Page_Load event of the form.

protected void Page_Load(object sender, EventArgs e)  {   SqlConnection con = DB.createConnection();   SqlDataAdapter sda = new SqlDataAdapter();   string sql="select * from person ";   sda.SelectCommand = new SqlCommand(sql, con);   DataSet ds=new DataSet();   sda.Fill(ds, "per");   this.Repeater1.DataSource=ds.Tables["per"];   Repeater1.DataBind();  } 

2) use the Repeater template of the Control <ItemTemplate> </ItemTemplate> to display information. The Code is as follows:

<asp:Repeater ID="Repeater1" runat="server">   <ItemTemplate>    <p align="center">    <%# DataBinder.Eval(Container.DataItem,"pID") %>    <%# DataBinder.Eval(Container.DataItem,"personName") %>    <%# DataBinder.Eval(Container.DataItem,"personSex") %>    </p>   </ItemTemplate>   </asp:Repeater> 

3) The display effect is as follows:

4) use the <AlternatingItemTemplate> </AlternatingItemTemplate> template (cross-display data)

<asp:Repeater ID="Repeater1" runat="server">   <AlternatingItemTemplate>    <p align="center">    <font color="blue"> <%# DataBinder.Eval(Container.DataItem,"pID") %>    <%# DataBinder.Eval(Container.DataItem,"personName") %>    <%# DataBinder.Eval(Container.DataItem,"personSex") %></font>    </p>   </AlternatingItemTemplate>   </asp:Repeater> 

The display effect is as follows. Only 2, 4, 6, and 9 columns are displayed in the structure. This is called cross-display.

Finally, I will use the five Templates One by one. The front-end code is as follows:

<Asp: repeater ID = "Repeater1" runat = "server"> <HeaderTemplate> 

Shown below

This is how to use controls to display information in the background database in a browser. In fact, not only Repeater controls, such as DataList, GridView, checkBoxList, DropDownList, and so on can bind the information in the database and then display it in the browser. We hope to be familiar with these important controls.

Articles you may be interested in:
  • Asp.net Repeater displays parent and child table data without blinking
  • Several Methods for asp.net to traverse controls in repeater
  • Use the AspNetPager paging Control for Repeater in asp.net
  • Asp.net Repeater data binding code
  • JQuery implements Repeater refreshing batch deletion (with the background asp.net source code)
  • Make Repeater and GridView support DataPager paging in asp.net
  • ASP. NET repeater
  • Use of ASP. NET notes Repeater
  • Implementation of asp.net Repeater data binding (graphic description)

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.