Asp. NET Data Binding Repeater Control _ Practical Tips

Source: Internet
Author: User

In the ASP.net learning process, the control of the learning and use of a large part of the article for you to introduce the control Repeater control, use it to bind the background data, and then on the client (browser) on display!
one, Repeater control

1, use: Use the template loop to display data.
2, contains the template:

    • <ItemTemplate></ItemTemplate> project template (the data inside the normal display)
    • <AlternatingItemTemplate></AlternatingItemTemplate> staggered display template (inside the bound data interleaved display) <footertemplate></ Footertemplate> Footer Template (edit footer)
    • <HeaderTemplate></HeaderTemplate> Header Template (edit header)
    • <SeparatorTemplate></SeparatorTemplate> spacer Templates (inserts intervals in the displayed data, such as horizontal lines, special symbols, and so on)

Second, the example

1. Content Introduction
Select the information in the person table in the database and display it on the client with the Repeater control. The following figure is the information in the person table in my Sqlser database.

1. Select the information in the database and bind in the background: Create a new Web Forms application, add a form, and add the following code to the form's Page_Load event.

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, with the Control Repeater template <ItemTemplate></ItemTemplate> Display the 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, <AlternatingItemTemplate></AlternatingItemTemplate> template use (Let Data cross display)

<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> 
  

The display effect is as follows, the structure displays only 2, 4, 6, and 9 columns, which is called cross display.

Finally, I will use five template pieces, the foreground code is as follows

<asp:repeater id= "Repeater1" runat= "Server" > <HeaderTemplate> 

Show the effect as shown below

This is the use of the control of the background database information in the browser to display the method, in fact, not only repeater controls, such as Datalist,gridview,checkboxlist, DropDownList and so on will be able to bind the information in the database and then display it in the browser, I hope to be proficient in these several important controls.

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.