[已解决问题] checkbox 如何取值?
提问时间: 2008-08-25 09:53
悬赏分:5 浏览:448 次

VS2005 的控件 checkbox 有多个选项,我想把选出来得值存入数据库中,

请问大家谁会啊??请尽快和我联系啊。。

eg: 比如说 table 表中 type 字段 是 varchar

页面上得类型是 类型: 飞机  大巴

怎样把页面上得值读出来插入数据库呢 

提问者:冰儿 - 菜鸟二级
最佳答案
感觉你这个应该是多选吧, 为什么不用CheckboxList控件呢 页面: 添加了CheckBoxList 控件和一个Button控件 <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal"> <asp:ListItem>飞机</asp:ListItem> <asp:ListItem>坦克</asp:ListItem> <asp:ListItem>大巴</asp:ListItem> </asp:CheckBoxList> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> 代码: 在Button的Click事件中遍历的CheckBoxList的Itme protected void Button1_Click(object sender, EventArgs e) { ArrayList al = new ArrayList(); for (int i = 0; i < this.CheckBoxList1.Items.Count; i++) { if (this.CheckBoxList1.Items[i].Selected) { al.Add(this.CheckBoxList1.Items[i].Value); } } }
2008/8/25 10:24:06 回答者:一杰


提问者对于答案的评价:谢谢啊。你得代码写得太棒了
其它回答(7)
checkbox.text
2个月前   回答者:小眼睛老鼠 - 小虾三级
服务器控件就用foreach HTML控件就用request.form
2个月前   回答者:丁学 - 专家六级
foreach (string s in Request.Form.AllKeys) { controlId = "cb" + strRID; if (s.Substring(s.Length - controlId.Length, controlId.Length) == controlId) { if (Request.Form[s] == "on") { //dv[i]["State"] = 1; state = true; } } }
2个月前   回答者:绝版色狼 - 菜鸟二级
.aspx <input type='checkbox' name='cbType' value='typeValue' />text 。。。 .cs string cb=Request.Form['cbType'];//cb的值为“1,2,3”选择多个值将以逗号隔开
2个月前   回答者:wsky - 菜鸟二级
循环取.selected=true的值不就行了?
2个月前   回答者:快乐的海盗 - 小虾三级
前台循环绑定HTML控件 <script language="javascript" type="text/javascript"> function aaa() { var s=""; var v=""; var n=""; var index1="cbk_"; s=document.getElementsByName("qq"); for(var x=0;x<s.length;x++) { if(s[x].checked==true) { v=v+s[x].value+","; } else { n=n+s[x].value+","; } } document.getElementById("hdf_id").value=v; document.getElementById("hdf_nid").value=n; // document.write(w); // document.write(d); } </script> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td></td> <td></td> </tr> </table> <table> <tr> <td>Username</td> <% DBaccess DBA = new DBaccess(); int counti = DBA.countitems(); DV2 = DBA.selectitems(); DV3 = DBA.selectusers(); for (int i = 0; i < counti; i++) { %> <td><%=HttpUtility.HtmlAttributeEncode(DV2[i]["itemname"].ToString())%></td> <% } %> </tr> <% int countu = DBA.countuser(); for (int i = 0; i < countu; i++) { %> <tr> <% for (int j = 0; j < counti + 1; j++) { %> <% if (j == 0) { %> <td><label><%=HttpUtility.HtmlAttributeEncode(DV3[i]["username"].ToString())%></label></td> <% } %> 代码太多发布不出来
2个月前   回答者:飘忽忽的雲 - 初学一级
美女就是受欢迎哦,这么多人抢肉
2个月前   回答者:老尧 - 老鸟四级
评论
2个月前   冰儿 :
我的意思是谁会写前台和后太得代码啊。
给我一个例子让我看看也成啊
2个月前   飘忽忽的雲 :
我做的事一个简单的体检项目也就是一个TABLE 竖着的第一列显示人名 横着第一行显示体检的所有项目其他位置全是CHECBOX 体检哪项哪个就是挑。
2个月前   飘忽忽的雲 :
前台循环绑定
<script language="javascript" type="text/javascript">
function aaa()
{
var s="";
var v="";
var n="";
var index1="cbk_";

s=document.getElementsByName("qq");



for(var x=0;x<s.length;x++)
{
if(s[x].checked==true)
{
v=v+s[x].value+",";
}
else
{
n=n+s[x].value+",";
}
}

document.getElementById("hdf_id").value=v;
document.getElementById("hdf_nid").value=n;
// document.write(w);
// document.write(d);


}

</script>
</head>


<body>

<form id="form1" runat="server">
<div>
<table>
<tr>
<td></td>
<td></td>
</tr>
</table>
<table>
<tr>
<td>Username</td>
<%
DBaccess DBA = new DBaccess();
int counti = DBA.countitems();

DV2 = DBA.selectitems();
DV3 = DBA.selectusers();
for (int i = 0; i < counti; i++)
{
%>
<td><%=HttpUtility.HtmlAttributeEncode(DV2[i]["itemname"].ToString())%></td>
<%
}
%>
</tr>

<%
int countu = DBA.countuser();
for (int i = 0; i < countu; i++)
{

%>
<tr>
<%
for (int j = 0; j < counti + 1; j++)
{
%>
<%
if (j == 0)
{
%>
<td><label><%=HttpUtility.HtmlAttributeEncode(DV3[i]["username"].ToString())%></label></td>
<%
}
%>

2个月前   飘忽忽的雲 :
后台从数据库读值
并给asp:Button 加一个JS的方法
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;
using System.Data.SqlClient;

public partial class Relation2 : System.Web.UI.Page
{
DBaccess DBA = new DBaccess();
DataView DV = new DataView();
DataView DV1 = new DataView();
public DataView DV2 = new DataView();
public DataView DV3 = new DataView();
public int countuser;
public int countitem;
public int item = 0;
public String[] indexs=new String[1000];

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{

countuser = DBA.countuser();
countitem = DBA.countitems();

DV2 = DBA.selectitems();
DV3 = DBA.selectusers();

}
}
protected void btn_submit_Click(object sender, EventArgs e)
{
check();
notcheck();
}
protected void btn_return_Click(object sender, EventArgs e)
{

string js = @"<Script language='JavaScript'>
history.go({0});
</Script>";
HttpContext.Current.Response.Write(string.Format(js, -2));
}
protected void check()
{
string id = hdf_id.Value;
string[] ids = id.Split(',');
for (int i = 0; i < ids.Length; i++)
{
insert(ids[i]);
}

}
protected void insert(string id)
{
DV2 = DBA.selectitems();
DV3 = DBA.selectusers();

for (int i = 0; i < DBA.countuser(); i++)
{
for (int j = 1; j < DBA.countitems()+1; j++)
{
string index1 = "cbk_";

2个月前   飘忽忽的雲 :
代码太长了MS发不全 如果觉得对你有用的话可以给我邮箱我发给你,加我QQ也行不果上班时间不在QQ 49373976
2个月前   冰儿 :
嘿嘿谢谢大家的帮助。。我很开心。。呵呵
   您需要登录以后才能回答!
 

我要提问

我的问题


快到期问题



> 问题排行榜

相关内容

相关链接