2011-02-16 55 views
1

我正在使用2个复选框列表控件,即我的.aspx 页面中的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和耳朵获取项目的名称但选择的财产是所有的方式显示错误的不选择

在此结果合作伙伴敬请致电

+0

你能发布aspx标记吗?可能的原因EnableViewState在页面上可能为false。 – 2011-02-16 07:47:53

回答

2

尝试写在页面加载代码由您的CheckBoxList的

if (!IsPostBack) 
1

由于您没有放置IsPostBack部分,复选框会再次绑定,因此它会再次被绑定,并且您的选择将会丢失

1

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

0

设置IsPostBack属性为真。并编写代码以便从复选框列表的selectedindexchanged事件中的复选框列表中选择项目,如果您希望在您从复选框列表中选择某个项目后立即执行某项任务。谢谢。

相关问题