Repeater使用方法—基础数据绑定+多级嵌套「建议收藏」

Repeater使用方法—基础数据绑定+多级嵌套「建议收藏」一、基础数据绑定Repeater控件在编译后不会生成任何多余的代码,而GridView等编译后会生成table标签,这样对于页面的负担和UI样式影响方面,使用Repeater就会显得很有优势了。下面

大家好,又见面了,我是你们的朋友全栈君。

一、基础数据绑定

  Repeater控件在编译后不会生成任何多余的代码,而GridView等编译后会生成table标签,这样对于页面的负担和UI样式影响方面,使用Repeater就会显得很有优势了。下面简单说明一下Repeater绑定数据库的方法。

效果图:

Repeater使用方法---基础数据绑定+多级嵌套「建议收藏」

说明:只有男性可以执行删除功能。

前台代码如下:

<head runat="server">
    <title>员工管理</title>
    <link href="staffCSS.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div class="divContent">
        <div class="dtcss">
            <p class="dt_p">
                编号</p>
            <p class="dt_p">
                姓名</p>
            <p class="dt_p">
                性别</p>
            <p class="dt_p">
                部门</p>
            <p class="dt_p">
                操作</p>
        </div>
        <asp:Repeater ID="repStaff" runat="server" OnItemCommand="repStaff_ItemCommand" 
            onitemdatabound="repStaff_ItemDataBound">
            <ItemTemplate>
                    <p class="dt_p">
                        <asp:Label ID="lblID" runat="server" Text='<%#Eval("id")%>'></asp:Label></p>
                    <p class="dt_p">
                        <asp:Label ID="lblName" runat="server" Text='<%#Eval("staffName")%>'></asp:Label></p>
                    <p class="dt_p">
                        <asp:Label ID="lblSex" runat="server" Text='<%#Convert.ToBoolean(Eval("sex"))?"女":"男"%>'></asp:Label></p>
                    <p class="dt_p">
                        <asp:Label ID="lblDepartment" runat="server" Text='<%#Eval("departmentName") %>'></asp:Label></p>
                    <p class="dt_p">
                        <asp:LinkButton ID="update" CommandName="update" runat="server" PostBackUrl='<%#"ModefyStaff.aspx?id="+Eval("staffid")%>'
                            Text="编辑"></asp:LinkButton>
                        <asp:LinkButton ID="delete" CommandName="delete" runat="server" CommandArgument='<%#Eval("staffid") %>'
                            OnClientClick="javascript:return confirm('确认删除此信息吗?')" Text="删除"></asp:LinkButton></p>
               
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>

其中staffCSS.css样式表如下:

body
{
    text-align:center;
    }
.divContent
{
    width:700px;
    text-align:left;
    font-size:12px;
}
.divContent p:hover
{
    background-color:Orange;
}
.dt_p
{
    width:18%;
    float:left;
    height:20px;
}

.dtcss
{
    background-color:Lime;
    width:100%;
    line-height:20px;
    height:20px;
}

