本帖最后由 napoleon1769 于 2010-03-09 16:48:43 编辑

解决方案 »

  1.   


    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            txt1.Text = "";
            foreach (ListItem item in CheckBoxList1.Items)
            {
                if (item.Selected)
                    txt1.Text += item.Text;
            }
        }
      

  2.   

    楼上的大哥, 你写的这个是可以运行的----------但是只实现了一半:添加, 如何去掉呢比如说我去掉b, textbox里就少了个b
      

  3.   

    LZ对1楼的代码理解错了。
    txt1.Text = "";
    明显是将TextBox清空了。然后遍历CheckBoxList重新生成你要的字符串。
      

  4.   

    foreach (ListItem item in CheckBoxList1.Items)
            {
                if (item.Selected)
                    txt1.Text += item.Text+",";
                else txt1.Text= txt1.Text.Replace(item.Text+",","");
            }
      

  5.   

    2楼给出了服务器端方法,我这里再放出一个客户端JS方式。<asp:CheckBoxList ID="chklstUserPass" runat="server" onClick="Test()">
        <asp:ListItem Value="1" Text="a"></asp:ListItem>
        <asp:ListItem Value="2" Text="b"></asp:ListItem>
        <asp:ListItem Value="3" Text="c"></asp:ListItem>
    </asp:CheckBoxList>
    <asp:TextBox ID="txt1" runat="server"></asp:TextBox>function Test(){
    var str = "";if(document.getElementById("cblSiteStyle_0").checked){
        str = str + document.getElementById("cblSiteStyle_0").nextSibling.innerText);
    }if(document.getElementById("cblSiteStyle_1").checked){
        str = str + document.getElementById("cblSiteStyle_1").nextSibling.innerText);
    }if(document.getElementById("cblSiteStyle_2").checked){
        str = str + document.getElementById("cblSiteStyle_2").nextSibling.innerText);
    }document.getElementById("txt1").value = str;
    }
      

  6.   

    正解
    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 Default4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void chklstUserPass_SelectedIndexChanged(object sender, EventArgs e)
        {
            txt1.Text = "";
            foreach (ListItem item in chklstUserPass.Items)
            {
                if (item.Selected)
                {
                    txt1.Text += item.Text;
                }
            }    }
    }