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.