全速加载中

数据绑定控件Reperter

1、  Reperter控件

a)         原理:对数据源数据做foreach进行遍历;

b)        使用前先绑定数据源

c)         ItemTemplate 显示数据

d)        <%#Eval("ID")用来绑定数据字段

e)         HeaderTemplate 头部说明

f)         FooterTemplate 尾部说明

g)        SeparatorTemplate 相隔数据之间显示的内容

h)        AlternatingItemTemplate 使隔行以不同的样式风格进行显示

i)          ItemTemplate

j)          ItemDataBound事件  当每次进行扫描的时候调用,可以用此对生成的数据或者控件进行操作

k)        使用实例:

 

 

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">

    <ItemTemplate >编号<%#Eval("ID") %> 姓名<%#Eval("UserName") %></ItemTemplate>

</asp:Repeater>

asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">

 

    <ItemTemplate >编号<%#Eval("ID") %> 姓名<input type="button"  value="<%#Eval("UserName") %>"/></ItemTemplate>

        <AlternatingItemTemplate >

        <div style=" background-color:Blue">

        编号<%#Eval("ID") %> 姓名<input type="button"  value="<%#Eval("UserName") %>"/>

        </div>

        </AlternatingItemTemplate>

    </asp:Repeater>

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">

    <HeaderTemplate>显示标题</HeaderTemplate>

    <ItemTemplate >编号<%#Eval("ID") %> 姓名<input type="button"  value="<%#Eval("UserName") %>"/></ItemTemplate>

        <AlternatingItemTemplate >

        <div style=" background-color:Blue">

        编号<%#Eval("ID") %> 姓名<input type="button"  value="<%#Eval("UserName") %>"/>

        </div>

        </AlternatingItemTemplate>

        <HeaderTemplate><p>头部</p></HeaderTemplate>

        <FooterTemplate><p>尾部说明</p></FooterTemplate>

        <SeparatorTemplate><hr /></SeparatorTemplate>

    </asp:Repeater>

    </form>

 

ItemDataBound事件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;

namespace WebApplication1
{
    public partial class DateBound : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataRowView dateView = (DataRowView)e.Item.DataItem;
            if (dateView != null)
            {
                var row = (WebApplication1.DateSource.DAL.UserDateSource.UserNameRow)dateView.Row;

                if (row.ID > 3)
                {
                    TextBox txtBox = (TextBox)e.Item.FindControl("TextBox2");
                    txtBox.BackColor = Color.Red;

                }
            }
        }
    }
}

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DateBound.aspx.cs" Inherits="WebApplication1.DateBound" %>

<!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>
    
    </div>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
        InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" 
        SelectMethod="GetData" 
        TypeName="WebApplication1.DateSource.DAL.UserDateSourceTableAdapters.UserNameTableAdapter">
        <InsertParameters>
            <asp:Parameter Name="UserName" Type="String" />
            <asp:Parameter Name="UserPsw" Type="String" />
            <asp:Parameter Name="UserPicturePath" Type="String" />
        </InsertParameters>
    </asp:ObjectDataSource>
    
    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1" 
        onitemdatabound="Repeater1_ItemDataBound">
    <HeaderTemplate><table bgcolor="#00FF99" border="2" frame="box"></HeaderTemplate>
    <ItemTemplate>
    <tbody>
    <tr> <td>用户名:</td><td>
        <asp:TextBox ID="TextBox1" runat="server" Text=' <%#Eval("UserName") %>'></asp:TextBox></td>
        <td>编号:</td><td>
        <asp:TextBox ID="TextBox2" runat="server" Text=' <%#Eval("ID") %>'></asp:TextBox></td>
        </tr>
        </tbody>
    </ItemTemplate>
    <FooterTemplate></table></FooterTemplate>
    
    </asp:Repeater>
    </form>
</body>
</html>

 ItemCommand事件:

aspx页面代码:

 <asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1" 
        onitemdatabound="Repeater1_ItemDataBound" 
        onitemcommand="Repeater1_ItemCommand" >
    <HeaderTemplate><table bgcolor="#00FF99" border="2" frame="box">
    <tr>
    <td>姓名</td><td>成绩</td><td>操作</td>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tbody>
    <tr> <td>
        <asp:TextBox ID="TextBox1" runat="server" Text=' <%#Eval("UserName") %>'></asp:TextBox></td>
        <td>
        <asp:TextBox ID="TextBox2" runat="server" Text=' <%#Eval("UserScore") %>'></asp:TextBox></td>
        <td>
        <asp:Button  Text="增加成绩" runat="server" CommandName="ArgScore" CommandArgument='<%#Eval("ID") %>'/>
        </td>
        </tr>
        </tbody>
    </ItemTemplate>
    <FooterTemplate></table></FooterTemplate>
    

 后台代码:

 protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "ArgScore")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                UserNameTableAdapter adapter = new UserNameTableAdapter();
                adapter.ArcUserScore(id);//操作数据库,对成绩进行改变
                Repeater1.DataBind();//强制刷新数据
            }
        }

 

 

 

说明:

1)        Eval()对数据字段进行绑定

2)        格式可以自己把握,想显示在哪就设置在哪就行了

例如:

asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">

    <ItemTemplate >编号<%#Eval("ID") %> 姓名<input type="button"  value="<%#Eval("UserName") %>"/></ItemTemplate>

    </asp:Repeater>

 

 

posted @ 2012-12-30 10:29  许鸿飞  阅读(682)  评论(0编辑  收藏  举报