留言板----一个注册页面的模板


<body>
    <form id="form1" runat="server">
    <div>
        <center>
            <table border="1" cellpadding="0" cellspacing="0" id="LoginTABLE" style="width: 409px">
                <tr>
                    <td colspan="2" style="background-color: #336633">
                        <span style="font-size: 36pt; color:White">注册会员</span>
                    </td>
                </tr>
                <tr>
                    <td style="width:108px">
                        <span style="font-size: 10pt">用 户 名:</span>
                    </td>
                    <td align="left">
                        <asp:TextBox ID="unameTextBox" runat="server" ForeColor="#C00000"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="unameRequiredFieldValidator" runat="server"
                        ErrorMessage="用户名不能为空" ControlToValidate="unameTextBox">*</asp:RequiredFieldValidator>
                    </td>
                </tr>
                 <tr>
                    <td style="width:108px">
                        <span style="font-size: 10pt">密&nbsp &nbsp &nbsp &nbsp 码:</span>
                    </td>
                    <td align="left">
                        <asp:TextBox ID="passwordTextBox" runat="server" ForeColor="#C00000" TextMode="Password"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="passwordRequiredFieldValidator" runat="server"
                        ErrorMessage="密码不能为空" ControlToValidate="passwordTextBox">*</asp:RequiredFieldValidator>
                    </td>
                </tr>
                 <tr>
                    <td style="width:108px">
                        <span style="font-size: 10pt">确定密码:</span>
                    </td>
                    <td align="left">
                        <asp:TextBox ID="surepasswordTextBox" runat="server" ForeColor="#C00000" TextMode="Password"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="surepasswordRequiredFieldValidator" runat="server"
                        ErrorMessage="确定密码不能为空" ControlToValidate="surepasswordTextBox">*</asp:RequiredFieldValidator>
                        <asp:CompareValidator ID="CompareValidator1" runat="server"
                        ErrorMessage="确定密码和密码不一致" ControlToCompare="passwordTextBox" ControlToValidate="surepasswordTextBox">*</asp:CompareValidator>
                    </td>
                </tr>
                <tr>
                    <td style="width:108px">
                        <span style="font-size: 10pt">EMAIL:</span>
                    </td>
                    <td align="left">
                        <asp:TextBox ID="emailTextBox" runat="server" ForeColor="#C00000"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="emailRequiredFieldValidator" runat="server"
                        ErrorMessage="Email地址不能为空" ControlToValidate="emailTextBox">*</asp:RequiredFieldValidator>
                        <asp:RegularExpressionValidator ID="emailRegularExpressionValidator" runat="server"
                        ErrorMessage="Email格式不正确" ControlToValidate="emailTextBox" ValidationExpression="\w+([-+.']\w+)*@\w+([-.']\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td style="width:108px">
                        <span style="font-size: 10pt">联系电话:</span>
                    </td>
                    <td align="left">
                        <asp:TextBox ID="phoneTextBox" runat="server" ForeColor="#C00000"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="phoneRequiredFieldValidator" runat="server"
                        ErrorMessage="联系电话不能为空" ControlToValidate="phoneTextBox">*</asp:RequiredFieldValidator>
                        <asp:RegularExpressionValidator ID="phoneRegularExpressionValidator" runat="server"
                        ErrorMessage="联系电话格式不正确" ControlToValidate="phoneTextBox" ValidationExpression="(\(\d{3}\)|\d{3}-)?\d{8}">*</asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td style="width:108px">
                        <span style="font-size: 10pt">家庭地址:</span>
                    </td>
                    <td align="left">
                        <asp:TextBox ID="addressTextBox" runat="server" ForeColor="#C00000"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="addressRequiredFieldValidator" runat="server"
                        ErrorMessage="家庭地址不能为空" ControlToValidate="addressTextBox">*</asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <asp:Button ID="loginButton" runat="server" Text="注册" OnClick="loginButton_Click" />
                    </td>
                </tr>
            </table>
        </center>
    </div>
        <asp:ValidationSummary ID="loginValidationSummary" runat="server" ShowMessageBox="true" ShowSummary="false" />
   
    </form>
</body>

注意RequiredFieldValidator 控件后面的*,如果不加*自己试试会出现什么效果;)

下面是此页面的后台代码:

 protected void loginButton_Click(object sender, EventArgs e)
    {
        try
        {
            //创建数据库连接
            SqlConnection con = db.CreateConnection();

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

            //建立插入的SQL语句
            string strsql = "insert into login(uname, upass, email, phone, addr) values('" + unameTextBox.Text + "', '" + passwordTextBox.Text + "', '" + emailTextBox.Text + "', '" + phoneTextBox.Text + "', '" + addressTextBox.Text + "' )";

            //创建Sqlcommd命令控件
            SqlCommand cmd = new SqlCommand(strsql, con);

            //执行sqlcommand控件,不返回任何数据
            cmd.ExecuteNonQuery();

            con.Close();

            Response.Redirect("login.aspx");
        }
        catch (Exception ee)
        {
            Response.Write("<script>alert('用户名存在,请选择其他用户名')</script>");
        }
    }

当然,还需要一个连接数据库的类:

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;

/// <summary>
/// db 的摘要说明
/// </summary>
public class db
{
 public db()
 {
  
 }
    public static SqlConnection CreateConnection()
    {
        SqlConnection con = new SqlConnection("Data Source=lacalhost;Initial Catalog=message;Integrated Security=True;");
        return con;
    }
}

 

posted @ 2008-06-20 23:07  不染丹心  阅读(7071)  评论(2编辑  收藏  举报