如题,有很多的CheckBox,当选中其中一个或几个时要将CheckBox的值写到TextBox中,并且值之间要用逗号或分号隔开(因为要写到数据库的一个字段里面)。还需要当CheckBox的勾去掉时TextBox中对应的值也消失?上述要如何做到?请各位高手指点一下,谢谢!

解决方案 »

  1.   

    我做过一个类似的,是鼠标移动文本框上出来一个层,上面有很多checkbox 能够多选,多选用,相隔
    lz留个邮箱,明天发给你
      

  2.   

    不防用用CheckBoxList
    另外,保存CheckBoxList的选择值,是不用先保存在TextBox之后,再存的.
      

  3.   

    做一个循环,如果checkbox上选中的,就加入值
      

  4.   

    我写了一个, 调试通过, 你试一下:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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">
        <div>
            &nbsp;<asp:CheckBoxList ID="CheckBoxList1" runat="server" Height="235px" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged"
                Width="322px" AutoPostBack="True">
                <asp:ListItem>1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
                <asp:ListItem>3</asp:ListItem>
                <asp:ListItem>4</asp:ListItem>
            </asp:CheckBoxList>
            <br />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></div>
        </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 _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int len = CheckBoxList1.Items.Count;
            string str = string.Empty;
            for (int i = 0; i < len; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    str += CheckBoxList1.Items[i].Value.ToString() + ",";
                }
                TextBox1.Text = str;
            }
        }
    }
      

  5.   

    我邮箱[email protected],谢谢2楼。我想了一下,实际也确实不需要先到TextBox再存这样多此一举,直接存选中的值到一个字段好像就可以了,之间再用逗号或分号分隔一下。这样是不是更简单点,或许大家能给类似的代码看看。谢谢。
      

  6.   

    修改了一下, 去掉了最后一个逗号, 你参考一下:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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">
        <div>
            &nbsp;<asp:CheckBoxList ID="CheckBoxList1" runat="server" Height="235px" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged"
                Width="322px" AutoPostBack="True">
                <asp:ListItem>1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
                <asp:ListItem>3</asp:ListItem>
                <asp:ListItem>4</asp:ListItem>
            </asp:CheckBoxList>
            <br />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></div>
        </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 _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int len = CheckBoxList1.Items.Count;
            string str = string.Empty;
            for (int i = 0; i < len; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    str += CheckBoxList1.Items[i].Value.ToString() + ",";
                }
            }        if (str.Length == 0)
            {
                TextBox1.Text = "";
            }
            else
            {
                if (str.Substring(str.Length - 1, 1) == ",")
                {
                    TextBox1.Text = str.Substring(0, str.Length - 1);
                }
            }
        }
    }希望对你有帮助