The previous study of HTML static Web programming, understand some of the syntax, but I feel that the Web programming mastery is not enough, so follow the plan, began the journey of ASP. Before I write this ASP, I would like to first compare the difference between ASP and HTML.
One, the connection and the difference:HTML is in the client programming, usually generates static Web pages, and ASP. NET is programmed on the server side, usually to generate dynamic Web pages. Asp. NET controls are redesigned and encapsulated by controls in HTML, which means that the controls in the ASP are based on controls in HTML. The ASP . NET control has the loopback function, enough viewstate to maintain the state of the control, the HTML control can not, when clicked on the operation of the page, its state will be lost.
In the learning process of ASP, the learning and use of its control is a big part, today, I say the use of the control Repeater control, use it to bind the background data, and then display it on the client (browser)!
Second, Repeater control
1. Usage: Use the template to cycle through the data.
2. Included templates:
<ItemTemplate></ItemTemplate> project template (The data inside is displayed correctly)
<AlternatingItemTemplate></AlternatingItemTemplate> Staggered display template (the data bound in it is interlaced with the display) <FooterTemplate></FooterTemplate> Footer Template (edit footer)
<HeaderTemplate></HeaderTemplate> Header template (edit header)
<SeparatorTemplate></SeparatorTemplate> interval templates (insert intervals in the displayed data, such as dashes, special symbols , and so on)
Iii. examples
I write an instance of the vs2012 ASP. NET Web Forms application.
1. Content Introduction
Select the information from the person table in the database, and then 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 in the background: Create a new Web Forms application, add a form, and add the following code to 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 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: Show the following effect
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> </asp:Repeater>
The display works as follows, the structure shows only 2, 4, 6, 9 columns, which is called cross-display.
Finally, I use five templates, the foreground code is as follows
<asp:repeater id= "Repeater1" runat= "Server" > <HeaderTemplate>
Shown below
This is the method of using a control to display information in a background database in a browser, not only repeater controls, like 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 will introduce each, please look forward to!!