Getting the selected values in a checkbox list - c#

I have set up the following method when the checkbox list is checked.
protected void chk1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListItem list in chk1.Items)
{
if (list.Selected)
{
string name = list.Value.ToString();
}
}
}
I need to display the checked item from the checkbox list. However, for each iteration the selected attribute always comes false. It never satisfies the condition
if (list.Selected)
{
string name = list.Value.ToString();
}
How do I fix this?

Try something like this
var selectedListItems = chk1.Items.Cast<ListItem>().Where(x => x.Selected);
or in your case
var list = chk1.Items.Cast<ListItem>().Where(x => x.Selected);
now you will have a Collection that you can check / code against
also make sure that this code is being fired and or check if there is a PostBack
you can check this by checking if(!Is.PostBack){ }

My money is on you are re-binding the controls on every postback, instead do this:
if (!Page.IsPostBack)
{
// Only bind controls on initial page and let viewstate remember what the user did
}

Related

How to get id of the selected item in combobox and populate another combobox with it?

I have 2 comboboxes created with Ajax Toolkit. One of them has a list of systems. I wanted to fill the other combobox with a list of subsystems whenever a system is selected. I did not use DisplayMember or ValueMember and most examples are using them.
.aspx side just in case:
<ajaxToolkit:ComboBox ID="cbox1" runat="server" OnSelectedIndexChanged="cbox1_SelectedIndexChanged" />
Is this doable with what I'm trying? Is the event I used correct for the situation?(I guess not,but other events seem to be unrelated) Let me show you the code:
protected void fillSystemCombo()
{
var sysOperations = new ModelOperations.ConstantSystem.ConstantSystemOperations();
var sys = sysOperations.GetSystemList().TransactionResultList;
foreach (var item in sys)
{
cbox1.Items.Add(item.description);
}
}
This works fine and I can see the systems in my first combobox.
This is what I tried for populating the second one:
protected void cbox1_SelectedIndexChanged(object sender, EventArgs e)
{
var subSysOperations = new ModelOperations.ConstantSubSystem.ConstantSubSystemOperations();
int index = Convert.ToInt32(cbox1.SelectedItem.Value);//i think this should get the id...
var subsys = subSysOperations.GetSubSystemList().TransactionResultList;
foreach (var item in subsys)
{
if (item.sysID == index)
{
cbox2.Items.Add(item.description);
}
}
}
sysID is the foreign key in SubSystem which is the ID of System. By the way, my SelectedIndexChanged event never fired when I was debugging the program even though I clicked on an item in combobox.
I've actually found the answer after carefully reading the parameters taken by Items.Add. It wants a ListItemso if I create a ListItem inside the loop I can finally add my items with both a Text and a Value like this:
foreach (var item in sys)
{
combo1.Items.Add(new ListItem { Text = item.description, Value = item.ID.ToString() });
}
After that I can get the index with
int index = Convert.ToInt32(combo1.SelectedValue);

Dropdown List not returning the correct value

