Asp.Net实现论坛楼层功能

首先,我们先要在前台的Aspx页面中准备一个Label控件(用于楼层的显示)。

 

 <asp:Label ID="Label2" runat="server" Text="Label" ForeColor="Red"></asp:Label><%--显示第几楼--%>

其次,我们在后台的cs页面中准备一个用于显示楼层的方法ItemDataBound(该事件是DataList绑定一行数据就执行一次)。

 protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            int floorid = 1;//第几楼

            Label Label2 = (Label)e.Item.FindControl("Label2");//先找到用于显示楼层的Label控件

            if (e.Item.ItemIndex != -1)//默认的是-1
            {
                if (this.Label3.Text == "1") //分页的页数(第一页永远是从楼主开始的)
                { 
                    floorid = e.Item.ItemIndex + 1; 
                }
                else
                {
                    floorid = (e.Item.ItemIndex + 1) + ((int.Parse(Label3.Text) - 1) * 6);//每页显示几条数据就乘以几
                }
                if (floorid == 1)
                {
                    Label2.Text = "<a name=\"" + floorid.ToString() + "\"></a><font color=\"red\">楼 主</font>";
                }
                else
                {
                    Label2.Text = "<a name=\"" + floorid.ToString() + "\"></a>第 <font color=\"red\"><b>" + floorid.ToString() + "</b></font> 楼";
                }
            }
        }

写好了之后就可以运行了,运行完了之后就是这个效果,各位刚学习的有兴趣的可以研究研究哦,不懂的给我留言,我会第一时间回复的

 

posted @ 2016-01-21 16:52  丶ScareCrow  阅读(766)  评论(0编辑  收藏  举报