• asp.net Repeater控件使用


    效果图

    一、列表页面

    <%@ 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;
             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; }
        }
    }

    二、添加、修改页面

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Save.aspx.cs" Inherits="WebApplication1.Person.SavePerson" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:HiddenField runat="server"  ID="hidId" />
            <p>
                <label>姓名</label><asp:TextBox runat="server" ID="txtUserName"></asp:TextBox>
            </p>
            <p>
                <label>年龄</label>
                <asp:DropDownList ID="ddlAge" runat="server">
                </asp:DropDownList>
            </p>
            <p>
                <label>性别</label><asp:RadioButtonList ID="rbtnListSex" runat="server"></asp:RadioButtonList> 
            </p> 
            <p>
                &nbsp;<asp:Button ID="btnSubmite" runat="server" Text="提交" OnClick="btnSubmite_Click" />
            </p>
        </form>
    </body>
    </html>
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebApplication1.Person
    {
        public partial class SavePerson : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    int id = Convert.ToInt32(Request["Id"] ?? "0");
                    hidId.Value = id+string.Empty; 
                    BindData(id);
                } 
            }
            private void BindData(int id)
            { 
                string sqlPerson = "select Age,Sex,UserName from tb_Person where id=" + id;
                string sqlAge = "select *from tb_Sex";
                DataTable dtAge = new DataTable();
                DataTable dtPerson = new DataTable(); 
                using (MSSqlHelper context = new MSSqlHelper())
                {
                    dtAge = context.ExecuteDataTable(sqlAge);
                    dtPerson = context.ExecuteDataTable(sqlPerson);
                } 
                //年龄绑定
                for (int i = 1; i < 120; i++)
                {
                    ddlAge.Items.Add(new ListItem(i.ToString()));
                } 
                //性别绑定 
                foreach (DataRow row in dtAge.Rows)
                {
                    rbtnListSex.Items.Add(new ListItem(row["SexName"].ToString(), row["Id"].ToString()));
                } 
                foreach (DataRow row in dtPerson.Rows)
                {
                    ddlAge.SelectedIndex = Convert.ToInt32(row["Age"])-1;
                    rbtnListSex.SelectedValue = Convert.ToString(row["Sex"]); 
                    txtUserName.Text =Convert.ToString( row["UserName"]);
                }
            }
    
            protected void btnSubmite_Click(object sender, EventArgs e)
            {
                int id =Convert.ToInt32( this.hidId.Value);
                string sql = "";
                if (id > 0)
                {
                    sql = "UPDATE [Demo].[dbo].[tb_Person] SET[UserName] = @UserName,[Age] = @Age,[Sex] = @Sex WHERE Id=@Id";
                }
                else
                {
                    sql = "INSERT INTO [Demo].[dbo].[tb_Person]([UserName],[Age],[Sex])VALUES(@UserName,@Age,@Sex)";
                }
                SqlParameter[] param = new[] {
                    new SqlParameter("@UserName",this.txtUserName.Text),
                     new SqlParameter("@Age",this.ddlAge.SelectedValue),
                      new SqlParameter("@Sex",this.rbtnListSex.SelectedValue),
                       new SqlParameter("@Id",id),
                }; 
                using (MSSqlHelper contenxt = new MSSqlHelper())
                {
                    contenxt.ExecuteNonQuery(sql, param);
                    Response.Redirect("~/Person/List.aspx"); 
                }
            }
        }
    }

    三、详细页面

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Detail.aspx.cs" Inherits="WebApplication1.Person.PersonDetail" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
    </head>
    <body>
        <p>
            <label>姓名</label><asp:Label ID="lbUserName" runat="server" ></asp:Label>
        </p>
        <p>
            <label>年龄</label><asp:Label ID="lbAge" runat="server" ></asp:Label>
        </p>
        <p>
            <label>性别</label><asp:Label ID="lbSexName" runat="server"></asp:Label>
        </p>
        <p>
            &nbsp;<input type="button" value="返回" onclick="window.location.href='/Person/List.aspx'" />
        </p>
    </body>
    </html>
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebApplication1.Person
    {
        public partial class PersonDetail : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                string sql = "select p.*,s.SexName from tb_Person p left join tb_Sex s on p.Sex=s.Id where p.Id=@Id";
                DataTable dt = new DataTable();
                using (MSSqlHelper context = new MSSqlHelper())
                {
                    dt = context.ExecuteDataTable(sql,new SqlParameter("@Id",Request["Id"]));
                } 
                foreach (DataRow row in dt.Rows)
                {
                    lbAge.Text = Convert.ToString(row["Age"]);
                    lbSexName.Text = Convert.ToString(row["SexName"]);
                    lbUserName.Text = Convert.ToString(row["UserName"]);  
                }
            }
        }
    }
  • 相关阅读:
    ArcGIS Pro二次开发-在地图上插入一个点
    ArcGIS Pro二次开发-获得所有图层
    ArcGIS Pro二次开发-识别工具
    ArcGIS Pro二次开发-放大缩小工具
    ArcGIS Pro二次开发-选择工具
    ArcGIS Pro二次开发计算一个面层的总面积
    arcgis pro二次开发官方文档
    已禁用对分布式事务管理器(MSDTC)的网络访问。请使用组件服务管理工具启用 DTC 以便在 MSDTC 安全配置中进行网络访问。
    Windows把内存变成快速虚拟硬盘
    Ramdisk虚拟内存盘,Swap分区
  • 原文地址:https://www.cnblogs.com/5tomorrow/p/5215067.html
Copyright © 2020-2023  润新知