Implementation of asp.net Repeater Data Binding

Source: Internet
Author: User

The design steps are as follows:

1. Connect to the database in C. For example:
2. Add a new item to the project, create a dataset, drag Categories from the server resource list to the dataset template, and click "generate-generate solution" in the menu, for example:

3. Put an ObjectDataSource control on aspx's webform and set its TypeName to the dataset type just created. Use its Wizard to create it.
4. Place a Repeater control on the aspx webform and use its Wizard to set its performanceid to the ObjectDataSource above.
5. set its source code on the webpage, that is, add templates such as <itemTemplate> <AlternatingItemTemplate>. The following code:

Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs" Inherits = "Repeater. Default" %>

<! 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>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>

<Asp: ObjectDataSource ID = "objectperformance1" runat = "server"
OldValuesParameterFormatString = "original _ {0}" SelectMethod = "GetData"
TypeName = "Repeater. DataSetEmployeesTableAdapters. CategoriesTableAdapter">
</Asp: ObjectDataSource>
<Asp: Repeater ID = "Repeater1" runat = "server" performanceid = "objectperformance1"
Onitemdatabound = "repeaterincluitemdatabound"
Onitemcreated = "repeaterincluitemcreated">
<HeaderTemplate>
Category Table
<Table border = "1"> <th> category ID </th> <th> category name </th> <th & gt; description </th> <th> image </th>
</HeaderTemplate>
<ItemTemplate>
<Tr>
<Td> <% # Eval ("CategoryID") %> </td>
<Td>
<Asp: TextBox ID = "TextBox1" runat = "server" Text = '<% # Eval ("CategoryName") %>'> </asp: TextBox>
</Td>
<Td> <% # Eval ("Description") %> </td>
<Td> '/> </td>
</Tr>

</ItemTemplate>
<AlternatingItemTemplate>
<Tr>
<Td style = "background-color: Blue"> <% # Eval ("CategoryID") %> </td>
<Td style = "background-color: Blue">
<Asp: TextBox ID = "TextBox1" runat = "server" Text = '<% # Eval ("CategoryName") %>'> </asp: TextBox>
</Td>
<Td style = "background-color: Blue"> <% # Eval ("Description") %> </td>
<Td style = "background-color: Blue"> '/> </td>
</Tr>
</AlternatingItemTemplate>
<FooterTemplate>
</Table>
</FooterTemplate>
</Asp: Repeater>

</Div>
</Form>
</Body>
</Html>


7. If the edit box with "O" in the category name is red, write the following code:

Copy codeThe Code is as follows:
Protected void repeaterincluitemdatabound (object sender, RepeaterItemEventArgs e)
{
If (e. Item. ItemType = ListItemType. AlternatingItem)
{

TextBox tb = (TextBox) e. Item. FindControl ("TextBox1 ");
If (tb. Text. Contains ("o "))
{
Tb. BackColor = Color. Red;
}

}
}


7. The running result is as follows:


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.