asp.net Repeater 資料繫結的具體實現(圖文詳解)

來源:互聯網
上載者:User

以下為設計步驟:

1、在C# 中串連資料庫。如:
2、在項目中添加建立項,建立一個資料集,並把Categories從伺服器資源清單中拖到這個資料集模板中並點擊菜單“產生-產生解決方案”,如:

3、在aspx的webform上放一個ObjectDataSource控制項,設定它的TypeName為剛剛建立的資料集類型,用它的嚮導建立即可。
4、在aspx的webform上放一個Repeater控制項,用它的嚮導設定它的DataSourceID為上面的ObjectDataSource
5、在網頁中設定它的源碼,即加上<itemTemplate><AlternatingItemTemplate>等模板。如下面的代碼:

複製代碼 代碼如下:<%@ 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="ObjectDataSource1" runat="server"
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
            TypeName="Repeater.DataSetEmployeesTableAdapters.CategoriesTableAdapter">
        </asp:ObjectDataSource>
        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1"
            onitemdatabound="Repeater1_ItemDataBound"
            onitemcreated="Repeater1_ItemCreated">
        <HeaderTemplate>
           類別表
           <table border="1"><th>類別ID</th><th>類別名稱</th><th>描述</th><th>圖片</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><img alt="None" src='<%#Eval("Picture")%>' /></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"><img alt="None" src='<%#Eval("Picture")%>' /></td>
           </tr>
        </AlternatingItemTemplate>
        <FooterTemplate>
          </table>
        </FooterTemplate>
        </asp:Repeater>

    </div>
    </form>
</body>
</html>

7、要求在類別名稱中帶"O"的編輯框顯示紅色,則寫出以下代碼:複製代碼 代碼如下:protected void Repeater1_ItemDataBound(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、運行顯示的效果為:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.