留言板----message.aspx页面(sql 插入语句)


<body>
    <form id="form1" runat="server">
    <div>
    <center>
        <table border="1" cellpadding="0" cellspacing="0">
            <tr>
                <td colspan="2" style="font-size:32pt; color: White; background-color: #669900">
                    留言板
                </td>
            </tr>
            <tr>
                <td style="font-size: 10pt">
                用户名:
                </td>
                <td style="width: 4px">
                    <asp:TextBox ID="UserNameTextBox" runat="server" Enabled="false"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="font-size: 10pt">
                留言主题:
                </td>
                <td align="left">
                    <asp:TextBox ID="themeTextBox" runat="server" Width="233px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="themeRequiredFieldValidator" runat="server"
                    ErrorMessage="留言主题不能为空" ControlToValidate="themeTextBox">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
               <td style="font-size: 10pt">
                留言内容:
                </td>
                <td align="left">
                    <asp:TextBox ID="contentTextBox" runat="server" Height="214px" Width="233px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="contentRequiredFieldValidator" runat="server"
                    ErrorMessage="留言内容不能为空" ControlToValidate="contentTextBox">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="messageButton" runat="server" Text="留言" OnClick="messageButton_Click" />&nbsp &nbsp
                    <asp:Button ID="cancelButton" runat="server" Text="取消" OnClick="cancelButton_Click" />
                </td>
            </tr>
            <tr>
                <td colspan="2" style="color: Black">
                    <span style="font-size: smaller">本站共有</span>
                    <asp:Label ID="Label1" runat="server" ForeColor="#C00000" Width="2px"></asp:Label>
                     <span style="font-size: smaller">位注册会员</span>
                </td>
            </tr>
        </table>
    </center>
    </div>
      <asp:validationsummary runat="server" ID="validationsummary1" ShowMessageBox="true" ShowSummary="false"></asp:validationsummary>
    </form>
</body>

后台代码:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //接受登陆页传过来的值
            this.UserNameTextBox.Text = Session["uname"].ToString();

            //连接数据库
            SqlConnection con = db.CreateConnection();

            //打开数据库
            con.Open();

            //定义查询语句
            string strsql = "select count(*) from login";

            //创建SqlCommand对象
            SqlCommand cmd = new SqlCommand(strsql, con);

            //定义一个读对象并执行cmd对象
            SqlDataReader rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                //将读出来的数据绑定到Label控件上
                this.Label1.Text = rd[0].ToString();
            }

            //关闭读操作
            rd.Close();

            //关闭数据库
            con.Close();
        }
        catch
        {
            Response.Redirect("login.aspx");
        }
    }
    protected void messageButton_Click(object sender, EventArgs e)
    {
        SqlConnection con = db.CreateConnection();
        con.Open();

        //定义Sql插入语句
        string strsql = "insert into message values('"+UserNameTextBox.Text+"', '"+themeTextBox.Text+"', '"+contentTextBox.Text+"', '"+DateTime.Now.ToString()+"')";       
       //定义sqlcommand命令对象
        SqlCommand cmd = new SqlCommand(strsql, con);
        cmd.ExecuteNonQuery();
        con.Close();

        //跳转到制定页面
        Response.Redirect("messageout.aspx");

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

    }
}


posted @ 2008-06-21 09:29  不染丹心  阅读(610)  评论(0编辑  收藏  举报