首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

ASP.NET Web Forms 留言板實作(三):留言列表顯示

     編輯:關於C語言

前端頁面 (list.aspx)

<%@ Page Language="c#" Codebehind="list.cs" AutoEventWireup="false" Inherits="notpage.list" %>
<html><head>
<meta name="GENERATOR" content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" content="C#"></head>
<body>
<form method="post" runat="server"></form>
<a href="addTopic.aspx">發表留言</a><br>
<table width="90%" border="0" cellspacing="2" cellpadding="2" id="liuyan">
<tr><td>主題</td><td>留言人</td><td>留言時間</td></tr>
<asp:Label id="n_tdtitle" runat="server"></asp:Label>
</table>
</body></html>

後端程式碼 (list.cs)

namespace notpage
{
    using System;
    using System.Collections;
    using System.Web.UI.WebControls;

    public class list : System.Web.UI.Page
    {
        protected Label n_tdtitle;

        public list()
        {
            Page.Init += Page_Init;
        }

        protected void Page_Load(object sender, EventArgs e) { }

        protected void Page_Init(object sender, EventArgs e)
        {
            InitializeComponent();
            Init_tdtitle();
        }

        private void InitializeComponent()
        {
            this.Load += Page_Load;
        }

        protected void Init_tdtitle()
        {
            try
            {
                notepage np = new notepage();
                ArrayList arrTopic = np.GetTopicList();

                for (int i = 0; i < arrTopic.Count; i++)
                {
                    notepage objTopic = (notepage)arrTopic[i];
                    string str = $"<tr><td><a href='showTopic.aspx?id={objTopic.ID}'>{objTopic.Title}</a></td>";
                    str += $"<td>{objTopic.Author}</td>";
                    str += $"<td>{objTopic.AddDate}</td></tr>";
                    n_tdtitle.Text = str + n_tdtitle.Text;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("取得貼子列表出錯:" + ex.ToString());
            }
        }
    }
}

實作重點

  • 使用 ArrayList 儲存留言清單物件。
  • 在 Page_Init 動態組裝 HTML 字串,輸出至單一 Label 控制項。
  • 每筆留言產生超連結指向 showTopic.aspx?id=編號。
  • 錯誤處理採用 try-catch 並重新拋出例外。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved