item.Selected is false when the checkboxlist is disabled. - c#

I have a checkboxlist where certain items are disabled for certain users.
When I click on 'Save', the below code is executed.
foreach (ListItem item in myCheckBoxList.Items)
{
if (!item.Selected)
{
continue;
}
selectedValues.Add(item.Value);
}
However, item.Selected evaluates to false for the disabled items even though they're selected.
Is there a way to get around this?

Disabled inputs are never posted to the server, hence it will be set to default value, i.e. false. You can use HiddenField and associate that with each checkbox and set it's value based on it's selection.

If the ListItems of the CheckBoxList are added with aspnet, either in code behind or the .aspx page, ViewState will ensure that it will see those disabled checkboxes as checked, even if they are not send to the server.
protected void Button1_Click(object sender, EventArgs e)
{
List<string> selectedValues = new List<string>();
Label1.Text = "";
foreach (ListItem item in myCheckBoxList.Items)
{
if (item.Selected)
{
selectedValues.Add(item.Value);
Label1.Text += item.Value + "<br>";
}
}
}
And to make the example complete:
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<br /><br />
<asp:CheckBoxList ID="myCheckBoxList" runat="server">
<asp:ListItem Text="Blueberry" Value="Blueberry"></asp:ListItem>
<asp:ListItem Text="Raspberry" Value="Raspberry"></asp:ListItem>
<asp:ListItem Text="Blackberry" Value="Blackberry"></asp:ListItem>
<asp:ListItem Text="Strawberry" Value="Strawberry" Enabled="false" Selected="True"></asp:ListItem>
<asp:ListItem Text="Gooseberry" Value="Gooseberry" Enabled="false" Selected="True"></asp:ListItem>
</asp:CheckBoxList>
<br /><br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

Related

My panel fails to show if the user clicks a specific drop down list item

I am attempting to get a <div> to appear when a specific ListItem is selected.
In my code behind I have:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (reportedBefore.SelectedItem.Text=="yes")
{
reportedBeforePanel.Visible = true;
}
else
{
reportedBeforePanel.Visible = false;
}
}
I referred to this article here initially, which stated I needed a few things:
You need to Enable the AutoPostBack of the dropdownlist for raising the OnSelectedIndexChanged event on server side.
AutoPostBack="true"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged
Admittedly, I did not have an AutoPostBack before. After adding it, I am afraid for some reason the requested div still does not show.
<asp:DropDownList ID="reportedBefore" CssClass="larger-drop-2" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="Select" Value="Select"></asp:ListItem>
<asp:ListItem Text="No" Value="No"></asp:ListItem>
<asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
<asp:ListItem Text="Unsure" Value="Unsure"></asp:ListItem>
</asp:DropDownList>
<asp:Panel ID="reportedBeforePanel" runat="server" Visible="false">
<div id="showDiv">
<label for="yesDetails">
Please provide details
</label>
<asp:TextBox ID="yesDetails" CssClass="third-w-form" runat="server"/>
</div>
</asp:Panel>
Would someone be so kind to help me out here?
The problem is in the following if-condition:
reportedBefore.SelectedItem.Text=="yes"
By this, you are doing a case-sensitive string comparison (this is the default in .NET), but the values in your dropdownlist are written in a different way ("Yes" vs. "yes").
In order to fix this, either perform a case-insensitive string comparison
string.Compare(reportedBefore.SelectedItem.Text, "yes", true) == 0
or change the casing in the if-statement.
C# is case sensitive, so it's "Yes" not "yes":
reportedBeforePanel.Visible = reportedBefore.SelectedItem.Text == "Yes";
Alternatievely you can use this:
reportedBeforePanel.Visible = reportedBefore.SelectedItem.Text.Equals("yes", StringComparison.InvariantCultureIgnoreCase);

asp:label change visibility after hiding it

I've got an asp:Label and a asp:DropDownList that I want to be able to switch back and forth between visible and invisible when clicking on some buttons. Right now, my code looks like
aspx file
<asp:Label AssociatedControlID="statusFilter" id="statusFilterLabel" runat="server" CssClass="filterLabel">Status
<asp:DropDownList ID="statusFilter" runat="server" CssClass="filterInput" AutoPostBack="true" OnSelectedIndexChanged="anyFilter_SelectedIndexChanged" AppendDataBoundItems="True">
<asp:ListItem Selected="True" Value=" 0"><All></asp:ListItem>
</asp:DropDownList>
</asp:Label>
<asp:Button Text="ALL" ID="AllTabButton" CssClass="tabButton" runat="server" OnClick="AllTab_Click" />
<asp:Button Text="Arrived" ID="ArrivedTabButton" CssClass="tabButton" runat="server" OnClick="ArrivedTab_Click" />
code behind
protected void AllTab_Click(object sender, EventArgs e)
{
AllTabButton.CssClass = "tabButtonClicked";
ArrivedTabButton.CssClass = "tabButton";
statusFilter.Visible = true;
statusFilterLabel.Visible = true;
}
protected void ArrivedTab_Click(object sender, EventArgs e)
{
AllTabButton.CssClass = "tabButton";
ArrivedTabButton.CssClass = "tabButtonClicked";
statusFilter.Visible = false;
statusFilterLabel.Visible = false;
}
The only problem is that if I try to set Visible=true after setting Visible=false it would give me an error Unable to find control with id 'statusFilter' that is associated with the Label 'statusFilterLabel'.
I tried doing some other things instead of using Visible, like setting the style: statusFilter.Style.Add("display", "block") and setting the cssclass: statusFilter.CssClass = "displayBlock"but the resulting error always showed up.
An asp:Panel would work, but I'm avoiding using that because I want my asp:Label and asp:DropDownList to line up with several other labels and dropdownlists; putting in a panel would make them not line up properly.
I'm guessing there is something I'm missing, something I just don't get, but I can't seem to figure out what that is. If anybody has any clue as to what's happening, I would really appreciate the help!
It's not able to always find the control on postback because it's a child of statusFilter. Move the input field outside of the label:
<asp:Label AssociatedControlID="statusFilter" id="statusFilterLabel" runat="server" CssClass="filterLabel">Status
</asp:Label>
<asp:DropDownList ID="statusFilter" runat="server" CssClass="filterInput" AutoPostBack="true" OnSelectedIndexChanged="anyFilter_SelectedIndexChanged" AppendDataBoundItems="True">
<asp:ListItem Selected="True" Value=" 0"><All></asp:ListItem>
</asp:DropDownList>

How do I do a FOREACH loop on specific controls on my web form?

This is basically what I want to do:
foreach (checkbox cbx in Controls.Checkboxes)
{
if (checkbox.checked)
{
//code
}
}
On my web page, there are 2 check boxes. I want to run a process for each selected item on the page.
If I understand your question correctly, you want to loop through each checkbox of checkboxlist control and get the values.
If so, here is an example.
<asp:CheckBoxList runat="server" ID="CheckBoxList1">
<asp:ListItem Text="One" Value="1" />
<asp:ListItem Text="Two" Value="2" />
<asp:ListItem Text="Three" Value="3" />
</asp:CheckBoxList>
<asp:Button runat="server" ID="Button1" Text="Submit" OnClick="Button1_Click" />
protected void Button1_Click(object sender, EventArgs e)
{
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
{
string text = item.Text;
string value = item.Value;
// Do something
}
}
}
If you are asking about individual checkboxes, click on my previous edited answer.

Drop Down List and Selected Value

