ASP.NET中的动态复选框选中的事件 [英] Dynamic checkbox checked event in asp.net

查看:90
本文介绍了ASP.NET中的动态复选框选中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Friends

我写了一种创建动态复选框的方法,它将很好地工作.

现在,我要检查多少个复选框,并根据选中的复选框创建下拉列表.但是我无法创建它.

请帮帮我.

Hi Friends

I write a method to create dynamic check box and it will work fine.

Now I want to how many check boxes is checked and create drop down list with respect of checked check box. But I am unable to create it.

Please help me.

private void AddCheckboxes()
    {
        try
        {
            String sql = "";
            sql = "Select * from Item_type_mst";
            da.SelectCommand = new SqlCommand(sql, conn);
            conn.Open();
            da.SelectCommand.ExecuteReader();
            conn.Close();
            da.Fill(ds);
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            for (int intControlIndex = 0; intControlIndex <= dt.Rows.Count-1; intControlIndex++)
            {
                chkList1 = new CheckBox();
                chkList1.Text = dt.Rows[intControlIndex][1].ToString(); 
                chkList1.ID = dt.Rows[intControlIndex][0].ToString();
                chkList1.Font.Name = "Verdana";
                chkList1.Font.Size = 9;
                PnlControl.Controls.Add(chkList1);
                PnlControl.Controls.Add(new LiteralControl(""));
            }
        }
        catch (Exception exp)
        {
            throw new Exception(exp.Message);
        }
}


现在,我要检查选中了多少个复选框,然后进行计数并创建下拉列表.


Now I want to check how many check box are checked and count and create drop down list.

推荐答案

您可以使用面板控件来保存复选框的集合,而无需使用使用CheckboxList.它使您可以轻松地从任何数据源创建复选框列表,并且可以轻松地检索选定的项目.

您可以将数据源直接绑定到Checkboxlist并使用属性控制显示方向.
Rather than using a panel control to hold your collection of Checkboxes you can use a CheckboxList. It allows you to easily create a list of checkboxes from any datasource and you can easily retrive the selected items.

You can directly bind your data source to the Checkboxlist and control the display orientation using properties.
chkList.DataSource=dt;
chkList.DataBind()


以下代码说明了如何获取值-


The below code illustrates how to fetch the values -

ArrayList SelectedItems
foreach ListItem item in chkList.Items
{
    if (item.Selected) 
    {
        SelectedItems.Add(item.Text);
    }
}


HTH!

Dinesh
如果可行,请标记为解决方案!


HTH!

Dinesh
Mark as solution if this worked!


Hi Tiwari A K,

要从复选框列表中获取选定复选框的数量并将这些项目转移到下拉列表中,请执行ff:

Hi Tiwari A K,

To get the number of selected checkboxes from checkboxlist and to transfer those items to a dropdownlist, do the ff:

int ctr = 0;
foreach(ListItem chk in chkListItems.Items)
{
   ddlItems.Items.Add(chk.Value);
   ctr++;
}



希望你能明白.

如果可以解决您的问题,请将其标记为正确答案.

最好的问候,
爱德华



Hope you get the idea.

Mark this as correct answer if it solves your problem.

Best regards,
Eduard


这篇关于ASP.NET中的动态复选框选中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