论坛(5)

TopicList.aspx

<%@ Page Language="c#" Inherits="MyBBS.Web.TopicList" CodeFile="TopicList.aspx.cs" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>帖子列表</title>
    <link href="Styles/Style.css" type="text/css" rel="stylesheet" />
    <style type="text/css">
        .style1
        {
            width: 769px;
            height: 12px;
        }
        .style2
        {
            height: 245px;
            width: 769px;
        }
    </style>
</head>
<body>
    <form id="Form1" method="post" runat="server">
        <table id="Table1" style="width: 688px; height: 557px" cellspacing="1"
            cellpadding="1" border="1">
            <tr>
                <td align="center" class="style1">
                    帖子列表</td>
            </tr>
            <tr>
                <td valign="top" align="center" class="style2">
                    &nbsp;<asp:GridView ID="GV" runat="server" AutoGenerateColumns="False" AllowPaging="True"
                        OnPageIndexChanging="GV_PageIndexChanging" OnRowDeleting="GV_RowDeleting" OnRowUpdating="GV_RowUpdating"
                        PageSize="5" OnRowCommand="GV_RowCommand" CellPadding="4" ForeColor="#333333"
                        GridLines="None" AllowSorting="True" Width="690px">
                        <Columns>
                            <asp:BoundField DataField="TopicId" HeaderText="编号" />
                            <asp:BoundField DataField="UserLoginName" HeaderText="用户" />
                            <asp:BoundField DataField="Title" HeaderText="标题" />
                            <asp:BoundField DataField="CreateTime" HeaderText="发表时间" />
                            <asp:ButtonField Text="修改" HeaderText="修改" CommandName="Update" />
                            <asp:HyperLinkField HeaderText="详细信息" DataTextFormatString="详细信息" DataNavigateUrlFormatString="TopicDetail.aspx?topic_id={0}"
                                Text="详细信息" DataNavigateUrlFields="TopicId" />
                            <asp:ButtonField Text="删除" HeaderText="删除" CommandName="Delete" />
                        </Columns>
                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                        <EditRowStyle BackColor="#999999" />
                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    </asp:GridView>
                    <asp:Label ID="LabelPages" runat="server" Width="150px"></asp:Label><asp:HyperLink
                        ID="HyperLinkAddTopic" runat="server" NavigateUrl="TopicAdd.aspx">发表新帖>></asp:HyperLink>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

 

TopicList.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using MyBBS.BusinessLogicLayer;

namespace MyBBS.Web
{
    /// <summary>
    /// TopicList 的摘要说明。
    /// </summary>
    public partial class TopicList : System.Web.UI.Page
    {
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, System.EventArgs e)
        {
            //if (!CheckUser())
            //    Response.Redirect("Login.aspx");

            InitData();
        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
        }
        #endregion

        /// <summary>
        /// 验证用户身份
        /// </summary>
        /// <returns></returns>
        private bool CheckUser()
        {
            if (Session["login_name"] == null)
            {
                Response.Write("<Script Language=JavaScript>alert('请登录!');</Script>");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 按时间降序,读取帖子数据
        /// </summary>
        private void InitData()
        {

            DataSet ds = Topic.QueryTopics();
            GV.DataSource = ds;
            GV.DataBind();
            LabelPages.Text = "查询结果(第" + (GV.PageIndex + 1).ToString() + "页 共" + GV.PageCount.ToString() + "页)";
        }

        /// <summary>
        /// 按钮列单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument); //待处理的行下标
            int topicId = -1;
           
            switch (e.CommandName)
            {
                //修改
                case "Update":
                    topicId = Convert.ToInt32(GV.Rows[index].Cells[0].Text);
                    Response.Redirect("TopicUpdate.aspx?topic_id=" + topicId);
                    break;

                //删除
                case "Delete":
                    topicId = Convert.ToInt32(GV.Rows[index].Cells[0].Text);
                    Topic topic = new Topic();
                    topic.LoadData(topicId);
                    topic.Delete();
                    break;

                default:
                    break;
            }
        }

        /// <summary>
        /// 翻页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GV_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GV.PageIndex = e.NewPageIndex;
            InitData();
        }

        /// <summary>
        /// 删除前事件,检测用户是否有删除该数据的权限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GV_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string userLoginNameOfTopic = GV.Rows[e.RowIndex].Cells[1].Text.ToString(); //UserLoginName
            if (userLoginNameOfTopic == "guest" || userLoginNameOfTopic != Session["login_name"].ToString())
            {
                Response.Write("<Script Language=JavaScript>alert('您无权删除!');</Script>");
                e.Cancel = true;
            }
        }

        /// <summary>
        /// 修改前事件,检测用户是否有修改该数据的权限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GV_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string userLoginNameOfTopic = GV.Rows[e.RowIndex].Cells[1].Text.ToString();
            if (userLoginNameOfTopic == "guest" || userLoginNameOfTopic != Session["login_name"].ToString())
            {
                Response.Write("<Script Language=JavaScript>alert('您无权修改!');</Script>");
                e.Cancel = true;
            }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值