如何选中CheckBox就将其值写到TextBox中? 如题,有很多的CheckBox,当选中其中一个或几个时要将CheckBox的值写到TextBox中,并且值之间要用逗号或分号隔开(因为要写到数据库的一个字段里面)。还需要当CheckBox的勾去掉时TextBox中对应的值也消失?上述要如何做到?请各位高手指点一下,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我做过一个类似的,是鼠标移动文本框上出来一个层,上面有很多checkbox 能够多选,多选用,相隔lz留个邮箱,明天发给你 不防用用CheckBoxList另外,保存CheckBoxList的选择值,是不用先保存在TextBox之后,再存的. 做一个循环,如果checkbox上选中的,就加入值 我写了一个, 调试通过, 你试一下:<%@ 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> <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; } }} 我邮箱[email protected],谢谢2楼。我想了一下,实际也确实不需要先到TextBox再存这样多此一举,直接存选中的值到一个字段好像就可以了,之间再用逗号或分号分隔一下。这样是不是更简单点,或许大家能给类似的代码看看。谢谢。 修改了一下, 去掉了最后一个逗号, 你参考一下:<%@ 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> <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); } } }}希望对你有帮助 有木精于验证码识别的朋友给点资料?关于天涯验证码 一个层样式丢失问题 在vs2008中,使用 TreeView 的问题 帮我分析下这个站的技术 引用Dll出现"不能是外部了,无法声明主体"错误 高手看看我的web.config配置那里出问题了 =========请问以下两种方法有什么不同? 简单问题:我的DataList控件实现ItemDataBound事件后,代码见内: !!如何实现textbox与数据库的绑定? 请问关闭IE窗口时候触发的事件是什么? 页面上的事件又不能执行了 ??/ 求救``````````````` 散分求救!!关于批量读取文件的问题!!!
lz留个邮箱,明天发给你
另外,保存CheckBoxList的选择值,是不用先保存在TextBox之后,再存的.
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<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;
}
}
}
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<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);
}
}
}
}希望对你有帮助