Summary of usage of ASP. NET-----Repeater Data Control (GO)

Source: Internet
Author: User

First, the use of repeater control procedures and examples:

1, first set up a website, create a new Web page index.aspx.

2. Add or create a App_Data data file, and then place the database file used in the App_Data folder.

3. Open Database Enterprise Manager, the database server is local (.), and then attach the database in the App_Data folder to the database server.

4. Add Ling to SQL class.

5. Open the view, Server Explorer, right-click the database server, select Add Connection, then select Database server, database type, and database table, then complete.

6. Select all the tables that you want to use, and then drag them to the. dbml suffix file, and then save. In this step, the data sheet is attached and the connection to the website is completed.

Goal: Make the data in the datasheet appear in the table by using the Repeater data control.

1. Add a style file, and then in the style file, write the style code for the table.

2, in index.aspx design mode, insert the table, usually insert two lines (a behavior header row, a behavior content line), because the Repeater control will automatically loop. Then, in the source code interface, change the cell in the first row of the table you just inserted into the header cell, and change <td> to <th>.

3. Select the table, select the format, and then select Attach Style sheet. Next, you need to remove the style code from the header in the source code, delete the row style, and write the class or ID in the new style sheet to the table.

4. Then, place the cursor in front of the table, double-click the Repeater control so that the Repeater control's code is added to the front of the table code, and then add the header template for the Repeater control (<HeaderTemplate>< /headertemplate>), list templates (<ItemTemplate></ItemTemplate>) and trailer templates (<FooterTemplate> </ footertemplate>).

Attention:

Header template Placement table start and first row header row (<table><tr><th></th></tr>); List template Placement Table second row (<tr></tr>) The trailing template is placed at the end of the table (</table>).

When inserting a table, you can insert only two rows, and the data is displayed in a loop based on the database table. The project template, which is displayed in a loop, places the second row of the table.

5. Then in the cell in the header row, write the alias of the field in the database that will be displayed, and in the cell of the content row, write the name of the field in the database by: <td><% #Eval (database field name)%></td>

The core code is:

<body> <form id= "Form1" runat= "Server" > <div> <!--cursor in front of table, double-click on Repeater control, three <asp:repeater id= "Repeater1" runat= "Server" > <HeaderTemplate><!--Header template, put table start and first row header---&L                    t;table class= "TS" ><!--insert a table with just two rows, and the data is displayed in a loop based on the database table--<tr> <th>                    School Number </th> <th> name </th> <th>                    Gender </th> <th> Native place </th> <th> Age </th> </tr></HeaderTemplate> <ItemTemplate><!--project template, which will be displayed in a loop, placing the second row of the table-  <tr> <td> <% #Eval ("number")%> < inserting additional code into!--HTML requires <% %>, Eval (field name in database)-</td> <td> <% #Eval ("Nam    E ")%> </td>            <td> <% #Eval ("Sex")%> </td> <td>            <% #Eval ("place")%></td> <td> <% #Eval ("Age")%> </td>        </tr> </ItemTemplate> <FooterTemplate><!--Bottom template--</table>                                        < end of!--form-</FooterTemplate> </asp:Repeater> </div> </form></body>

Attention:

Inserting additional code into HTML needs to be enclosed in <%%>.

6. Then bind the data source in the Index.aspx.cs Page_Load () event.

The core code is:

public partial class citynumber:system.web.ui.page{    dataclassesdatacontext dc = new Dataclassesdatacontext ();    protected void Page_Load (object sender, EventArgs e)    {        var query = from C in dc.city select C;        Repeater1.datasource = query;        Repeater1.databind ();    }}

7. Run the Index.aspx page to see the field information in the database.

Second, when you display the fields in the database by table, add hyperlinks to the fields.

1, new two pages, index.aspx page and Cities.aspx page.

Index.aspx page Code:

<body>    <asp:repeater id= "Repeater1" runat= "Server" >    <HeaderTemplate>    <table class= "ts" >        <tr>            <th>                province name </th>            <th>                province number </th>        </ tr>    </HeaderTemplate>    <ItemTemplate>    <tr>            <td>                <a href= ' cities.aspx?pro=<% #Eval ("proid")%> ' target= "_blank" ><% #Eval ("Proname")%></a></td> <!--add hyperlinks, place hyperlinks on both sides of the content-            <td>            <% #Eval ("proid")%></td>        </tr>    </ItemTemplate>    <FooterTemplate>    </table>    </FooterTemplate>    </ asp:repeater>    <form id= "Form1" runat= "Server" >    <div>    </div>    </form ></body>

Code in Index.aspx.cs:

public partial class index:system.web.ui.page{    dataclassesdatacontext dc = new Dataclassesdatacontext ();    protected void Page_Load (object sender, EventArgs e)    {        var query = from C in dc.province select C;        Repeater1.datasource = query;        Repeater1.databind ();    }}

Cities.aspx the code in the page:

<body>    <form id= "Form1" runat= "Server" >    <div>            <asp:gridview id= "GridView1" runat= "Server" cellpadding= "4" forecolor= "#333333"             gridlines= "None" width= "909px" >            <footerstyle backcolor= "#507CD1" font-bold= "True" forecolor= "white"/>            <rowstyle backcolor= "#EFF3FB"/>            <pagerstyle Backcolor= "#2461BF" forecolor= "white" horizontalalign= "Center"/>            <selectedrowstyle backcolor= "#D1DDF1" Font-bold= "true" forecolor= "#333333"/>            

Cities.aspx.cs the code in the page:

public partial class cities:system.web.ui.page{    dataclassesdatacontext dc = new Dataclassesdatacontext ();    protected void Page_Load (object sender, EventArgs e)    {        int  ID =convert.toint32 (request.querystring["Pro" ]. ToString ());        var query = from C in dc.city where c.proid = = ID Select C;        Gridview1.datasource = query;        Gridview1.databind ();    }}

Then run the Index.aspx page and click the hyperlink to jump to cities.aspx, where the information is displayed.

Rotor http://www.cnblogs.com/fengzheng126/archive/2012/05/07/2487355.html

Qinyuanchun www.qinychun.com

Summary of usage of ASP. NET-----Repeater Data Control (GO)

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.