C# 如何获取checkboxlist控件中的选定项

C# 如何获取checkboxlist控件中的选定项,c#,asp.net,checkboxlist,C#,Asp.net,Checkboxlist,我在my.aspx中使用了两个checkboxlist控件,即chklstearnings、chklstdeclarations page和am使用数据集将数据绑定到复选框列表。现在,当我尝试获取所选项目时,我无法这样做 以下是我的数据绑定代码: page_load { MySqlConnection con= new MySqlConnection(System.Configuration.ConfigurationSettings.AppSettings.Get("connectio

我在my.aspx中使用了两个checkboxlist控件,即chklstearnings、chklstdeclarations page和am使用数据集将数据绑定到复选框列表。现在,当我尝试获取所选项目时,我无法这样做

以下是我的数据绑定代码:

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();
}
为所选项目单击“代码输入”按钮:

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
      }
   }
}
现在我的问题是,我在ded和ear中获得了项目的名称,但所选的属性在所有方面都显示为错误,不考虑选择


感谢您在adv中再次绑定复选框,因为您没有放入
IsPostBack
部分,因此它将再次绑定,您的选择将丢失由于您没有放入
IsPostBack
部分,复选框将再次绑定,因此,它将再次绑定,您的选择将丢失

请在页面加载中检查IsPostBack。因为当您单击按钮时,它正在重新加载页面。

请在页面加载中检查IsPostBack。因为当您单击按钮时,它正在重新加载页面。

请尝试在“页面加载方式”中编写代码

if (!IsPostBack)

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

if (!IsPostBack)

将复选框列表的isPostBack属性设置为true。并编写代码,以便在复选框列表的selectedindexchanged事件中从复选框列表中获取所选项目,前提是您希望在从复选框列表中选择某个项目后立即执行某项任务。谢谢。

将复选框列表的isPostBack属性设置为true。并编写代码,以便在复选框列表的selectedindexchanged事件中从复选框列表中获取所选项目,前提是您希望在从复选框列表中选择某个项目后立即执行某项任务。谢谢。

您可以发布aspx标记吗?可能原因EnableViewState在页面上可能为false。您可以发布aspx标记吗?可能原因EnableViewState在页面上可能为false。