首页 > 开发 > 综合 > 正文

利用c#制作简单的留言板 (2)

2024-07-21 02:20:21
字体:
来源:转载
供稿:网友
myconn.cs
namespace notpage
{
    using system;
    using system.data.sql ;

    /// <summary>
    ///    summary description for myconn.
    /// </summary>
    public class myconn:system.data.sql.sqlconnection
    {
        private void initializecomponent ()
        {
        }
    
        public myconn()
        {
            //
            // todo: add constructor logic here
            //
            this.database = "back" ;
            this.datasource = "luochang" ;
            this.userid = "sa" ;
            this.password = "" ;
        }
    }
}
添加留言addtopic.aspx
<%@ page language="c#" codebehind="addtopic.cs" autoeventwireup="false" inherits="notpage.addtopic" %>
<html><head>
<meta content="microsoft visual studio 7.0" name=generator>
<meta content=c# name=code_language></head>
<body>
<form method=post runat="server">
<table cellspacing=1 cellpadding=1 width="88%" border=0>
  <tr>
    <td>留言主题:</td>
    <td><asp:textbox id=txttitle runat="server" maxlength="80" columns="65"></asp:textbox></td></tr>
  <tr>
    <td>姓名:</td>
    <td><asp:textbox id=txtauthor runat="server" maxlength="40" columns="20"></asp:textbox></td></tr>
  <tr>
    <td>留言内容</td>
    <td><asp:textbox id=txtcontent runat="server" maxlength="2000" columns="50" rows="20" textmode="multiline"></asp:textbox><asp:button
      id=btnsubmit runat="server"
  text="确认"></asp:button></td></tr></table></form>
    
  </body></html>

对应的cs
namespace notpage
{
    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;

    /// <summary>
    ///    summary description for addtopic.
    /// </summary>
    public class addtopic : system.web.ui.page
    {
        protected system.web.ui.webcontrols.textbox txtcontent;
        protected system.web.ui.webcontrols.textbox txtauthor;
        protected system.web.ui.webcontrols.textbox txttitle;
        protected system.web.ui.webcontrols.button btnsubmit;
    
    public addtopic()
    {
        page.init += new system.eventhandler(page_init);
        }

        protected void page_load(object sender, eventargs e)
        {
            if (!ispostback)
            {
                //
                // evals true first time browser hits the page
                //
            }
        }

        protected void page_init(object sender, eventargs e)
        {
            //
            // codegen: this call is required by the asp+ windows form designer.
            //
            initializecomponent();
        }

        /// <summary>
        ///    required method for designer support - do not modify
        ///    the contents of this method with the code editor.
        /// </summary>
        private void initializecomponent()
        {
            btnsubmit.click += new system.eventhandler (this.onsubmit);
            this.load += new system.eventhandler (this.page_load);
        }
        public void onsubmit(object sender , eventargs e)
        {
            
            
            if (page.isvalid)
            {
                //数据入库
                try
                {
            
                    notepage objnp = new notepage();
                    objnp.title = txttitle.text;
                    objnp.author = txtauthor.text;
                    objnp.content = txtcontent.text;
                    objnp.adddate = system.datetime.now;
                    notepage objnp1 = new notepage();
                    if(objnp1.addtopic(objnp))
                    {
                        
                        response.write ("<p align=center class=cn>成功留言,点击<a href = list.aspx>此处</a>查看留言列表!。</p>") ;
                    }
                }
                catch(exception exp)
                {
#if debug
                    response.write ("出现异常:" + exp.message) ;
                    return ;
#endif//debug
                
                }
            }
            
            
        }

    }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表