[导入]将多个复选框的值赋值给一个字符串或一个数组

后台主要代码:
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
    {
        string checkResult = "";
         ResultLabel.Text = "";
                
         if (A.Checked)
         {
             checkResult += A.ID + ",";
         }
         if (B.Checked)
         {
             checkResult += B.ID + ",";
         }
         if (C.Checked)
         {
             checkResult += C.ID;
         }
         ResultLabel.Text = checkResult.Trim();
    }

 protected void x_CheckedChanged(object sender, EventArgs e)
    {
        int[] strResult = new int[3];
        string str = "";
        xLabel.Text = "";
        for (int i = 0; i <strResult.Length; i++)
        {
            strResult[i] = 0;
        }
        //*        strResult[0] = 0;        strResult[1] = 0;        strResult[2] = 0;
      
        if (x.Checked)
        {
            strResult[0] = 1;           
           
        }
        if (y.Checked)
        {
            strResult[1] = 1;          
           
        }
        if (z.Checked)
        {
            strResult[2] = 1;                 
        }
     
        for (int i = 0; i < strResult.Length; i++)
        {
            str += strResult[i].ToString();
        }
          xLabel.Text = str;

    }

前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="获取复选框的值.aspx.cs" Inherits="获取复选框的值" %>

<!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">
       <p>      
            <asp:CheckBox ID="A"  runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox_CheckedChanged" Text="A" />
            <asp:CheckBox ID="B" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox_CheckedChanged" Text="B" />
            <asp:CheckBox ID="C" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox_CheckedChanged" Text="C" />
         </p>
        <p>
            <asp:Button ID="Result" runat="server" OnClick="Result_Click" Text="您选的是:" />
            <asp:Label ID="ResultLabel" runat="server"></asp:Label></p>
        <p>
            &nbsp;</p>
        <p>
            <br />
            <asp:CheckBox ID="CheckBox3" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox3_CheckedChanged"
                Text="洪川医药的医院评价系统很好" />
            <asp:Label ID="Label1" runat="server"></asp:Label></p>
        <p>
            <br />
            <asp:CheckBox ID="x" runat="server" AutoPostBack="True" OnCheckedChanged="x_CheckedChanged" Text="X"/>
            <asp:CheckBox ID="y" runat="server" AutoPostBack="True" OnCheckedChanged="x_CheckedChanged" Text="Y"/>
            <asp:CheckBox ID="z" runat="server" AutoPostBack="True" OnCheckedChanged="x_CheckedChanged" Text="Z" />
            <asp:Label ID="xLabel" runat="server"></asp:Label></p>
    </form>
</body>
</html>



后台全部代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class 获取复选框的值 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void CheckBox_CheckedChanged(object sender, EventArgs e)
    {
        string checkResult = "";
         ResultLabel.Text = "";
                
         if (A.Checked)
         {
             checkResult += A.ID + ",";
         }
         if (B.Checked)
         {
             checkResult += B.ID + ",";
         }
         if (C.Checked)
         {
             checkResult += C.ID;
         }
         ResultLabel.Text = checkResult.Trim();
    }
    protected void Result_Click(object sender, EventArgs e)
    {

    }
    protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
    {
       
        //这是当选中后激发的事件
        if (CheckBox3.Checked)//判断该控件是否被选中
        {//如果控件被选中就激发
            Label1.Text = "洪川医药的地址是<a href='http://www.hc115.com'>Www.Hc115.Com</a>";
        }
        else
        {//否则,当然是取消选择的时候了
            Label1.Text = "谢谢关心哈!!";
        }
    }
    protected void x_CheckedChanged(object sender, EventArgs e)
    {
        int[] strResult = new int[3];
        string str = "";
        xLabel.Text = "";
        for (int i = 0; i <strResult.Length; i++)
        {
            strResult[i] = 0;
        }
        //*        strResult[0] = 0;        strResult[1] = 0;        strResult[2] = 0;
      
        if (x.Checked)
        {
            strResult[0] = 1;           
           
        }
        if (y.Checked)
        {
            strResult[1] = 1;          
           
        }
        if (z.Checked)
        {
            strResult[2] = 1;                 
        }
     
        for (int i = 0; i < strResult.Length; i++)
        {
            str += strResult[i].ToString();
        }
          xLabel.Text = str;

    }
}



[新闻]Firefox联合搜狐推出奥运浏览器
文章来源:http://www.cnblogs.com/joyboy/articles/1167704.html
posted @ 2008-04-23 16:20  代码示例  阅读(470)  评论(0编辑  收藏  举报