Repeater使用方法---基礎數據綁定+多級嵌套


一、基礎數據綁定

  Repeater控件在編譯后不會生成任何多余的代碼,而GridView等編譯后會生成table標簽,這樣對於頁面的負擔和UI樣式影響方面,使用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嵌套使用,使用方法是:
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();
                }
            }
        }

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM