简体   繁体   English

如何在checkboxlist控件中获取所选项目

[英]how to get the selected items in the checkboxlist control

I am using 2 checkboxlist controls namely chklstearnings,chklstdeductions in my .aspx page and am binding the data to the checkbox list using a dataset. 我正在使用2个checkboxlist控件,即我的.aspx页面中的chklstearnings,chklstdeductions,并使用数据集将数据绑定到复选框列表。 Now when I try to get the selected items am unable to do so. 现在,当我尝试获取所选项目时,我无法这样做。

Here is my code for data binding: 这是我的数据绑定代码:

page_load
{

   MySqlConnection con= new MySqlConnection(System.Configuration.ConfigurationSettings.AppSettings.Get("connectionString"));
   MySqlCommand com=con.CreateCommand();
   com.CommandText="select earningordeductiondescription,earningordeductioncode from tblearninganddeduction where earningordeductioncode between 1000 and 1999";
   com.CommandType=CommandType.Text;
   DataSet ds=new DataSet();
   MySqlDataAdapter da=new MySqlDataAdapter();
   da.SelectCommand=com;
   da.Fill(ds,"earnings");
   chklstEarnings.DataSource=ds.Tables["earnings"];
   chklstEarnings.DataTextField = "earningordeductiondescription";
   chklstEarnings.DataValueField="earningordeductioncode";
   chklstEarnings.DataBind();
   MySqlCommand com1 = con.CreateCommand();
   com1.CommandText = "select earningordeductiondescription,earningordeductioncode from tblearninganddeduction where earningordeductioncode between 2000 and 2999";
   com1.CommandType = CommandType.Text;       
   da.SelectCommand = com1;
   da.Fill(ds, "deductions");
   chklstdeductions.DataSource = ds.Tables["deductions"];
   chklstdeductions.DataTextField = "earningordeductiondescription";
   chklstdeductions.DataValueField = "earningordeductioncode";
   chklstdeductions.DataBind();
}

Code in button click for selected items: 按钮中的代码单击所选项目:

protected void btnsubmit_Click(object sender, EventArgs e)
{
   foreach  (ListItem ear in chklstEarnings.Items)
   {
      if (ear.Selected)
      {
         //save the earning prefarences
      }

   }

   foreach (ListItem ded in chklstdeductions.Items)
   {
      if (ded.Selected)
      {
         //save the deduction prefarences
      }
   }
}

now my prob is i am getting the name of the item in ded and ear but the property selected is all ways showing false irrespect of selection 现在我的问题是我得到了ded和ear中项目的名称,但所选择的属性是所有方式显示错误的选择

Thanks in adv 谢谢你

尝试在页面加载中编写代码

if (!IsPostBack)

复选框再次绑定,因为你还没有放入IsPostBack部分,所以它将再次绑定,你的选择将会丢失

Check IsPostBack in your page load. 检查页面加载中的IsPostBack。 Because when you are clicking the button it is reloading the page. 因为当您单击按钮时,它正在重新加载页面。

Set isPostBack property of your checkboxlist as true. 将checkboxlist的isPostBack属性设置为true。 and write code to get selected items from checkbox list in the selectedindexchanged event of your check box list if you want to perform some task as soon as you select some Item from your checkbox list. 如果要在复选框列表中选择某个项目后立即执行某项任务,请编写代码以从复选框列表的selectedindexchanged事件中的复选框列表中获取所选项目。 Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM