How to Get The Checkboxlist Selected Values, What I Have Doesn't Seem to Work C#.Net/Visualwebpart

How can I get the CheckBoxList selected values, what I have doesn't seem to work C#.NET/VisualWebPart

In your ASPX page you've got the list like this:

    <asp:CheckBoxList ID="YrChkBox" runat="server" 
onselectedindexchanged="YrChkBox_SelectedIndexChanged"></asp:CheckBoxList>
<asp:Button ID="button" runat="server" Text="Submit" />

In your code behind aspx.cs page, you have this:

    protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Populate the CheckBoxList items only when it's not a postback.
YrChkBox.Items.Add(new ListItem("Item 1", "Item1"));
YrChkBox.Items.Add(new ListItem("Item 2", "Item2"));
}
}

protected void YrChkBox_SelectedIndexChanged(object sender, EventArgs e)
{
// Create the list to store.
List<String> YrStrList = new List<string>();
// Loop through each item.
foreach (ListItem item in YrChkBox.Items)
{
if (item.Selected)
{
// If the item is selected, add the value to the list.
YrStrList.Add(item.Value);
}
else
{
// Item is not selected, do something else.
}
}
// Join the string together using the ; delimiter.
String YrStr = String.Join(";", YrStrList.ToArray());

// Write to the page the value.
Response.Write(String.Concat("Selected Items: ", YrStr));
}

Ensure you use the if (!IsPostBack) { } condition because if you load it every page refresh, it's actually destroying the data.

How to get values of selected items in CheckBoxList with foreach in ASP.NET C#?

Note that I prefer the code to be short.

List<ListItem> selected = CBLGold.Items.Cast<ListItem>()
.Where(li => li.Selected)
.ToList();

or with a simple foreach:

List<ListItem> selected = new List<ListItem>();
foreach (ListItem item in CBLGold.Items)
if (item.Selected) selected.Add(item);

If you just want the ListItem.Value:

List<string> selectedValues = CBLGold.Items.Cast<ListItem>()
.Where(li => li.Selected)
.Select(li => li.Value)
.ToList();

How to Access Programmatically Added checkbox

There are certain limitations:

  • You cannot access any controls in ASP.net which are not added before or on Init. So you can add the controls on Page OnInit, and then it will be accessible. But you have to be careful in adding the controls, so that they are added at right place and not duplicated.

  • You should not write

    CheckBox chk = (CheckBox)Page.FindControl("chk");

instead write

var chkControl = Page.FindControl("chk");
if(chkControl != null /*&& check type*/) {
CheckBox chk = (CheckBox)chkControl;
// and do something
}
  • It will be better if you use CheckBoxList Control at design time and change the binding values to populate different check boxes at runtime

  • Create check box(s) at design time and show hide based on user action.

Hope it helps

If ELSE condition not working properly

try something similar to:

for (i = 0; i < dtspmonthyear.Rows.Count; i++)
{
foreach (var item in cmbEmp_Name.Items)
{
if (item.Selected)
{
if (item.Text.Contains("PROCESSED"))
{
//CF.ExecuteQuerry("exec Emp_Resign_Allocate_Leave '" + str_emp_sel + "','" + dtspmonthyear.Rows[0]["month"].ToString() + "', '" + dtspmonthyear.Rows[0]["year"].ToString() + "'");
}
else
{
// not going in else for `PENDING`
}
}
}
}

Listview with databound CheckBox

It sounds like you need to take your databinding code and make sure it only fires on the initial page load (not on postbacks). Otherwise, you're reloading the ListView with the original data from your database before the click event has a chance to fire.

Something like this should work:

If(!Page.IsPostBack)
{
if (Session["IDValue"] != null)
{
int gen = Convert.ToInt32(Session["IDValue"]);
lstPreferences.DataSource = mdb.GetPreferences(gen);
lstPreferences.DataBind();
}
}


Related Topics



Leave a reply



Submit