vs2015 dynamicweb4-3

webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="dynamicweb4_3.WebForm1" %>

<!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>
        <h3>个人信息</h3>
        <table border="1">
            <tr>
                <td>联系方式:</td>
                <td>
                    <asp:CheckBox ID="chbQQ" runat="server" Text="QQ" />
                    <asp:CheckBox ID="chbWeChat" runat="server" Text="微信" />
                    <asp:CheckBox ID="chbPhone" runat="server" Text="手机" />
                </td>
            </tr>
            <tr>
                <td>爱好:</td>
                <td>
                    <asp:CheckBoxList ID="chbHobby" runat="server" RepeatDirection="Horizontal" RepeatColumns="3">
                        <asp:ListItem>阅读</asp:ListItem>
                        <asp:ListItem>运动</asp:ListItem>
                        <asp:ListItem>电影</asp:ListItem>
                        <asp:ListItem>音乐</asp:ListItem>
                        <asp:ListItem>旅游</asp:ListItem>
                        <asp:ListItem>上网</asp:ListItem>
                    </asp:CheckBoxList>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="btnSubmit" runat="server" Text="确定" OnClick="btnSubmit_Click" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Label ID="lblTips" runat="server" Text=""></asp:Label>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

webform1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace dynamicweb4_3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            lblTips.Text = "";
            string strContact = "";
            if (chbQQ.Checked)
            {
                strContact += chbQQ.Text + ",";
            }
            if (chbWeChat.Checked)
            {
                strContact += chbWeChat.Text + ",";
            }
            if (chbPhone.Checked)
            {
                strContact += chbPhone.Text + ",";
            }
            if (strContact == "")
            {
                lblTips.Text += "联系方式:未进行选择!<br>";
            }
            else
            {
                lblTips.Text += "联系方式:" + strContact.Substring(0,strContact.Length-1) + "<br>";
            }

            string strHobby = "";
            for (int i = 0; i < chbHobby.Items.Count; i++)
            {
                if (chbHobby.Items[i].Selected)
                {
                    strHobby += chbHobby.Items[i].Text + ","; //注意,逗号是中文格式的
                }
            }

            if (strHobby == "")
            {
                lblTips.Text += "爱好:未进行选择!<br>";
            }
            else
            {
                lblTips.Text += "爱好:" + strHobby.Substring(0,strHobby.Length-1) + "<br>";
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/modern358/article/details/115391678