asp.net Repeater控件使用 -开发者知识库

asp.net Repeater控件使用 -开发者知识库,第1张

效果圖

asp.net Repeater控件使用 -开发者知识库,第2张

一、列表頁面

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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <script src="../Scripts/jquery-1.10.2.min.js"></script>
    <title></title>
</head>
<body>
    <p>
      <a href="/Person/Save" style="float:right">添加</a>
    </p>
    <form id="form1" runat="server">
        <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" OnItemCommand="Repeater1_ItemCommand">
            <HeaderTemplate>
                <table class="tb_list">
                    <tr>
                        <th>編號</th>
                        <th>姓名</th>
                        <th>年齡</th>
                        <th>性別</th>
                        <th>操作</th>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td><%#Eval("Id") %></td>
                    <td><%#Eval("UserName") %></td>
                    <td>
                        <asp:Label runat="server" ID="lbAge" Text='<%#Eval("Age") %>' />
                    </td>
                    <td><%#Eval("SexName") %></td>
                    <td>  
                           <a href='/Person/Save.aspx?Id=<%#Eval("Id") %>'>編輯</a>
                        <a  href='/Person/Detail.aspx?Id=<%#Eval("Id") %>'>詳細</a>
                         <asp:LinkButton ID="lbtnDel" OnClientClick="javascript:return confirm('您確定要刪除該項么?')" CommandName="DelPerson" CommandArgument='<%#Eval("Id") %>' runat="server">刪除</asp:LinkButton>
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    </form>
</body>
</html>
<style type="text/css">
    .tb_list {
        border-collapse: collapse;
        width: 100%;
        border: 1px solid black;
    }

        .tb_list th, .tb_list td {
            padding: 10px;
            text-align: center;
            border-bottom: 1px solid black;
        }

    .alternating {
        background-color: silver;
    }
</style>
<script type="text/javascript">
    $(function () {
        $(".tb_list tr:odd").each(function (index, elm) {
            $(elm).addClass("alternating");
        });
    });
</script> 
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1.Person
{
    public partial class PersonList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Repeater1.DataSource = rptDataBind();
                this.Repeater1.DataBind();
            }
        }
        private IList<Person> rptDataBind()
        {
            string sql = "select p.*,s.SexName from tb_Person p left join tb_Sex s on p.Sex=s.Id";
            IList<Person> list = new List<Person>(); 
            using (MSSqlHelper context = new MSSqlHelper())
            {
                SqlDataReader reader = context.GetSqlDataReader(sql); 
                while (reader.Read())
                {
                    Person p = new Person(); 
                    p.Id = Convert.ToInt32(reader["Id"]);
                    p.Age = Convert.ToInt32(reader["Age"]);
                    p.Sex = Convert.ToInt32(reader["Sex"]);
                    p.UserName = reader["UserName"].ToString();
                    p.SexName = reader["SexName"].ToString();
                    list.Add(p);
                }
                reader.Close();
                return list;
            }
        }
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label lbAge = (Label)e.Item.FindControl("lbAge");
                //方式一
                if (Convert.ToInt32( lbAge.Text??"0") > 25)
                {
                    lbAge.ForeColor = Color.Red;
                    lbAge.Style.Add(HtmlTextWriterStyle.FontWeight, "900");
                }
                ////方式二
                //if (((Person)e.Item.DataItem).Age > 25)
                //{
                //    lbAge.ForeColor = Color.Red;
                //    lbAge.Style.Add(HtmlTextWriterStyle.FontWeight, "900");
                //}
            }
        }
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DelPerson")
            {
                string sql = "delete  tb_Person  where Id=@Id"; 
                using (MSSqlHelper context = new MSSqlHelper())
                {
                    context.ExecuteNonQuery(sql, new SqlParameter("@Id", e.CommandArgument));
                    Server.Transfer("/Person/List.aspx");
                }
            }
        }
    }
    class Person
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public int Age { get; set; }
        public int Sex { get; set; }
        public string SexName { get; set; }
    }
}

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复