当前位置:编程学习 > C#/ASP.NET >>

求代码..如何让CheckBoxList选择后, 加在TextBox里

--------------------编程问答--------------------

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        txt1.Text = "";
        foreach (ListItem item in CheckBoxList1.Items)
        {
            if (item.Selected)
                txt1.Text += item.Text;
        }
    }
--------------------编程问答-------------------- 楼上的大哥, 你写的这个是可以运行的----------但是只实现了一半:添加, 如何去掉呢

比如说我去掉b, textbox里就少了个b --------------------编程问答-------------------- LZ对1楼的代码理解错了。
txt1.Text = "";
明显是将TextBox清空了。然后遍历CheckBoxList重新生成你要的字符串。 --------------------编程问答-------------------- foreach (ListItem item in CheckBoxList1.Items)
        {
            if (item.Selected)
                txt1.Text += item.Text+",";
            else txt1.Text= txt1.Text.Replace(item.Text+",","");
        }
--------------------编程问答-------------------- 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;
}
--------------------编程问答-------------------- 正解。。。。。。
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;
            }
        }

    }
}
--------------------编程问答-------------------- 楼主消失了。。。。。。。。 --------------------编程问答-------------------- 已经给了答案了 帮顶~ --------------------编程问答--------------------
引用 5 楼 vvolf_28 的回复:
2楼给出了服务器端方法,我这里再放出一个客户端JS方式。


C# code<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>JScript codefunction 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;
}


很好啊   顶一个 --------------------编程问答--------------------
引用 1 楼  的回复:
C# code

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        txt1.Text = "";
        foreach (ListItem item in CheckBoxList1.Items)
        {
            if (……
你这个可以加入选择的值但是没法去除取消选择的值 --------------------编程问答--------------------
引用 4 楼  的回复:
foreach (ListItem item in CheckBoxList1.Items)
        {
            if (item.Selected)
                txt1.Text += item.Text+",";
            else txt1.Text= txt1.Text.Replace(item.Text+",","")……
这个是可以的 
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,