Repeater and ListView functions and usage

Source: Internet
Author: User

Repeater

The Repeater (foreach) is used to traverse and display the data in the bound data source in the format. The format of each data entry is determined by the Repeater's <ItemTemplate>, the template is displayed multiple times, just like the statements in foreach and ItemTemplate that are equivalent. <ItemTemplate> Name: <% # Eval ("Name") %> <B> Age: <% # Eval ("Age ") %> </B> <br/> </ItemTemplate>. Note: there must be no spaces between % and.

<% # Eval ("Name") %> indicates that the Name attribute of the current object is displayed at this position. Note that # is used to call the Eval and Bind data binding methods #.

Because Eval displays the attribute to the specified position, it can also display the <ItemTemplate> name in the text box:
<Asp: TextBox runat = "server" Text = '<% # Eval ("Name") %>'/>
</ItemTemplate>

Do not write Text = "<% # Eval ('name') %>" because <%> contains C # code, ''is a character, not a string.

It can also be used in server controls <asp: TextBox Text = '<% # Eval ("Name") %> 'runat = "server"> </asp: TextBox>

DemoCode and notes
Repeater. aspx
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Repeater. aspx. cs" Inherits = "WebForm. Repeater" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Style type = "text/css">
# Tblist {border-top: 1px solid #000; border-left: 1px solid #000; margin: 0px auto; width: 600px ;}
# Tblist td {border-bottom: 1px solid #000; border-right: 1px solid #000; padding: 5px}
# DidPanel {position: absolute; left: 350px; top: 200px; width: 500px; height: 70px; border: 1px solid #000; background-color: Window; padding: 15px; display: none}
</Style>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Asp: ObjectDataSource ID = "objectperformance1" runat = "server"
SelectMethod = "getAllClasses" TypeName = "BLL. Classes">
<SelectParameters>
<Asp: Parameter DefaultValue = "false" Name = "isDel" Type = "Boolean"/>
</SelectParameters>
</Asp: ObjectDataSource>
<Div>
<Table id = "tbList">
<Asp: Repeater ID = "Repeater1" runat = "server" performanceid = "objectperformance1">
<HeaderTemplate> <! -- Header template -->
<Tr>
<Td> ID </td>
<Td> Name </td>
<Td> Count </td>
<Td> Img </td>
<Td> operation </td>
</Tr>
</HeaderTemplate>
<ItemTemplate> <! -- Item template -->
<Tr>
<Td> <input type = "text" value = "<% # Eval (" CID ") %>"/> </td>
<Td>
<Asp: TextBox ID = "TextBox1" runat = "server" Text = '<% # Eval ("CName") %>'> </asp: TextBox> </td>
<Td> <% # Eval ("CCount") %> </td>
<Td>
<% -- -- %>
<! -- To add the images/file path to the image path on the server, Put it after #. If images/'%' causes '%' to be parsed as a string -->
<Asp: Image ID = "Image1" runat = "server" ImageUrl = '<% # "images/" + Eval ("CImg ") %> 'width = "100px" Height = "80px"/>
<! -- Supplement: Generally, the buttons in the template Do not write The OnClick event response, but respond to the ItemCommand event of Repeater. -->
</Td>
</Tr>
</ItemTemplate>
<SeparatorTemplate> <! -- Interval template between two data items -->
<Tr>
<Td colspan = "5" style = "background-color: red; height: 2px; line-height: 3px;"> </td>
</Tr>
</SeparatorTemplate>
<AlternatingItemTemplate> <! -- Alternate item template -->
<Tr style = "background-color: Gray">
<Td> <input type = "text" value = "<% # Eval (" CID ") %>"/> </td>
<Td>
<Asp: TextBox ID = "TextBox1" runat = "server" Text = '<% # Eval ("CName") %>'> </asp: TextBox> </td>
<Td> <% # Eval ("CCount") %> </td>
<Td> <% # Eval ("CImg") %> </td>
<Td>
<Asp: button ID = "btnDel" runat = "server" Text = "delete" OnCommand = "Button_OnClick" CommandName = "Del" CommandArgument = '<% # Eval ("CID ") %> '/>
</Td>
</Tr>
</AlternatingItemTemplate>
<FooterTemplate> <! -- Script Template -->
<Tr>
<Td colspan = "5"> not all consumers are called a hair </td>
</Tr>
</FooterTemplate>

</Asp: Repeater>
</Table>
</Div>
</Form>
</Body>
</Html>

Repeater. aspx. cs
Copy codeThe Code is as follows:
Using System;
Using System. Web. UI. WebControls;
Namespace WebForm {
Public partial class Repeater: System. Web. UI. Page {
Protected void Page_Load (object sender, EventArgs e ){
}
Protected void Button_OnClick (object sender, CommandEventArgs e ){
// Response. Write ("CommandArgument" + e. CommandArgument + "CommandName" + e. CommandName + "deleted" + DateTime. Now); The CommandArgument and CommandName attributes must be set on the foreground.
If (new BLL. Classes (). SoftDel (Convert. ToInt32 (e. CommandArgument)> 0 ){
Response. Write ("deleted successfully ");
Repeater1.DataBind (); // rebind data. Otherwise, the server will not generate Repeater data again, but will return the original data in _ VIEWSTATE.
} Else {
Response. Write ("deletion failed ");
}
}
}
}

:

ListView

Repeater is generally only used to display data. If you want to add, delete, modify, and query (CRUD), ListView is more convenient. Using the Wizard to enable ListView to automatically generate many templates, which saves the trouble of writing the template code. If necessary, manually adjust the template.

Set the data source like Repeater, click "Configure ListView" in the smart prompt, and select a layout and style, select "Enable Edit", "enable Delete", "enable Insert", and "enable pagination" as needed to automatically generate common templates.

For example:

By default, the pagination function of ListView Retrieves all data from the data source and then truncates the part of the current page. When the data size is very large, the efficiency is very low. Therefore, the default pagination function is basically unavailable. Only the data to be displayed is obtained from the data source. For details, see "how to implement efficient paging of ListView" in the next chapter.

For the same content, see the code posted in "how to implement efficient paging of ListView ".

LayoutTemplate is a layout template. The layout template must contain a server control with the ID itemPlaceholder. It is a placeholder for items (not required after FrameWork4.0). Before itemPlaceholder, it is equivalent to HeaderTemplate in Repeater, itemPlaceholder is followed by FooterTemplate in Repeater. Therefore, there are no such templates in ListView.

ItemTemplate is the template of each item, and AlternatingItemTemplate is the template displayed on the interline, which is the same as Repeater.

EmptyDataTemplate indicates the content displayed when the data source does not have data (Insert is also regarded as data). In this way, "No search results" and "sorry, you cannot find the data you are looking ."

InsertItemTemplate is the template for inserting the data interface,

EditItemTemplate is the template for editing data,

SelectedItemTemplate is the template of the row marked as Selected.

For data source configuration, see the data source in Asp. Net in the previous chapter.

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.