註冊

 

先用linq鏈接數據  而後建個類寫幾個方法 以下:html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// DA 的摘要說明
/// </summary>
public class DA
{
    private MyDBDataContext Context;
    public DA()
    {
        Context = new MyDBDataContext();
    }
    public void Insert(zhuce data)
    {
        Context.zhuce.InsertOnSubmit(data);
        Context.SubmitChanges();
    }
    public bool SelectBy(string id)
    {
        List<zhuce> list = Context.zhuce.Where(p => p.Id == id).ToList();
        if (list.Count() > 0)
        {
            return true;
        }
        return false;
    }
    public List<ChinaStates> Select(string code)
    {
        return Context.ChinaStates.Where(p => p.ParentAreaCode == code).ToList();
    }
}

  註冊頁面aspx代碼:sql

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:Label ID="Label1" runat="server" Text="代號:"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="惟一驗證" />
        <br />
        <asp:Label ID="Label2" runat="server" Text="用戶名:"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label3" runat="server" Text="密碼:"></asp:Label>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label4" runat="server" Text="確認密碼:"></asp:Label>
        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
        <br />
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <br />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                省:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                </asp:DropDownList>
                市:<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" >
                </asp:DropDownList>
                區:<asp:DropDownList ID="DropDownList3" runat="server">
                </asp:DropDownList>
            </ContentTemplate>
        </asp:UpdatePanel>
    
    </div>
        <asp:Label ID="Label5" runat="server" Text="驗證碼:"></asp:Label>
        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="Handler.ashx" OnClick="ImageButton1_Click" />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="註冊" />
    </form>
</body>
</html>

  註冊頁面aspx.cs代碼:數據庫

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BIndDropDown1();
        }
    }
    protected void Button2_Click(object sender, EventArgs e)//惟一驗證
    {
        if (string.IsNullOrWhiteSpace(TextBox1.Text))
        {
            Response.Write("<script>alert('用戶ID不能爲空!!')</script>");
        }
        else
        {
            bool isok = new DA().SelectBy(TextBox1.Text);
            if (isok)
            {
                Response.Write("<script>alert('此ID不可用')</script>");
            }
            else
            {
                Response.Write("<script>alert('此ID可用')</script>");
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string id = TextBox1.Text;
        string uid = TextBox2.Text;
        string pwd = TextBox3.Text;
        string rpwd = TextBox4.Text;
        string adrs = DropDownList1.SelectedItem.Text + DropDownList2.SelectedItem.Text + DropDownList3.SelectedItem.Text;
        if (id == "")
        {
            Response.Write("<script>alert('用戶ID不能爲空!!')</script>");
        }
        else if (uid == "")
        {
            Response.Write("<script>alert('用戶名不能爲空!!')</script>");
        }
        else if (pwd == "")
        {
            Response.Write("<script>alert('用戶密碼不能爲空!!')</script>");
        }
        else if (rpwd != pwd)
        {
            Response.Write("<script>alert('密碼不一致!!')</script>");
        }
        else if (adrs=="")
        {
            Response.Write("<script>alert('請選擇地址!!')</script>");
        }
        else
        {
            zhuce data = new zhuce();
            data.Id = id;
            data.Uid = uid;
            data.Pwd = pwd;
            data.Adrs = adrs;
            string ss = Session["yanzheng"].ToString();
            if (TextBox5.Text==ss)
            {
                new DA().Insert(data);
                //Session["uid"] = TextBox2.Text;//Sesson傳值
                //Response.Redirect("Default2.aspx");

                Response.Redirect("Default3.aspx?uid="+TextBox2.Text+ "&pwd="+TextBox3.Text);
            }
            else
            {
                Response.Write("<script>alert('註冊失敗!!')</script>");
            }
           
          

        }
    }
    //-------------------------------------------倆種方法選擇其一
    //public void BIndDropDown()
    //{
    //    List<ChinaStates> list = new ChinaDA().Select("0001");
    //    foreach (ChinaStates data in list)
    //    {
    //        ListItem lt = new ListItem(data.AreaName, data.AreaCode);
    //        DropDownList1.Items.Add(lt);
    //    }
    //}
    public void BIndDropDown1()
    {
        List<ChinaStates> list = new DA().Select("0001");
        DropDownList1.DataSource = list;
        DropDownList1.DataTextField = "AreaName";
        DropDownList1.DataValueField = "AreaCode";
        DropDownList1.DataBind();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string city = DropDownList1.SelectedItem.Value;
        List<ChinaStates> list = new DA().Select(city);
        DropDownList2.DataSource = list;
        DropDownList2.DataTextField = "AreaName";
        DropDownList2.DataValueField = "AreaCode";
        DropDownList2.DataBind();

        string qu = DropDownList2.SelectedItem.Value;
        List<ChinaStates> list1 = new DA().Select(qu);
        DropDownList3.DataSource = list1;
        DropDownList3.DataTextField = "AreaName";
        DropDownList3.DataValueField = "AreaCode";
        DropDownList3.DataBind();
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string qu = DropDownList2.SelectedItem.Value;
        List<ChinaStates> list1 = new DA().Select(qu);
        DropDownList3.DataSource = list1;
        DropDownList3.DataTextField = "AreaName";
        DropDownList3.DataValueField = "AreaCode";
        DropDownList3.DataBind();
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton1.ImageUrl = "Handler.ashx?id=" + new Random().Next(100);
    }
}

  利用Session傳值的頁面aspx代碼:session

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        歡迎      
        <asp:Literal ID="Literal1" runat="server"></asp:Literal>
    
    </div>
    </form>
</body>
</html>

  利用Session傳值的頁面aspx.cs代碼:dom

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Literal1.Text = Session["uid"].ToString();
    }
}

  利用超連接傳值的頁面aspx代碼:字體

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
          歡迎:<asp:Literal ID="Literal1" runat="server"></asp:Literal>
     </div>
    </form>
    <p>
 </p>
</body>
</html>

  利用超連接傳值的頁面aspx.cs代碼:ui

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string uid = Request["uid"].ToString();
        string pwd = Request["pwd"].ToString();
        Literal1.Text = uid + "  " + pwd;
    }
}

  

  圖片驗證碼,就是首先造一張空白的圖,而後在圖片上繪製隨機數就能夠了,在這裏用到了通常處理程序。code

  通常處理程序就是單純的頁面(無任何標籤),是.ashx文件,裏面的驗證數字利用Session保存,用Response顯示在頁面上,若是想用的話 直接圖片按鈕(Image Button)路徑到這個頁面就能夠,, 而裏面的驗證數字則能夠利用Session["xxx"]來接收。orm

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Drawing;//引用繪圖命名空間
using System.Web.SessionState;//IRequiresSessionState的命名空間

public class Handler : IHttpHandler , IRequiresSessionState  //Session的接口,必須得繼承,否則無法用session
{
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "image/jpeg";//設置類型
        //開始造圖片,
        Bitmap img = new Bitmap(50, 20);  //一張寬度爲50,高度爲20的空白圖片
        Graphics gc = Graphics.FromImage(img);  //往img圖裏繪製
        Font font = new Font("楷體", 12, FontStyle.Bold);//字體格式
        SolidBrush brush = new SolidBrush(Color.White);//刷子格式 ,裏面的顏色是背景顏色    
        gc.FillRectangle(brush, 0, 0, 50, 20);//繪製的樣子
        brush.Color = Color.Red;//刷子顏色

        //已經繪製完了,往圖裏扔驗證碼,開始造驗證碼
        string s = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        string str = "";
        Random r = new Random(); //隨機出一個數來

        for (int i = 0; i < 4; i++)
        {
            int x = r.Next(0, 62);
            string ss = s.Substring(x, 1); //截取字符串
            str += ss;
        }

        //驗證碼已經造好了,開始往圖片裏扔,前提先保存一下驗證碼的內容,爲了之後的使用(輸入驗證)
        context.Session["yanzheng"] = str;

        //往圖片裏仍
        gc.DrawString(str, font, brush, 0, 0);


        //  最後將圖片保存,經過response響應流保存
        img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

  

通常處理程序造好之後,須要來用它,這裏是用的圖片按鈕(ImageBotton)來接收,右鍵ImageButton ,找到ImageUrl屬性,直接選擇路徑就能夠了,在這裏的路徑是Handler.ashx。圖片已經顯示在頁面上了,就須要進行驗證。。。  用Session來接收 在註冊頁面aspx.cs代碼下server

 

數據庫:

use mydb

create table zhuce
(
    Id  varchar(50) primary key not null,
	Uid varchar(50)  not null,
	Pwd varchar(50) not null,
	Adrs varchar(50),
)
select*from zhuce

select * from chinastates
select * from chinastates where ParentAreaCode=0000
select * from chinastates where ParentAreaCode=0001
select * from chinastates where ParentAreaCode=11
select * from chinastates where ParentAreaCode=1101
相關文章
相關標籤/搜索