I'm having a very hard time figuring out what I'm doing wrong here. In my Edit Item Template I have the following code for my drop down list:
<asp:DropDownList ID="dd_is_active" runat="server" AppendDataBoundItems="true"
DataValueField="Enabled">
<asp:ListItem Text="Yes" Value="1"></asp:ListItem>
<asp:ListItem Text="No" Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:HiddenField ID="is_activeTextBox" runat="server" Value='<%# Bind("Enabled") %>' />
Here is my aspx.cs code:
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
e.Values["SUB_last_modified_date"] = DateTime.Now.ToString();
e.Values["SUB_last_modified_by_user_id"] = HttpContext.Current.User.Identity.Name;
e.Values["SUB_last_modified_by_user_name"] = Session["UserName"].ToString();
e.Values["Enabled"] = ((DropDownList)(sender as ListView).InsertItem.FindControl("dd_is_active")).SelectedValue;
e.Values["Category_ID"] = ((DropDownList)(sender as ListView).InsertItem.FindControl("dd_category")).SelectedValue;
}
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
e.NewValues["SUB_last_modified_date"] = DateTime.Now.ToString();
e.NewValues["SUB_last_modified_by_user_id"] = HttpContext.Current.User.Identity.Name;
e.NewValues["SUB_last_modified_by_user_name"] = Session["UserName"].ToString();
}
It seems something is either missing from my .cs code or I have the values of 1 and 0 bound incorrectly in the html code. This exact same code works for the Insert Item Template, but the Update (or Edit Item Template) is not working correctly.
When I try to edit an item in my table I get an error stating the input string is in an incorrect format. I know it's trying to bind the Text of "Yes" or "No" but I need to ind to the Values of either "0" or "1". Any help is greatly appreciated!
I think your syntax is wrong for HiddenField value.
Instead of this
<asp:HiddenField ID="is_activeTextBox" runat="server" Value='<%# Bind("Enabled") '>' />
It should be
<asp:HiddenField ID="is_activeTextBox" runat="server" Value='<%# Bind("Enabled")%>' />

C# ASPX CheckListbox where are the values

I have an ASPX C# page with a CheckListBox.
Using the following code to determine if the item is checked and it is always false.
if (lstFiles.Items[i].Selected)
I have tried lstFiles.Items[i].CheckedItems, but that is not a valid attribute. I also tried
I have tried lstFiles.Items[i].SelectedItems, but that is not a valid attribute either.
I think VS2010 is confused, but I don't know where.
----Ok, they click on a box in my CheckBoxList control and all that should happen is the box is checked waiting for them to select another item. The user then click the Delete Button and this code is executed.
`protected void btnDelete_Click(object sender, EventArgs e)
{
foreach (ListItem i in lstFiles.Items)
{
if (i.Selected)
{
string filename = i.Value.ToString();
DeleteFTP(filename);
}
}
string[] filenames = GetFileList();
lstFiles.Items.Clear();
foreach (string filenamel in filenames)
{
lstFiles.Items.Add(filenamel);
}
}`
--- in all cases i.Selected = False, I have 2 items with one of them checked.
As glosrob said, simple way to check all CheckBoxList tiems is this:
foreach (ListItem i in CheckBoxList1.Items)
{
if (i.Selected)
{
//do stuff
}
}
It looks like the fact that you don't work with this control itself, but with its items, confused you. So you can't look if the CheckBoxList is checked or selected:
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Selected="True" Value="1">a</asp:ListItem>
<asp:ListItem Value="2">b</asp:ListItem>
<asp:ListItem Value="3">c</asp:ListItem>
</asp:CheckBoxList>
My mistake - I see you are using a web control.
The following is working for me:
Markup
<asp:CheckBoxList ID="checkBoxList1" runat="server" AutoPostBack="true">
<asp:ListItem Text="Test 1" Value="1" />
<asp:ListItem Text="Test 2" Value="2" />
<asp:ListItem Text="Test 3" Value="3" />
</asp:CheckBoxList>
<asp:Button ID="btnTest" runat="server" Text="Go!" OnClick="btnTest_click" />
Code Behind
protected void btnTest_click(object sender, EventArgs e)
{
foreach (ListItem li in checkBoxList1.Items)
{
if (li.Selected)
{
//item is selected
}
}
}

Categories