个人信息提交表单
样式及运行结果如下
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="ControlTest.Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 88px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1> 设置我的信息</h1>
<br />
<table style="width:100%;">
<tr>
<td class="style1">
姓名:</td>
<td>
<asp:TextBox ID="TextBoxName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
性别:</td>
<td>
<asp:RadioButton ID="RadioButtonm" runat="server" Text="男" GroupName="sex" />
<asp:RadioButton ID="RadioButtonf" runat="server" Text="女" GroupName="sex" />
</td>
</tr>
<tr>
<div id="div1" runat="server">
<td class="style1">
爱好:</td>
<td>
<asp:CheckBox ID="CheckBox2" runat="server" Text ="踢球"/>
<asp:CheckBox ID="CheckBox3" runat="server" Text ="游戏"/>
<asp:CheckBox ID="CheckBox4" runat="server" Text ="读书"/></td>
</div>
</tr>
<tr>
<td class="style1">
出生日期:</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
年<asp:DropDownList ID="DropDownList2" runat="server"
onselectedindexchanged="DropDownList2_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
月<asp:DropDownList ID="DropDownList3" runat="server">
</asp:DropDownList>
日</td>
</tr>
<tr>
<td class="style1">
</td>
<td>
你是否同意春天最美丽?<asp:RadioButton ID="RadioButton1"
runat="server" Text="同意" GroupName="agree"/>
<asp:RadioButton ID="RadioButton2" runat="server" Text="不同意"
GroupName="agree"/>
</td>
</tr>
<tr>
<td class="style1">
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="提交" onclick="Button1_Click" />
</td>
</tr>
</table>
<br />
</div>
<p>
你提交的信息有:<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</p>
</form>
</body>
</html>
因为选择日的下拉框要根据年月判断,所以将月的AutoPostBack属性设为true,在DropDownList2_SelectedIndexChanged里面写代码生成日。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ControlTest
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
for (int year = DateTime.Now.Year; year >= 1900; year--)
DropDownList1.Items.Add(year.ToString());
for (int month = 1; month <= 12; month++)
DropDownList2.Items.Add(month.ToString());
for (int date = 1; date <= 31; date++)
DropDownList3.Items.Add(date.ToString());
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList3.Items.Clear();
int y = int.Parse(DropDownList1.SelectedValue);
int m = int.Parse(DropDownList2.SelectedValue);
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
{
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
{
for (int date = 1; date <= 31; date++)
DropDownList3.Items.Add(date.ToString());
}
else if (m == 2)
{
for (int date = 1; date <= 29; date++)
DropDownList3.Items.Add(date.ToString());
}
else
{
for (int date = 1; date <= 30; date++)
DropDownList3.Items.Add(date.ToString());
}
}
else
{
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
{
for (int date = 1; date <= 31; date++)
DropDownList3.Items.Add(date.ToString());
}
else if (m == 2)
{
for (int date = 1; date <= 28; date++)
DropDownList3.Items.Add(date.ToString());
}
else
{
for (int date = 1; date <= 30; date++)
DropDownList3.Items.Add(date.ToString());
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
String sex = "";
if(RadioButtonm.Checked){
sex = "男";
}
else if(RadioButtonf.Checked){
sex = "女";
}
String hobby = "";
foreach (Control ctl in this.div1.Controls)//Control 控件类 服务器控件的父类 Controls控件集合
{
if (ctl is CheckBox)
{
CheckBox chk = ctl as CheckBox;
if (chk.Checked)
{
hobby += chk.Text;
}
}
}
String agree = "";
if(RadioButton1.Checked){
agree = "同意";
}
else if(RadioButton2.Checked){
agree = "不同意";
}
Label1.Text = "<br/>" + "姓名:" + TextBoxName.Text + "<br/>" + "性别:" + sex + "<br/>"
+ "生日:" + DropDownList1.SelectedItem.Text + "年"
+ DropDownList2.SelectedItem.Text + "月"
+ DropDownList3.SelectedItem.Text + "日"+"<br/>"
+ "爱好:" + hobby + "<br/>" + agree + "春天最美丽" ;
}
}
}