Search This Blog

Tuesday, May 12, 2009

CheckBoxList

Populate CheckBoxList (List Control)

Creates a group of check boxes. Properties and events are identical to other list controls, such as DropDownList

Remove CheckBoxList Item By Item Text:

if (CheckBoxList1.Items.FindByText(itemValue) != null)
{
string itemText = CheckBoxList1.Items.FindByText(itemValue).Text;
ListItem li = new ListItem();
li.Text = itemText;
li.Value = itemValue;
Label1.Text = "Removed Item Is: " + itemText;
CheckBoxList1.Items.Remove(li);
}

Remove CheckBoxList Item By Item Value

if (CheckBoxList1.Items.FindByValue(itemValue) != null)
{
string itemText = CheckBoxList1.Items.FindByValue(itemValue).Text;
ListItem li = new ListItem();
li.Text = itemText;
li.Value = itemValue;
Label1.Text = "Item Found and remove: " + itemText;
CheckBoxList1.Items.Remove(li);
}

Add ListItem with Item Text and Value in CheckBoxList:


protected void Button1_Click(object sender, System.EventArgs e)
{
ListItem li = new ListItem();
li.Text = TextBox1.Text.ToString();
li.Value = TextBox2.Text.ToString();
CheckBoxList1.Items.Add(li);
Label1.Text = "ListItem added in CheckBoxList";

}

Use Theme & Skin In a Checkboxlist

runat="server"
BorderColor="Gray"
BorderWidth="2"
BackColor="White"
ForeColor="Black"
Font-Bold="true"
Font-Italic="true" RepeatColumns="3"
>

Programatically Bind CheckBoxList and Retrive Value with comma separator:

Bind:

chkInterest.DataSource = dataTable1
chkInterest.DataTextField = "Name"
chkInterest.DataValueField = "Id"
chkInterest.DataBind();

Retrieve:

protected void imgSubmit_Click(object sender, ImageClickEventArgs e)
{


string strInterests = "";
string strHobbies = "";


for (int i = 0; i < chkInterest.Items.Count; i++)
{
if (chkInterest.Items[i].Selected)
{
strInterests += chkInterest.Items[i].Text + ",";
}
}

strInterests = strInterests.Trim(new char[] { ',' });


for (int j = 0; j < chkListHobby.Items.Count; j++)
{
if (chkListHobby.Items[j].Selected)
{
strHobbies += chkListHobby.Items[j].Text + ",";
}

}

strHobbies = strHobbies.Trim(new char[] { ',' });


}

No comments: