using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack)//获取一个值,该值指示页面是第一次呈现还是为了相应回发而加载 { Dropdownlist(); } } //第一种显示方法 public void Dropdownlist() { List<ChinaStates> list = new ChinaBF().SelectByParent("0001"); foreach (ChinaStates data in list) { ListItem it = new ListItem(data.AreaName,data.AreaCode); DropDownList1.Items.Add(it); } } //第二种显示方法 public void BindDropdownlist() { List<ChinaStates> list = new ChinaBF().SelectByParent("0001"); DropDownList1.DataSource = list; DropDownList1.DataTextField = "AreaName"; DropDownList1.DataValueField = "AreaCode"; DropDownList1.DataBind(); } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { //Label1.Text = DropDownList1.SelectedItem.Text; string a = DropDownList1.SelectedItem.Value; List<ChinaStates> list = new ChinaBF().SelectByParent(a); DropDownList2.DataSource = list; DropDownList2.DataTextField = "AreaName"; DropDownList2.DataValueField = "AreaCode"; DropDownList2.DataBind(); string b = DropDownList2.SelectedItem.Value; List<ChinaStates> list1 = new ChinaBF().SelectByParent(b); DropDownList3.DataSource = list1; DropDownList3.DataTextField = "AreaName"; DropDownList3.DataValueField = "AreaCode"; DropDownList3.DataBind(); } protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) { string b = DropDownList2.SelectedItem.Value; List<ChinaStates> list1 = new ChinaBF().SelectByParent(b); DropDownList3.DataSource = list1; DropDownList3.DataTextField = "AreaName"; DropDownList3.DataValueField = "AreaCode"; DropDownList3.DataBind(); } //当点击注册的时候,判断验证码格式输入是否正确 protected void Button1_Click(object sender, EventArgs e) { if (TextBox1.Text.ToUpper().Trim() == Session["ValidateCode"].ToString().ToUpper().Trim()) { Response.Write("<script>alert('登陆成功')</script>"); } else { Response.Write("<script>alert('验证码格式不正确')</script>"); } } }
方法里的代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// ChinaBF 的摘要说明 /// </summary> public class ChinaBF { private DataClassesDataContext _Context; public ChinaBF() { // // TODO: 在此处添加构造函数逻辑 // _Context=new DataClassesDataContext (); } public List<ChinaStates> Select() { return _Context.ChinaStates.ToList(); } public List<ChinaStates> SelectByParent(string id) { return _Context.ChinaStates.Where(p=>p.ParentAreaCode==id) .ToList(); } }
Html里的源代码
<%@ 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> <br /> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <br /> <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional"> <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> <br /> </ContentTemplate> </asp:UpdatePanel> <br /> <br /> <br /> 请输入验证码:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 验证码:<asp:Image ID="CreateImage" runat="server" /> <br /> <br /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="注册" /> </div> </form> </body> </html>