【问题标题】:how to get the selected items in the checkboxlist control如何获取复选框列表控件中的选定项目
【发布时间】:2011-02-16 07:43:00
【问题描述】:

我在我的 .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 中获得了项目的名称,但是选择的属性在所有方面都显示错误,与选择无关

感谢广告

【问题讨论】:

  • 你能发布 aspx 标记吗?可能的原因 EnableViewState 在页面上可能为 false。

标签: c# asp.net checkboxlist


【解决方案1】:

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

if (!IsPostBack)

【讨论】:

    【解决方案2】:

    由于您没有输入IsPostBack部分,因此再次绑定Checkbox,因此将再次绑定,您的选择将丢失

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-10
          • 2011-09-25
          相关资源
          最近更新 更多