我想循环遍历asp.net控件checkboxlist并检索已检查项的值,以便我可以在数据库中存储id

时间:2017-02-24 07:14:02

标签: c# asp.net

public void InsertHobbies()
{
        //foreach(object obj in chkEmpHobbyList.Controls)
        //{
        //    CheckBox chkbox = (CheckBox)obj;
        //    if(chkbox.Checked)
        //    {
        //        hobby objhobby = new hobby();

        //        objhobby.insertHobby((Convert.ToInt32(chkbox.ID)),empid);
        //    }
        //}

        foreach (ListItem listItem in chkEmpHobbyList.Items)
        {
            if (listItem.Selected)
            {
                hobby objhobby = new hobby();
                objhobby.insertHobby((Convert.ToInt32(/what to give/)), empid);
            }

        }
}

/ *** aspx代码**** /

<asp:CheckBoxList ID="chkEmpHobbyList" runat="server">
        </asp:CheckBoxList>

/ **使用数据库绑定列表** /

public List<hobby> GetHobbies()
    {
        DBlist obj = new DBlist();
        DataSet ds = obj.getdata("str_gethobby", null);
        List<hobby> hobbies = new List<hobby>();
        foreach (DataRow orow in ds.Tables[0].Rows)
        {
            hobby dlist = new hobby();
            dlist.HobbyName = orow["hobby_name"].ToString();
            dlist.Hobbyvalue = int.Parse(orow["id"].ToString());
            hobbies.Add(dlist);
        }
        return hobbies;
    }

/ **** /

 hobby dlist = new hobby();
    List<hobby> hobbies = dlist.GetHobbies();
    chkEmpHobbyList.DataSource = hobbies;
    chkEmpHobbyList.DataTextField = "HobbyName";
    chkEmpHobbyList.DataValueField = "Hobbyvalue";
    chkEmpHobbyList.DataBind();

我想循环遍历asp.net控件checkboxlist并检索检查项的值,以便我可以在数据库中存储id。如何获得已选中复选框的ID?

0 个答案:

没有答案