ASP.NET开发的第一小系统

学习ASP.NET的一点感受,我学习的是基于C#的部分,感觉C#特别
像java,所以进行简单开发看一天就可以了,前台视图和C# 的结
合我感觉更像java的Tapestry框架,也是基于事件驱动的开发模
式,因为用过Tapestry所以现在学习起ASP.NET感觉难度不大,
也许还没有进入深入开发的原因吧!以下是我用ASP.NET开发
的一个的留言板,主要是测试用的,因此非常简单。这次目标
主要是把ASP.NET的一个系统,用java重写,现在已经实现,
因此也不计划太深入的继续往下学习这个了

第一、添加留言的页面Defalt.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.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>
        userName:<asp:TextBox ID=
"guest_Msg" runat="server" TextMode="MultiLine"></asp:TextBox><br />
        <asp:Button ID=
"send_Message" runat="server" Text="发表留言" OnClick="send_Message_Click"/></div>
    </form>
</body>
</html>
对应的后台处理代码Default.aspx.cs
using System;
using System.Data;
using System.Data.SqlClient;
//加入的命名空间
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;

public partial class _Default : System.Web.UI.Page
{
    public const string connString =
"Data Source=IP;Initial Catalog=数据库;User ID=用户名;Password=密码";

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void send_Message_Click(object sender, EventArgs e)
    {
        string msg = guest_Msg.Text;
        if(msg!=null&&!
"".Equals(msg)){
            addMsg(msg);
            Response.Redirect(
"Default.aspx");
        }
        
    }
    private void addMsg(string msg)
    {
        string Sql =
"INSERT INTO guestbook(ID,UserName)values(newid(),'"+msg+"')";

        SqlConnection conn = new SqlConnection(connString);
        SqlCommand cmd = new SqlCommand(Sql, conn);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
}
第二、显示留言列表的页面ShowMsg.aspx
<%@ Page Language=
"C#" AutoEventWireup="true" CodeFile="ShowMsg.aspx.cs" Inherits="ShowMsg" %>

<!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:Repeater ID=
"rpt_Message" runat="server">
        <ItemTemplate>
         <table>
           <tr>
             <td>留言ID</td>
             <td>留言内容</td>
           </tr>
           <tr>
             <td><%# Eval(
"ID")%></td>
             <td><%# Eval(
"UserName")%></td>
           </tr>
         </table>
        </ItemTemplate>
        </asp:Repeater>
        
    </div>
    </form>
</body>
</html>
与之对应的后台代码ShowMsg.aspx.cs
using System;
using System.Data;
using System.Data.SqlClient;
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;

public partial class ShowMsg : System.Web.UI.Page
{
    public const string connString =
"Data Source=IP;Initial Catalog=数据库;User ID=用户名;Password=密码";

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

    private void SetBind()
    {
        using (SqlConnection conn = new SqlConnection(connString))
        {
            string cmdText =
"SELECT * FROM guestbook";
            SqlDataAdapter da = new SqlDataAdapter(cmdText,conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
            rpt_Message.DataSource = dt;
            rpt_Message.DataBind();
        }
    }

}


lunzi   2007-12-13 16:34:27 评论:1   阅读:487   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2004-2010 powered by YuLog