i am trying to retrieve the value on a previously databinded DropDownList like this:
<asp:DropDownList ID="DropDownListReception" runat="server" CssClass="span3 drop-down-reception"
OnPreRender="DropDownListReception_PreRender" OnSelectedIndexChanged="DropDownListReception_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
var receptions = BLLFactory.ReceptionBLL.GetListAll();
DropDownListReception.DataSource = receptions;
DropDownListReception.DataBind();
}
On the DropDown PreRender i am personalizing this DropDown like this:
protected void DropDownListReception_PreRender(object sender, EventArgs e)
{
if (DropDownListReception.DataSource != null)
{
DropDownListReception.Items.Clear();
DropDownListReception.Items.Add(new ListItem("-- Select --", "NA"));
foreach (Reception item in (DropDownListReception.DataSource as IEnumerable))
{
DropDownListReception.Items.Add(new ListItem(item.Name + " " + item.Number, item.Id.ToString()));
}
}
}
this is working perfectly, my DropDown loads as it should, my problem is when i try to retrieve the SelectedValue in the SelectedIndexChanged event, it wont return the value as a string but as a type, what i am doing is:
protected void DropDownListReception_SelectedIndexChanged(object sender, EventArgs e)
{
//CurrentReception is a string i want to save in ViewState
//I also tried (sender as DropDownList).SelectedValue
//Tried DropDownListReception.SelectedValue
CurrentReception = DropDownListReception.SelectedItem.Value;
}
but this "DropDownListReception.SelectedItem.Value" will always return "Reception" which is the type of the item, not the id i assigned as the item value in the PreRender event. This also happens if i do this: "DropDownListReception.SelectedItem.Text", this also return "Reception". How can i return the string Value i assigned to the DropDown item?
var CurrentReception = DropDownListReception.SelectedItem as Reception;
string val = CurrentReception.PropertyYouNeed;
DropDownListReception.SelectedItem.Text and DropDownListReception.SelectedItem.Value will return the value of the selection, which is the second term in the ListItem used when you add it to the list. In other words, the problem is with item.Id.ToString(). It returns the type of the object instead of the ID. I'm not sure what your item object actually consists of so I'm not sure what you actually need, but are you sure it's not just item.Id? ToString() is generally the string representation of the object, if item.Id is an int, then ToString should give you the string equivalent of that int... but the fact that it doesn't work suggests item.Id is not actually an int.
I think you need to cast the list item to the type that you stored in it (Reception), then access the property from the Reception object that you want (from your description it sounds like you want the id). Like this:
protected void DropDownListReception_SelectedIndexChanged(object sender, EventArgs e)
{
//CurrentReception is a string i want to save in ViewState
CurentReception = ((Reception)DropDownListReception.SelectedItem).Id.ToString();
}
I figured it out, i was DataBinding the DropDownList on the PageLoad, which fires before the SelectedIndexChanged event. Since the DropDown does a PostBack when its value changes, the PageLoad was "Recreating" the DropDown and i was losing the changes before getting to the SelectedIndexChanged code.
Thank you all for your answers.:)

How to deselect a single Item in a listbox

I have a listbox on my asp page, and I was wondering if there was anyway in code behind or javascript to deselect a selected item in my listbox. the selection mode is single.
Any help please? I tried adding a handler on selectedindexchanged...but it doesn't hit it if I am clicking on the same selectedItem.
I fixed my issue as Andrew pointed out, by setting the SelectedIndex = -1;
But, this created weird behavior since now I would have A checkboxList being populated from the selected list item I chose, and there was no Idea to see which list item populated it after wards.
So What I did was added 2 more lines of code on top of what andrew suggested and it worked great.
var index = lstGroups.SelectedIndex;
lstGroups.SelectedIndex = -1;
lstGroups.Items[index].Attributes["style"] = "background-color:lightblue";
Thanks for pushing me in the right direction!
You can use the following workaround if your SelectionMode="Single" and you want to deselect an item on second click.
Create a property in your page to keep the last selected list item value using session. I am assuming the selected value in your listbox is always int
public int ListBoxLastSelected
{
get
{
if (Session["ListBoxLastSelected"] != null)
return Convert.ToInt32(Session["ListBoxLastSelected"]);
return -1;
}
set { Session["ListBoxLastSelected"] = value; }
}
Create the following method
private void EnableListboxDeselect()
{
if (!IsPostBack)
{
// Register a postback event whenever you click on the list item
ClientScriptManager cs = Page.ClientScript;
lstMyList.Attributes.Add("onclick", cs.GetPostBackEventReference(lstMyList, "clientClick"));
Session["ListBoxLastSelected"] = null;
}
if (IsPostBack)
{
// Ensure the postback is from js side
if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "clientClick")
{
if (CorpListLastSelected == Convert.ToInt32(lstMyList.SelectedValue))
{
lstMyList.ClearSelection();
CorpListLastSelected = -1;
}
else
{
ListBoxLastSelected= Convert.ToInt32(lstMyList.SelectedValue);
}
}
}
}
On your Page_Load add the below code
protected void Page_Load(object sender, EventArgs e)
{
EnableListboxDeselect();
}

