ASP.NET 實現簡單的註冊介面(使用asp控制元件)
阿新 • • 發佈:2019-02-02
我們利用ASP.NET來實現簡單的註冊介面
一:註冊首頁的介面程式碼(.aspx檔案 這裡使用的是asp自帶的控制元件,也可以使用HTML的控制元件來實現)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Register.aspx.cs" Inherits="Web.Register" %> <!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> <asp:Label ID="Label1" runat="server" Text="使用者註冊"></asp:Label> <form id="form1" runat="server"> <div> <br /> <br /> </div> 使用者名稱:<asp:TextBox ID="TextBoxName" runat="server"></asp:TextBox> <br /> <br /> 密碼: <asp:TextBox ID="TextBoxPassword" runat="server" TextMode="Password"></asp:TextBox> <br /> <br /> <br /> 性別:<br /> <asp:RadioButtonList ID="RadioButtonListSex" runat="server" RepeatDirection="Horizontal"> <asp:ListItem Value="male">男</asp:ListItem> <asp:ListItem Value="female">女</asp:ListItem> </asp:RadioButtonList> <br /> <br /> 職業:<br /> <asp:RadioButtonList ID="RadioButtonListWork" runat="server" RepeatDirection="Horizontal"> <asp:ListItem Value="publicServat">公務員</asp:ListItem> <asp:ListItem Value="doctor">醫務工作者</asp:ListItem> <asp:ListItem Value="teacher">教師</asp:ListItem> <asp:ListItem Value="business">經商</asp:ListItem> </asp:RadioButtonList> <br /> 所屬省份:<asp:DropDownList ID="DropDownListProvince" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownListProvince_SelectedIndexChanged" style="height: 19px"> <asp:ListItem Value="guangdong">廣東省</asp:ListItem> <asp:ListItem Value="guangxi">廣西省</asp:ListItem> <asp:ListItem Value="heilongjiang">黑龍江省</asp:ListItem> </asp:DropDownList> <br /> <br /> 所在城市:<asp:ListBox ID="ListBoxCity" runat="server" Height="43px" style="margin-top: 4px" Width="102px"></asp:ListBox> <br /> <br /> 愛好:<asp:CheckBoxList ID="CheckBoxListHobby" runat="server" RepeatColumns="2"> <asp:ListItem>球類運動</asp:ListItem> <asp:ListItem>田徑運動</asp:ListItem> <asp:ListItem>讀書看報</asp:ListItem> <asp:ListItem>聊天交友</asp:ListItem> </asp:CheckBoxList> <br /> <asp:Button ID="ButtonOk" runat="server" OnClick="ButtonOk_Click" Text="確定" /> <br /> </form> </body> </html>
二:後臺處理程式碼(C#實現 .aspx.cs)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Web { public partial class Register : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void DropDownListProvince_SelectedIndexChanged(object sender, EventArgs e) { switch (DropDownListProvince.SelectedItem.Text) { case "廣東省": ListBoxCity.Items.Clear(); ListBoxCity.Items.Add("廣州市"); ListBoxCity.Items.Add("深圳市"); break; case "廣西省": ListBoxCity.Items.Clear(); ListBoxCity.Items.Add("南寧市"); ListBoxCity.Items.Add("桂林市"); break; case "黑龍江省": ListBoxCity.Items.Clear(); ListBoxCity.Items.Add("哈爾濱市"); ListBoxCity.Items.Add("齊齊哈爾市"); break; } } protected void ButtonOk_Click(object sender, EventArgs e) { string name, password, sex = " ", work, location = "", hobby = ""; if (TextBoxName.Text == "") { Response.Write("<script>window.alert('使用者名稱不能為空!');</script>"); } else { name = TextBoxName.Text; password = TextBoxPassword.Text; if (RadioButtonListSex.Items[0].Selected) { sex = "男"; } else if (RadioButtonListSex.Items[1].Selected) { sex = "女"; } work = RadioButtonListWork.SelectedItem.Text; if (ListBoxCity.SelectedItem != null) { location = DropDownListProvince.SelectedItem.Text + "," + ListBoxCity.SelectedItem.Text; } for (int i = 0; i < CheckBoxListHobby.Items.Count; i++) { if (CheckBoxListHobby.Items[i].Selected) hobby += CheckBoxListHobby.Items[i].Text + ","; } hobby = hobby.TrimEnd(','); Response.Redirect(string.Format("SimpleResult.aspx?name={0}&sex={1}&work={2}&location={3}&hobby={4}", name, sex, work, location, hobby)); } } } }
三:結果介面程式碼( .aspx檔案)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SimpleResult.aspx.cs" Inherits="Web.SimpleResult" %> <!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 /> 姓 名:<%=name %><br /> 性 別:<%=sex %><br /> 職 業:<%=work %><br /> 所在城市:<%=location %><br /> 愛 好:<%=hobby %><br /> 請牢記你的密碼! </div> </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;
namespace Web
{
public partial class SimpleResult : System.Web.UI.Page
{
public string name;
public string sex;
public string work;
public string location;
public string hobby;
protected void Page_Load(object sender, EventArgs e)
{
name = Request.QueryString["name"];
sex = Request.QueryString["sex"];
work = Request.QueryString["work"];
location = Request.QueryString["location"];
hobby = Request.QueryString["hobby"];
}
}
}