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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • pycharm有中文插件吗_pycharm中英文切换

    pycharm有中文插件吗_pycharm中英文切换打开file——settings——plugins:1-翻译插件(Translation),哈哈,好用吧,再也不用去用词典翻译了,直接在pycharm里就可以翻译安装:效果:2-汉化pycharm,实在不喜欢这些英语的,把它全部汉化安装:效果:(哈哈,还担心英语不好吗)…

    2022年8月26日
    7
  • 安全日志审计系统服务器,日志审计服务器「建议收藏」

    安全日志审计系统服务器,日志审计服务器「建议收藏」日志审计服务器内容精选换一换本地使用远程桌面连接登录Windowsserver2012云服务器,报错:122.112…,服务器频繁掉线,Windows登录进程意外中断。系统资源不足或不可用。服务启动失败。通过VNC方式登录云服务器。单击打开服务管理,选择“管理工具>事件查看器>Windows日志>系统>筛选当前日志”。事件查看器在“事件级别”负载均…

    2022年6月4日
    105
  • 你的windows许可证即将过期需要在设置中激活windows_windows10许可证即将过期

    你的windows许可证即将过期需要在设置中激活windows_windows10许可证即将过期首先:按Win+R,输入slmgr.vbs-dlv或 slmgr/xpr ,回车,查询Windows的激活信息方法一:KMS激活(工具百度一下,自己找)方法二:通过kms

    2022年8月4日
    8
  • python解释器与编译器_pycharm python解释器

    python解释器与编译器_pycharm python解释器第一部分Python语言基础第一节课Python简介以及Python和Pycharm安装和配置知识点:1、Python简介什么是Python?Python的起源和发展。Python的优势。2、为什么要用PythonPython和其它语言有什么不一样的地方。3、Python和Pycharm安装部署(重)内容:1、Python1)什么是Pytho…

    2022年8月27日
    3
  • 浙江python课程_浙江八年级新增Python编程课程!是谁将少儿编程推上风口?

    浙江python课程_浙江八年级新增Python编程课程!是谁将少儿编程推上风口?浙江消息,今年9月份开始的新学期,三到九年级信息技术课将同步替换新器材。其中,八年级将新增Python课程内容。新高一信息技术编程语言由VB替换为Python,大数据、人工智能、程序设计与算法按照教材规划五六年级开始接触。不得不说,在“少儿编程”这条路上,浙江省算是“死磕”到底了。早在2014年,浙江就发布了《浙江省深化高校考试招生制度综合改革试点方案》,方案提到:把信息技术(含编程)正式纳入高考…

    2022年5月17日
    45
  • MIUI解BL锁失败[通俗易懂]

    MIUI解BL锁失败[通俗易懂]最后解决办法是:换USB2.0接口分析问题:或许可能是软件兼容性不好,USB3.0影响读取设备信息,导致无法解锁。

    2022年5月27日
    84

发表回复

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

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