ASP.Net: GridView control and combobox woes

I've got a GridView control and Combobox control that are both successfully populated in my Page_Load event (within a block that checks for IsPostBack == false).
I've got an empty button 'btnClick' event handler which will reload the page when clicked. Both the GridView and Combobox controls have their EnableViewState property set to True. The behaviour I was expecting and hoping for was:
Page will reload with the GridView control still populated.
Page will reload with Combobox still populated and the item selected by the user still set as the selected item.
Unfortunately, the behaviour I'm getting is as follows:
GridView control is now empty and shows no data.
Combobox is now empty.
Code as follows:
public MyPage()
{
this.Load += new EventHandler(Page_Load);
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
DataAccessObj daObj = new DataAccessObj();
foreach (DataRow dataRow in daObj.GetAllData())
{
ListItem listItem = new ListItem(dataRow.ToString(), dataRow.Id.ToString());
myCombobox.Items.Add(listItem);
}
IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
IncidentGrid.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Do nothing
}
What I would like to do is allow the user to select an item from the Combobox. Upon clicking Submit, the GridView would be repopulated (based upon the selected item). The Combobox will remain populated and show the last selected item.
Can someone help explain where I might be going wrong? TIA
When you click your button, the page is posted back, in your page load, if it is a postback you need to databind the grid appropriately you need to add a condition to your page load event like
Firstly on your btn_click, you need to store the id selected with something like:
if (myCombobox.SelectedItem != null)
{
if (int.TryParse(myCombobox.SelectedItem.Value, out reportedById) == false)
{
reportedById = 0;
ViewState["reportedById"] = reportedById; // Need to remember which one was selected
}
}
Then On your Post Back
else (IsPostBack)
{
if (ViewState["reportedById"]) != null)
{
IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(Convert.ToInt32(ViewState["reportedById"]));
IncidentGrid.DataBind();
myCombobox.SelectedItem.Value = ViewState["reportedById"].ToString(); // set combo
}
else
{
IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
IncidentGrid.DataBind();
}
}

How to Get Updates(changed Items) From CheckboxList?

I have a Checkbox list in my page and its datasource set programatically in PreLoad() event:
protected void Page_PreLoad()
{
if (!Page.IsPostBack)
{
CheckBoxList1.DataSource = NoK.AcceptedNoks((Guid)Membership.GetUser().ProviderUserKey);
CheckBoxList1.DataTextField = "FullName";
CheckBoxList1.DataValueField = "NoKId";
CheckBoxList1.DataBind();
}
foreach (ListItem chk in CheckBoxList1.Items)
{
if (PrivateMessage.HasAccess(Request.QueryString["MessageId"], chk.Value))
{
chk.Selected = true;
}
}
}
as you see in foreach will check for whether an item must be checked or Not. and it works nice. this means that end-user can Edit list Items and by default some of Item has been checked. now I want to get items by clicking a Button:
protected void UpdateRightBtn_Click(object sender, EventArgs e)
{
var SelectedNokIds =
CheckBoxList1.Items
.OfType<ListItem>()
.Where(li =>
li.Selected == true)
.Select(l => new Guid(l.Value));
}
but the Items in SelectedNokIds are still Old Items and if user change checkboxes no effect apeares in SelectedNokIds. Why???
Please Help!
It looks like it is because you are re-setting the values again at postback, effectively clearing the user's selection. You need to only initialize the values when it is not a postback.
foreach (ListItem chk in CheckBoxList1.Items){ if (PrivateMessage.HasAccess(Request.QueryString["MessageId"], chk.Value)) { chk.Selected = true; }}
This line fires on every page load, so that would reset the selection (at least the ones that set selected to true). Shouldn't that be within !Page.IsPostback too? And in the update button, you could rebind there...
If you need to figure out what changed, you need to query the items in the data source again, and cross-reference those against the new selection list.

Categories