Add a Repeater control to a Web forms page

Source: Internet
Author: User
Tags bind eval
web| Control

Adding Repeater Web server controls to a page requires several steps. The following procedure describes the minimum steps that a Repeater control must perform to create a work.

Add a Repeater Web server control to a Web Forms page

  1. Adds a data source to a Web forms page. There are several ways to work with data on a page. To select the appropriate method, see Introduction to Ado.net Data access and introduction to data access in WEB forms pages.
  2. In Design view, drag the Repeater control onto the page from the Web Forms tab of the Toolbox.
  3. Sets the DataSource property of the control. In the Properties window, all data sources are displayed in the Drop-down menu for the DataSource property, such as the dataset and DataView controls defined on the page.
  4. Invokes the DataBind method of the control. This is typically done in the Page_Load event of the page using the code shown below, where the Repeater1 control binds to the DataSet object. Because the DataSet is bound to the Repeater control in the designer, it does not need to be bound in that code:
    ' Visual basicprotected Sub Page_Load (ByVal sender as System.Object, _byval e as System.EventArgs) Handles mybase.load
        
          ' Put user code to initialize the page, including data, here.    Repeater1.databind () end sub//c#protected void Page_Load (object sender, EventArgs e) {   //Put user code to initialize T He page, including data, here.   Repeater1.databind ();}
        
  5. Switch to HTML view.
  6. Create a template that contains HTML text and controls within the Repeater element to display the data. For more information, see Web Server Control Templates. The skeleton for the ItemTemplate element might look like this:
    <asp:repeater id=repeater1 runat= "Server" datasource= "<%# dataView1 >" >   <ItemTemplate>   </ItemTemplate></asp:repeater>
  7. Add any combination of HTML elements and Web server controls to the template to display data source information.

    You must bind a control to data by including a data-binding expression. Typically, you bind them to a container control. The ItemTemplate elements that display the "EmployeeName" and "PhoneNumber" fields for each row in the data source will resemble the following:

    <ItemTemplate>   <%# DataBinder.Eval (Container, "Dataitem.employeename")%>   <%# DataBinder.Eval (Container, "Dataitem.phonenumber")%>   <br></ItemTemplate>

    For more information, see Data Access in Web forms pages.

The following example shows how a complete Repeater control definition should look like in HTML view. Specifies that the Repeater control is rendered as a table. <table> elements begin in HeaderTemplate and end in FooterTemplate. (The Task list will report errors about this situation, but these errors can be safely ignored, and the table will be displayed correctly when you run the application.) In the Repeater control body, the table cell is used to display the columns of the data source. The AlternatingItemTemplate element is the same as the ItemTemplate item, except that the table cell has a different background color to create a banded effect.

<asp:repeater id=repeater1 runat= "Server" datasource= "<%# dataview1%>" > <HeaderTemplate> <tabl  E width= "100%" style= "font:8pt Verdana" > <tr style= "background-color:dfa894" > <th> Name      </th> <th> last Name </th> </tr> </HeaderTemplate> <ItemTemplate>         <tr> <td><%# DataBinder.Eval (Container, "Dataitem.employeename")%> </td> <td><%# DataBinder.Eval (Container, "Dataitem.phonenumber")%> </td> </tr> </ itemtemplate> <AlternatingItemTemplate> <tr> <td bgcolor= "LightBlue" > <%# D  Atabinder.eval (Container, "Dataitem.employeename")%> </td> <td bgcolor= "LightBlue" > <%#   DataBinder.Eval (Container, "Dataitem.phonenumber")%> </td> </tr> </AlternatingItemTemplate> <FooterTemplate></table> </FooterTemplate></asp:Repeater> 

The following example uses a Label control to display the value of a database. Use the separator template to create delimiters between values in the list. Note that the value of the Text property of the Label control is enclosed in single quotes. Property values are usually placed in double quotes, but because the DataBinder call contains double quotes, the Text property value must be enclosed in single quotes.

<asp:repeater id= "Repeater1" runat= "Server" datasource= "<%# dataview1%>" >   <ItemTemplate>      <asp:label id=label1 runat= "Server"          Text= ' <%# DataBinder.Eval (Container, "Dataitem.employeename")%> ' >      </asp:Label>   </ Itemtemplate>   <SeparatorTemplate>,</SeparatorTemplate></asp:Repeater>



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.