记录这几天的课程作业:
设计一份调查问卷,包括单选、多选题,把用户的选项显示在label标签中。
先上结果图和代码:
代码:
1.前端代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test05.aspx.cs" Inherits="test05" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<h1 style = "margin-left:600px">
大学生调查问卷</h1>
<br />
<span style = "margin-left:600px">----------------------------</span>
<br />
<br />
<br />
<div style = "margin-left:550px;">
1.您现在的学历是?
<br />
<br />
<asp:RadioButton ID="RadioButton1" runat="server" Text = "本科" GroupName="One"/>
<asp:RadioButton ID="RadioButton2" runat="server" Text = "硕士" GroupName="One"/>
<asp:RadioButton ID="RadioButton3" runat="server" Text = "专科" GroupName="One"/>
<asp:RadioButton ID="RadioButton4" runat="server" Text = "博士" GroupName="One"/>
<br />
<br />
<br />
2.您现在的年级是?<br />
<br />
<asp:RadioButton ID="RadioButton5" runat="server" Text = "大一"
GroupName="Two"/>
<asp:RadioButton ID="RadioButton6" runat="server" Text = "大二" GroupName="Two"
/>
<asp:RadioButton ID="RadioButton7" runat="server" Text = "大三" GroupName="Two"
/>
<asp:RadioButton ID="RadioButton8" runat="server" Text = "大四" GroupName="Two"
/>
<br />
<br />
<br />
3.您平时的爱好是?[多选]<br />
<br />
<div id = "div_check" runat = "server">
<asp:CheckBox ID="CheckBox1" runat="server" Text = "爬山"/>
<asp:CheckBox ID="CheckBox2" runat="server" Text = "玩游戏"/>
<asp:CheckBox ID="CheckBox3" runat="server" Text = "看书"/>
<asp:CheckBox ID="CheckBox4" runat="server" Text = "唱歌"/>
<br />
<br />
<asp:CheckBox ID="CheckBox5" runat="server" Text = "跳舞"/>
<asp:CheckBox ID="CheckBox6" runat="server" Text = "看电影"/>
<asp:CheckBox ID="CheckBox7" runat="server" Text = "旅游"/>
<asp:CheckBox ID="CheckBox8" runat="server" Text = "听音乐"/>
<br />
<br />
<br />
</div>
4.您对学习的态度是?<br />
<br />
<asp:RadioButton ID="RadioButton9" runat="server" Text = "非常热爱学习" GroupName = "Three"/>
<br />
<br />
<asp:RadioButton ID="RadioButton10" runat="server" Text = "定期学习" GroupName = "Three"/>
<br />
<br />
<asp:RadioButton ID="RadioButton11" runat="server" Text = "从来不学习" GroupName = "Three"/>
<br />
<br />
<br />
5.您平时喜欢听什么类型的歌曲?[多选]<br />
<div id = "div3" runat = "server">
<asp:CheckBox ID="CheckBox17" runat="server" Text = "民谣歌曲" />
<asp:CheckBox ID="CheckBox18" runat="server" Text = "说唱歌曲" />
<asp:CheckBox ID="CheckBox19" runat="server" Text = "流行歌曲" />
<br />
<br />
<asp:CheckBox ID="CheckBox20" runat="server" Text = "古典歌曲" />
<asp:CheckBox ID="CheckBox21" runat="server" Text = "重金属歌曲"/>
<asp:CheckBox ID="CheckBox22" runat="server" Text = "钢琴曲"/>
<br />
<br />
<br />
</div>
</div>
<p style = "margin-left:650px">
<asp:Button ID="Button1" runat="server" Text="提交" Height="39px" Width="81px"
onclick="Button1_Click" />
</p>
<p>
</p>
<span style = "margin-left:600px">----------------------------</span>
<p>
<asp:Label ID="Label1" runat="server" Text="" style = "margin-left:550px"></asp:Label>
</p>
<p>
<asp:Label ID="Label2" runat="server" Text="" style = "margin-left:550px"></asp:Label>
</p>
<p>
<asp:Label ID="Label3" runat="server" Text="" style = "margin-left:550px"></asp:Label>
</p>
<p>
<asp:Label ID="Label4" runat="server" Text="" style = "margin-left:550px"></asp:Label>
</p>
<p>
<asp:Label ID="Label5" runat="server" Text="" style = "margin-left:550px"></asp:Label>
</p>
</form>
</body>
</html>
2.后端代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class test05 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
CheckOne();
CheckTwo();
CheckThree();
CheckFour();
CheckFive();
}
protected void CheckOne()
{
if(RadioButton1.Checked)
{
Label1.Text = "您的学历是:" + RadioButton1.Text;
RadioButton1.Checked = false;
}
if (RadioButton2.Checked)
{
Label1.Text = "您的学历是:" + RadioButton2.Text;
RadioButton2.Checked = false;
}
if (RadioButton3.Checked)
{
Label1.Text = "您的学历是:" + RadioButton3.Text;
RadioButton3.Checked = false;
}
if (RadioButton4.Checked)
{
Label1.Text = "您的学历是:" + RadioButton4.Text;
RadioButton4.Checked = false;
}
}
protected void CheckTwo()
{
if (RadioButton5.Checked)
{
Label2.Text = "您的年级是:" + RadioButton5.Text;
RadioButton5.Checked = false;
}
if (RadioButton6.Checked)
{
Label2.Text = "您的年级是:" + RadioButton6.Text;
RadioButton6.Checked = false;
}
if (RadioButton7.Checked)
{
Label2.Text = "您的年级是:" + RadioButton7.Text;
RadioButton7.Checked = false;
}
if (RadioButton8.Checked)
{
Label2.Text = "您的年级是:" + RadioButton8.Text;
RadioButton8.Checked = false;
}
}
protected void CheckThree()
{
Label3.Text = "您的爱好是:";
foreach (Control cs in div_check.Controls)
{
if (cs is CheckBox)
{
CheckBox cb = (CheckBox)cs;
if (cb.Checked)
{
Label3.Text += cb.Text + ",";
cb.Checked = false;
}
}
}
}
protected void CheckFour()
{
if (RadioButton9.Checked)
{
Label4.Text = "您对学习的态度是:" + RadioButton9.Text;
RadioButton9.Checked = false;
}
if (RadioButton10.Checked)
{
Label4.Text = "您对学习的态度是:" + RadioButton10.Text;
RadioButton10.Checked = false;
}
if (RadioButton11.Checked)
{
Label4.Text = "您对学习的态度是:" + RadioButton11.Text;
RadioButton11.Checked = false;
}
}
protected void CheckFive()
{
Label5.Text = "您喜欢听:";
foreach (Control cs in div3.Controls)
{
if (cs is CheckBox)
{
CheckBox cb = (CheckBox)cs;
if (cb.Checked)
{
Label5.Text += cb.Text + ",";
cb.Checked = false;
}
}
}
}
}
总结:
1.单选题用的是RadioButton组件,并且要加上GroupName样式,才能实现单选,否则其他选项也可以勾选上。
2.写多选题的时候用一个div标签嵌套整个代码块,并在后端遍历这个div,来实现输出多选题选项的结果。
3.您平时的爱好是?[多选]<br />
<br />
<div id = "div_check" runat = "server">
<asp:CheckBox ID="CheckBox1" runat="server" Text = "爬山"/>
<asp:CheckBox ID="CheckBox2" runat="server" Text = "玩游戏"/>
<asp:CheckBox ID="CheckBox3" runat="server" Text = "看书"/>
<asp:CheckBox ID="CheckBox4" runat="server" Text = "唱歌"/>
<br />
<br />
<asp:CheckBox ID="CheckBox5" runat="server" Text = "跳舞"/>
<asp:CheckBox ID="CheckBox6" runat="server" Text = "看电影"/>
<asp:CheckBox ID="CheckBox7" runat="server" Text = "旅游"/>
<asp:CheckBox ID="CheckBox8" runat="server" Text = "听音乐"/>
<br />
<br />
<br />
</div>
protected void CheckThree()
{
Label3.Text = "您的爱好是:";
foreach (Control cs in div_check.Controls)
{
if (cs is CheckBox)
{
CheckBox cb = (CheckBox)cs;
if (cb.Checked)
{
Label3.Text += cb.Text + ",";
cb.Checked = false;
}
}
}
}
3.每个选项可以单独写一个判断选项的方法,然后在button按钮的后端代码直接按顺序调用对应方法。