簡單的 ASP.NET 2.0 C# 留言本

來源:互聯網
上載者:User
簡單的ASP.NET 留言本開發工具:vs2005
  1. 數據庫設計

    • 建立一個數據庫名為:webtest
      sql語句如下:
      create database webtest        
    • 建立一個表名為:databook
      結構如下:

      資料表名稱 中文對應名稱 所包含欄位
      databook 會計基本科目維護 欄位名稱 中文對應名稱 欄位類型 欄位大小
      ID bigint 8
      標題 nvarchar 50
      姓名 nvarchar 10
      本文 nvarchar 200
      IP datetime 15

      sql語句如下:

      CREATE TABLE DATABOOK        (        ID BIGINT  IDENTITY (1, 1) NOT FOR REPLICATION,        標題 VARCHAR (50),        姓名 VARCHAR (10),        本文 VARCHAR (200),        IP VARCHAR (15),        )        INSERT  DATABOOK  SELECT   '測試 ',  '破曉之陽 ',   '測試內容 ', '127.0.0.1 '        UNION   ALL   SELECT   '測試 ',  '破曉之陽 ',   '測試內容 ', '127.0.0.1 '        

  2. 用戶留言頁面
    • 用戶留言頁面設計
      建立一個guest.aspx檔案,html代碼如下:

      <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="guest.aspx.cs" Inherits="_Default" %>        <!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>留言本</title>        </head>        <body>        <form id="form1" runat="server">        <div>        <label id="Label1">留言本</label>        <asp:GridView ID="GridView1" runat="server">        <Columns>        <asp:HyperLinkField DataNavigateUrlFields="id" DataNavigateUrlFormatString="see.aspx?id={0}"        Text="查看!" />        </Columns>        </asp:GridView>        <br />        <label id="Label2">我要留言</label>        <div>        <ul style="width: 350px; padding-right: 5px; padding-left: 5px; padding-bottom: 5px; padding-top: 5px; margin-top: 20px;">        <li>標題:<asp:TextBox ID="title" runat="server"></asp:TextBox><a>*</a><asp:RequiredFieldValidator        ID="RequiredFieldValidator1" runat="server" ErrorMessage="此項不能為空" ControlToValidate="title"></asp:RequiredFieldValidator></li>        <li>姓名:<asp:TextBox ID="name" runat="server"></asp:TextBox><a>*</a><asp:RequiredFieldValidator        ID="RequiredFieldValidator2" runat="server" ErrorMessage="此項不能為空" ControlToValidate="name"></asp:RequiredFieldValidator></li>        <li>內容:<textarea id="body" cols="20" rows="2" runat="server"></textarea></li>        <li> <asp:Button ID="Button1" runat="server" Style="position: relative" Text="提交內容" OnClick="Button1_Click" /></li>        </ul>        </div>        </div>        </form>        </body>        </html>        

    • 用戶留言頁面程式代碼編寫
      using System;        using System.Data;        using System.Configuration;        using System.Web;        using System.Web.Security;        using System.Web.UI;        using System.Web.UI.WebControls;        using System.Web.UI.WebControls.WebParts;        using System.Web.UI.HtmlControls;        using System.Data.SqlClient;        public partial class _Default : System.Web.UI.Page        {        protected void Page_Load(object sender, EventArgs e)        {        if (!IsPostBack)        {        BindData();        }        }        public void BindData()        {        string ConnStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();        SqlConnection Sqlconn = new SqlConnection(ConnStr);        string Sqlstr = "select * from databook";        SqlDataAdapter myAdapter = new SqlDataAdapter(Sqlstr, Sqlconn);        DataSet ds = new DataSet();        myAdapter.Fill(ds, "databook");        GridView1.DataSource = ds.Tables["databook"].DefaultView;        GridView1.DataBind();        }        protected void Button1_Click(object sender, EventArgs e)        {        SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());        cn.Open();        string str = "Insert into databook(標題,姓名,本文,ip) values('" + this.title.Text.Trim().ToString() + "','" + this.name.Text.Trim().ToString() + "','" + this.body.Value.Trim().ToString() + "','" + Request.UserHostAddress.ToString() + "')";        SqlCommand com = new SqlCommand(str, cn);        com.ExecuteNonQuery();        BindData();        }        }        
  3. 查看頁面
    • 查看頁面設計
      建立一個see.aspx檔案,html代碼如下:

      <%@ Page Language="C#" AutoEventWireup="true" CodeFile="see.aspx.cs" Inherits="see" %>        <%@ Import Namespace="System.Data" %>        <!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>查看留言</title>        </head>        <body>        <form id="form1" runat="server">        <div>        <asp:DataList ID="DataList1" runat="server">        <ItemTemplate>        ID::<%# ((DataRowView)Container.DataItem)["id"] %>  <br />        標題:<%# ((DataRowView)Container.DataItem)["標題"] %><br />        姓名:<%# ((DataRowView)Container.DataItem)["姓名"] %><br />        IP:<%# ((DataRowView)Container.DataItem)["ip"] %><br />        本文:<%# ((DataRowView)Container.DataItem)["本文"] %>        </ItemTemplate>        </asp:DataList>        <a href="guest.aspx">返回</a>        </div>        </form>        </body>        </html>        
    • 查看頁面程式代碼編寫
      using System;        using System.Data;        using System.Configuration;        using System.Collections;        using System.Web;        using System.Web.Security;        using System.Web.UI;        using System.Web.UI.WebControls;        using System.Web.UI.WebControls.WebParts;        using System.Web.UI.HtmlControls;        using System.Data.SqlClient;        public partial class see : System.Web.UI.Page        {        protected void Page_Load(object sender, EventArgs e)        {        string thisId=Request.QueryString["id"];        string ConnStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();        SqlConnection Sqlconn = new SqlConnection(ConnStr);        string Sqlstr = "select * from databook where id="+thisId+"";        SqlDataAdapter myAdapter = new SqlDataAdapter(Sqlstr, Sqlconn);        DataSet ds = new DataSet();        myAdapter.Fill(ds, "databook");        DataList1.DataSource = ds.Tables["databook"].DefaultView;        DataList1.DataBind();        }        }        
  4. web.config檔案代碼
    <?xml version="1.0"?>    <!--    注意: 除了手動編輯這個檔案以外,您也可以使用    Web 管理工具設定您的應用程式設定值。請使用    Visual Studio 中的 [網站] -> [ASP.NET 組態] 選項。    如需完整的設定與註解清單,請參考    machine.config.comments (通常位於    \Windows\Microsoft.Net\Framework\v2.x\Config)    -->    <configuration>    <appSettings>    <add key="ConnectionString" value="Server=210.1.1.22;database=webtest;uid=sa;pwd=;"/>    </appSettings>    <connectionStrings/>    <system.web>    <!--    設定 compilation debug="true" 會將偵錯    符號插入編譯過的頁面。因為這樣會    影響效能,所以只有在開發期間才能將    這個值設定為 true。    -->    <compilation debug="true"/>    <!--    <authentication> 區段可以用來設定 ASP.NET    使用的安全性驗證模式,以識別連入的    使用者。    -->    <authentication mode="Windows"/>    <!--    <customErrors> 區段可以用來設定    在執行要求期間發生未處理    錯誤時所要執行的動作。具體來說,    它可以讓開發人員設定要顯示的 HTML 錯誤網頁,    以取代錯誤堆疊追蹤。    <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">    <error statusCode="403" redirect="NoAccess.htm" />    <error statusCode="404" redirect="FileNotFound.htm" />    </customErrors>    -->    </system.web>    </configuration>    
源碼下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.