A simple BBS demo including(CRUD) - 4

本文介绍如何使用ASP.NET WebForms创建一个简单的公告板系统,包括编写Insert.aspx页面及其后台代码Insert.aspx.cs,实现文章发布功能。该系统允许用户输入标题、作者及内容,并进行有效性验证。

7. Create a webform named "Insert" in the website.

Insert.aspx

=========

<%@ Page Language="C#" Debug="true" AutoEventWireup="true" CodeFile="Insert.aspx.cs" Inherits="Insert" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Insert</title>
    <style type="text/css">
        .style1 {
            height: 235px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td>Title: </td>
            <td><asp:TextBox ID="txtTitle" runat="server" Width="327px"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RfvTxtTitle" runat="server"
                    ControlToValidate="txtTitle" ErrorMessage="*"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>Writer: </td>
            <td><asp:TextBox ID="txtWriter" runat="server" Width="327px"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RfvTxtWriter" runat="server"
                    ControlToValidate="txtWriter" ErrorMessage="*"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="style1">Content: </td>
            <td class="style1"><asp:TextBox ID="txtContent" runat="server" Height="227px" TextMode="MultiLine"
                    Width="327px"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RfvTxtContent" runat="server"
                    ControlToValidate="txtContent" ErrorMessage="*"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td colspan="2"><asp:Label ID="lblMsg" runat="server" Visible="false" ForeColor="Red"></asp:Label></td>
        </tr>
    </table>
    <p><asp:Button ID="btnInsert" Text="Write" runat="server"
            onclick="btnInsert_Click" />&nbsp;&nbsp;&nbsp;
        <asp:Button ID="btnCancel" Text="Cancel" runat="server" Visible="false"
            onclick="btnCancel_Click" />
    </p>
    </div>
    </form>
</body>
</html>

===========================================================================================

Insert.aspx.cs

===========

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Insert : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        BLL bbs = new BLL();
        bbs.Title = txtTitle.Text;
        bbs.Writer = txtWriter.Text;
        bbs.Content = txtContent.Text;
        bbs.WriteDate = DateTime.Now;

        try
        {
            new DAL().InseertData(bbs);
        }
        catch (Exception ex)
        {
            lblMsg.Visible = true;
            lblMsg.Text = "최대 3000자까지 입력 가능합니다.";
        }
        Response.Redirect("List.aspx");
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("List.aspx");
    }
}

转载于:https://www.cnblogs.com/hgx0488/archive/2009/01/13/1375227.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值