asp.net_完成一份简单的调查文件

这篇博客展示了如何使用ASP.NET进行前端和后端代码编写,设计一份包含单选和多选题的大学生调查问卷。用户选择的选项会在提交后显示在Label标签中。博客强调了RadioButton的GroupName属性在实现单选题中的作用,以及如何遍历div来获取多选题的选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录这几天的课程作业:

设计一份调查问卷,包括单选、多选题,把用户的选项显示在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"/>
&nbsp;&nbsp;
        <asp:RadioButton ID="RadioButton2" runat="server" Text = "硕士" GroupName="One"/>
        &nbsp;&nbsp;
        <asp:RadioButton ID="RadioButton3" runat="server" Text = "专科" GroupName="One"/>
         &nbsp;&nbsp;
         <asp:RadioButton ID="RadioButton4" runat="server" Text = "博士" GroupName="One"/>
        <br />
        <br />
        <br />
        2.您现在的年级是?<br />
        <br />
        <asp:RadioButton ID="RadioButton5" runat="server" Text = "大一" 
            GroupName="Two"/>
&nbsp;&nbsp;
        <asp:RadioButton ID="RadioButton6" runat="server" Text = "大二" GroupName="Two"
           />
&nbsp;&nbsp;
        <asp:RadioButton ID="RadioButton7" runat="server" Text = "大三" GroupName="Two"
            />
&nbsp;&nbsp;
        <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 = "爬山"/>
        &nbsp;&nbsp;&nbsp;
        <asp:CheckBox ID="CheckBox2" runat="server" Text = "玩游戏"/>
        &nbsp;&nbsp;&nbsp;
        <asp:CheckBox ID="CheckBox3" runat="server" Text = "看书"/>
        &nbsp;&nbsp;&nbsp;
        <asp:CheckBox ID="CheckBox4" runat="server" Text = "唱歌"/>
        <br />
        <br />
        <asp:CheckBox ID="CheckBox5" runat="server" Text = "跳舞"/>
        &nbsp;&nbsp;&nbsp;
        <asp:CheckBox ID="CheckBox6" runat="server" Text = "看电影"/>
        &nbsp;&nbsp;&nbsp;
        <asp:CheckBox ID="CheckBox7" runat="server" Text = "旅游"/>
        &nbsp;&nbsp;&nbsp;
        <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 />
&nbsp;<div id = "div3" runat = "server">
        <asp:CheckBox ID="CheckBox17" runat="server" Text = "民谣歌曲" />
        &nbsp;&nbsp;&nbsp;
        <asp:CheckBox ID="CheckBox18" runat="server" Text = "说唱歌曲" />
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:CheckBox ID="CheckBox19" runat="server" Text = "流行歌曲" />
              <br />
        &nbsp;&nbsp;&nbsp;
              <br />
        <asp:CheckBox ID="CheckBox20" runat="server" Text = "古典歌曲" />
            &nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="CheckBox21" runat="server" Text = "重金属歌曲"/>
&nbsp;&nbsp;&nbsp;&nbsp;
            <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>
         &nbsp;</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 = "爬山"/>
        &nbsp;&nbsp;&nbsp;
        <asp:CheckBox ID="CheckBox2" runat="server" Text = "玩游戏"/>
        &nbsp;&nbsp;&nbsp;
        <asp:CheckBox ID="CheckBox3" runat="server" Text = "看书"/>
        &nbsp;&nbsp;&nbsp;
        <asp:CheckBox ID="CheckBox4" runat="server" Text = "唱歌"/>
        <br />
        <br />
        <asp:CheckBox ID="CheckBox5" runat="server" Text = "跳舞"/>
        &nbsp;&nbsp;&nbsp;
        <asp:CheckBox ID="CheckBox6" runat="server" Text = "看电影"/>
        &nbsp;&nbsp;&nbsp;
        <asp:CheckBox ID="CheckBox7" runat="server" Text = "旅游"/>
        &nbsp;&nbsp;&nbsp;
        <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按钮的后端代码直接按顺序调用对应方法。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BeJav

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值