后台代码:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindStaff();
            }
        }
        //绑定数据
        private void BindStaff()
        {
            string connstr = "Data Source=.;Initial Catalog=RepeaterSummary;User ID=sa;Password=123456;";
            SqlConnection conn = new SqlConnection(connstr);
            if (conn.State == System.Data.ConnectionState.Closed)
                conn.Open();
            string sqlstr = @"select ROW_NUMBER() over(order by s.id) id,s.id as staffid,s.staffName,s.sex,d.departmentName from Staff s,Department d 
                              where s.departmentid=d.id";
            SqlDataAdapter sda = new SqlDataAdapter(sqlstr, conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            ds.Dispose();
            conn.Close();
            if (ds != null)
            {                
                this.repStaff.DataSource = ds;
                this.repStaff.DataBind();
            }
        }
        //生成事件时触发
        protected void repStaff_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName=="update")
            {
                //跳转至修改页面
            }
            if (e.CommandName == "delete")
            {
                int id =Convert.ToInt32(e.CommandArgument);
                string connstr = "Data Source=.;Initial Catalog=RepeaterSummary;User ID=sa;Password=123456";
                SqlConnection conn = new SqlConnection(connstr);
                if (conn.State==System.Data.ConnectionState.Closed)
                {
                    conn.Open();
                }
                string sqlstr = "delete from Staff where id=" + id;
                SqlCommand comm = new SqlCommand(sqlstr, conn);
                int delInt = comm.ExecuteNonQuery();
                if (delInt > 0)
                    BindStaff();
            }
        }
        //数据绑定时触发
        protected void repStaff_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label lblsex = (Label)e.Item.FindControl("lblSex");
            LinkButton lbupdate = (LinkButton)e.Item.FindControl("update");
            LinkButton lbdelete = (LinkButton)e.Item.FindControl("delete");
            if (lblsex!=null)
            {
                if (lblsex.Text.Trim() == "")
                {
                    lbupdate.Visible = false;
                    //lbdelete.Visible = false;
                }
            }
        }
    }

—————————————————————————————忧郁的分隔符——————————————————————————————————————

二 、多级嵌套

  如果数据展示需要现实父子孙等多级关系,如图:

            Repeater使用方法---基础数据绑定+多级嵌套「建议收藏」

需要两个或多个Repeater嵌套使用,使用方法是:
1. 前台代码定义时,在父Repeater内部定义子Repeater,子Repeater内部定义孙Repeater。如代码:

<!-- 父Repeater开始 -->
                                <asp:Repeater ID="parentRepeater" runat="server" OnItemDataBound="parentRepeater_ItemDataBound">
                                    <ItemTemplate>
                                        <dl id="dlrepeater">
                                            <dt>
                                                <%-- 可以在这里绑定父节点的ID,绑定子Repeater时,需要根据这个ID来查--%>
                                                <asp:HiddenField ID="hfid" runat="server" Value=' <%#Eval("id")%>' />
                                                <asp:CheckBox ID="cbParent" runat="server" Text=' <%#Eval("MenuName")%>' onclick="javascript:FormSelectAll('form1','cbChild',this);" />
                                            </dt>
                                            <!-- 子Repeater开始 -->
                                            <asp:Repeater ID="childRepeater" runat="server" OnItemDataBound="childRepeater_ItemDataBound">
                                                <ItemTemplate>
                                                    <dd>
                                                    <%--同理,绑定子节点的ID,供孙子Repeater查询绑定时用--%>
                                                        <asp:HiddenField ID="hfidchild" runat="server" Value=' <%#Eval("id")%>' />
                                                        <asp:CheckBox name="cbChild" ID="cbChild" runat="server" Text=' <%#Eval("MenuName")%>'
                                                            CssClass="abcd" onclick="javascript:FormSelectAllGrant('form1','cbGrantchild',this);" />
                                                    </dd>
                                                    <!-- 孙Repeater开始 -->
                                                    <asp:Repeater ID="grantchildRepeater" runat="server">
                                                        <ItemTemplate>
                                                            <dd>
                                                                <asp:HiddenField ID="hfidgrantchild" runat="server" Value=' <%#Eval("id")%>' />
                                                                &nbsp;&nbsp;&nbsp;<asp:CheckBox name="cbGrantchild" ID="cbGrantchild" runat="server"
                                                                    Text=' <%#Eval("MenuName")%>' CssClass="abcd" />
                                                            </dd>
                                                        </ItemTemplate>
                                                    </asp:Repeater>
                                                    <!-- 孙Repeater结束 -->
                                                </ItemTemplate>
                                            </asp:Repeater>
                                            <!-- 子Repeater结束 -->
                                        </dl>
                                    </ItemTemplate>
                                </asp:Repeater>
                                <!-- 父Repeater结束 -->

2. 绑定数据时,在父Repeater的ItemDataBound事件中绑定子Repeater,在子Repeater的ItemDataBound事件中绑定孙Repeater。如代码:

//绑定父Repeater时触发
        protected void parentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HiddenField hf = (HiddenField)e.Item.FindControl("hfid");
                Repeater rpchild = (Repeater)e.Item.FindControl("childRepeater");
                if (hf != null || hf.ToString() != "")
                {
                    int id = Convert.ToInt32(hf.Value);
                    rpchild.DataSource = BLLmenu.GetMenuChild(id);//根据父节点id查询子节点
                    rpchild.DataBind();
                }
            }
        }
        //绑定子Repeater时触发
        protected void childRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HiddenField hf = (HiddenField)e.Item.FindControl("hfidchild");
                Repeater rpgrantchild = (Repeater)e.Item.FindControl("grantchildRepeater");
                if (hf != null || hf.ToString() != "")
                {
                    int id = Convert.ToInt32(hf.Value);
                    rpgrantchild.DataSource = BLLmenu.GetMenuChild(id);//根据父节点id查询子节点
                    rpgrantchild.DataBind();
                }
            }
        }

 

 

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/155463.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Windows程序设计:MFC 、Winform 和 WPF 比较[通俗易懂]

    MFC生成本机代码,自然是很快,可是消息循环减缓了界面显示速度。WinForm封装了win32的api,多次进行P/invoke操作(大部分使用p/invoke操作封装),速度慢。WPF是一种新的模型,不再使用win32模型,自己新建模型,使用dx作为新的显示技术,直接访问驱动程序,加快了运行速度,可是,这种模型,需要支持dx9的显卡,硬件要求高(你还能找到现代机器不支持dx9的吗?)…

    2022年4月15日
    51
  • 在定义adt时_ScriptableObject

    在定义adt时_ScriptableObjectADT操作分类Creators构造器:利用其他的数据类型对象产生一个新的对象可能实现为构造函数或静态工厂方法Producers生产器:用已有该类型对象产生新对象如string.concat()(连接两个字符串,产生一个新的字符串)Observers观察器如list.size()返回int(不同于原类型)Mutators变值器(改变对象属性的方法)通常范围void,如果返回void,则必然意味着它改变了某些对象的内部状态,也可能范围非空类型(如容器类的put、add方法)…

    2025年9月4日
    5
  • Gazebo使用笔记(5) —— 力/力矩传感器的使用[通俗易懂]

    Gazebo使用笔记(5) —— 力/力矩传感器的使用[通俗易懂]1.插件的添加在相应的<joint>标签内添加如下代码:<sensorname=”force_torque”type=”force_torque”><update_rate>30</update_rate></sensor>运行world:gazebo–verboseforce_torque_tutorial.world查看传感器输出:法1:命令查看gztopi

    2022年5月15日
    67
  • 项目中常用的MySQL 优化

    项目中常用的MySQL 优化

    2021年7月5日
    84
  • matlab保存图片函数后突变分辨变化,MATLAB总结 – 图片保存「建议收藏」

    matlab保存图片函数后突变分辨变化,MATLAB总结 – 图片保存「建议收藏」I.Matlab中保存图片的方法1.一种是出来图形窗口后手动保存(这儿又可以分两种):1.1直接从菜单保存,有fig,eps,jpeg,gif,png,bmp等格式。1.2edit——〉copyfigure,再粘贴到其他程序。2.另一种是用命令直接保存(这里也有两种):2.1用saveas命令保存图片。saveas的三个参数:(1)图形句柄,如果图形窗口标题栏是“Figure3…

    2025年11月3日
    4
  • OHEM网络

    OHEM网络该网络就是解决fastr-cnn、sppnet等网络在训练过程中,训练样本不均衡的问题。比如可能前景少,背景多。网络结构这个网络相比于fastr-cnn就增加了红色的部分,同时绿色部分最终计算出来的loss不再是用于反向传播,而是寻找hardnegative,下边红色区域计算出来的loss用于反向传播。注意下边红色区域计算loss是利用在绿色区域寻找的hardnegative进行计算的。…

    2022年5月24日
    36

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号