复选框编码错误 [英] error in checkbox coding

查看:45
本文介绍了复选框编码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ii要在数据库中存储多个选定的复选框...因此,请使用以下代码:-

 字符串 selectedItems = 字符串 .Empty;
         foreach (ListItem项目 in   this  .CheckBox1 .项目)
        {
            如果(已选中)
                selectedItems + =项目+ " ;
        }
        SqlCommand cmd =  SqlCommand("  + selectedItems +  ')",cn);
        cn.Open(); // 我假设您已写下连接字符串.
         int  iCount = cmd.ExecuteNonQuery();
        cn.Close(); 



bt错误是:-不包含"CheckBox1"的定义,找不到可以接受类型为第一个参数的扩展方法"CheckBox1" ....

m使用单个复选框,然后使用Repeater Control显示多个复选框.... so如何在我的代码中声明复选框????或代码中的错误是什么???

解决方案

尝试使用 CheckBoxList 控件,而不只是使用复选框+重复器.这是专门为此目的设计的控件.

参考: MSDN:CheckBoxList Web服务器控件声明性语法 [ ^ ]
MSDN:CheckBoxList类 [ Web应用程序的多选下拉列表 [ ^ ]

获取reapeter控件并为每一行执行循环.在每一行中,使用FindControl("Checkbox1")并将其强制转换为复选框对象,然后检查是否已选中该复选框.


首先,获取复选框(id = chkAdminList)代替复选框
然后使用它
for(int i = 0; i< chkAdminList.Items.Count; i ++)
{

如果(chkAdminList.Items [i] .Selected)
{

//在数据库中发送复选框值
}
其他
{
}
}


ii want to store multiple selected checkbox in database...so m using below code:-

string selectedItems = string.Empty;
        foreach (ListItem item in this.CheckBox1.Items)
        {
            if (item.Selected)
                selectedItems += item + ",";
        }
        SqlCommand cmd = new SqlCommand("Insert into Candidates(Candidate_Pref_Job_Cat) values('" + selectedItems + "')", cn);
        cn.Open(); // I am assuming that you have written down the connection string.
        int iCount = cmd.ExecuteNonQuery();
        cn.Close();



bt error is:- does not contain a definition for ''CheckBox1'' and no extension method ''CheckBox1'' accepting a first argument of type could be found....

m using single checkbox nd use Repeater Control to display multiple checkbox....so how to declare checkbox in my code??????or what is error in code????

解决方案

Try to use CheckBoxList control instead of just a checkbox+repeater. This is the control designed exactly for such purposes.

Refer: MSDN: CheckBoxList Web Server Control Declarative Syntax[^]
MSDN: CheckBoxList Class[^]

Alternatively, have a look at this control too: Multiselect Dropdown for Web Applications[^]


Get the reapeter control and do the loop for each row.From each row,use FindControl("Checkbox1") and type casting it to check box object then check whether the check box is selected or not.Based on that get the value of checked items.


firstly you take check box(id=chkAdminList) list in place of check box
then use it
for (int i = 0; i < chkAdminList.Items.Count; i++)
{

if (chkAdminList.Items[i].Selected)
{

//send check box value in the database
}
else
{
}
}


这篇关于复选框编码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