1

我在我的 .aspx 页面中使用 2 个复选框列表控件,即 chklstearnings、chklstdeductions,并使用数据集将数据绑定到复选框列表。现在,当我尝试获取所选项目时,我无法这样做。

这是我的数据绑定代码:

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 中获得了项目的名称,但是选择的属性是所有显示错误的方式,不考虑选择

感谢广告

4

4 回答 4

2

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

if (!IsPostBack)
于 2011-02-16T07:47:58.990 回答
1

Checkbox 再次绑定,因为你没有放IsPostBackpart,所以会再次绑定,你的选择会丢失

于 2011-02-16T07:47:12.580 回答
1

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

于 2011-02-16T07:47:56.467 回答
0

将复选框列表的 isPostBack 属性设置为 true。并编写代码以从复选框列表的 selectedindexchanged 事件中的复选框列表中获取所选项目,如果您想在从复选框列表中选择某些项目后立即执行某些任务。谢谢你。

于 2013-04-05T05:25:54.873 回答