asp:ListItem item to be auto checked in a CheckBoxList - c#

How to only auto check option 3 if the if statement below is true?
I have the following in my .aspx:
<asp:CheckBoxList ID="CB_Test" runat="server">
<asp:ListItem Text="Opt 1" Value="1" />
<asp:ListItem Text="Opt 2" Value="2" />
<asp:ListItem Text="Opt 3" Value="3" />
</asp:CheckBoxList>
In the .aspx.cs page,
if(variable = "3")
CB_Test.Checked = true;
The above is not working for me.

I suppose you wanna check particular item depends on the value instead of the CheckBoxList itself.
Therefore, what you want should be something like
foreach (ListItem item in CB_Test.Items)
{
if (item.Value == "3")
item.Selected = true;
}

Related

On change show alert C# asp.net

I am trying to check if else condition works on the asp.net C# code behind what am not getting is on dropdown change there is a condition if selected index value is zero then alrt 0 alert but nothing shows on any of the selected index is selected not sure where am i going wrong.
CS: Code Behind
protected void Student_type_dd_change(object sender, EventArgs e)
{
if (Student_type_dd.SelectedIndex == 0)
{
Response.Write("<script>alert('0');</script>");
}
else if (Student_type_dd.SelectedIndex == 1)
{
Response.Write("<script>alert('1');</script>");
}
else if (Student_type_dd.SelectedIndex == 2)
{
Response.Write("<script>alert('2');</script>");
}
}
Aspx
<asp:DropDownList ID="Student_type_dd" runat="server" onselectedindexchanged="Student_type_dd_change" autopostback="true" >
<asp:ListItem Text="Select Type" Value="0" />
<asp:ListItem Text="All Students" Value="All Students" />
<asp:ListItem Text="Class Wise" Value="Class Wise" />
<asp:ListItem Text="Select Specific" Value="Select Specific" />
</asp:DropDownList>
You can use something like this :
ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('0')", true);

jQuery - ASP RadioButtonList: How do you get value attribute?

I am using an ASP RadioButtonList with ListItem elements inside. I have given each option value. See below as an example:
<asp:RadioButtonList ID="rblTestList" runat="server">
<asp:ListItem Selected="True" Text="Test1" Value="Test1" />
<asp:ListItem Text="Test2" Value="Test2" />
<asp:ListItem Text="Test3" Value="Test3" />
<asp:ListItem Text="Test4" Value="Test4" />
</asp:RadioButtonList>
I am trying to get the value of the selected ListItem. I am using the following line of code to get the value, but all I get is the value 1 (not the text value I gave it). The API says the Value property is of type String, so I see cannot see why it isn't working. (See: ListItem.Value Property)
var option = $('#<%=rblTestList.ClientID %> input[type=radio]:checked').val();
Does anyone know what the issue is here?
Edit: Updated code snippet (old one was wrong)
Use Checkbox On Change Event to Get Value
$('#<%=rblTestList.ClientID%>').on('change',function () {
if($(this).is(':checked')){ //Checks Checked or Not
var Value = $(this).val(); //Here Your Value
}
});
try this
$(function () {
$(".Jqanchor").click(function () {
var option = $('# input[type=radio]:checked').next().text();
alert(option);
});
})
and aspx
<asp:RadioButtonList ID="rblTestList" runat="server">
<asp:ListItem Selected="True" Text="Test1" Value="1" />
<asp:ListItem Text="Test2" Value="2" />
<asp:ListItem Text="Test3" Value="3" />
<asp:ListItem Text="Test4" Value="4" />
</asp:RadioButtonList>
content
Try this
$('document').ready(function () {
$('#<%=rblTestList.ClientID%>').click(function () {
if($(this).is(':checked')){
var selectedValue = $(this).val();
alert(selectedValue);
}
});
});
UPDATE 1:
It's a server side control and here you need to find the input radio control first and check for it's check or uncheck property.
Try this Update
$('document').ready(function () {
$('#<%=rblTestList.ClientID%>').find('input[type=radio]').click(function () {
if($(this).is(':checked')){
var selectedValue = $(this).val().replace('Test','');
alert(selectedValue);
}
});
});
UPDATE 2
if you want the numeic output then you have to put the options value in numeric like Value="1".
The HTML
<asp:RadioButtonList ID="rblTestList" runat="server">
<asp:ListItem Selected="True" Text="Test1" Value="1" />
<asp:ListItem Text="Test2" Value="2" />
<asp:ListItem Text="Test3" Value="3" />
<asp:ListItem Text="Test4" Value="4" />
</asp:RadioButtonList>
The jquery code remains same as in UPDATE 1
It turned out it was related to resource keys. I had initially used numbers and then changed to text but the resource keys were not updated so it always priority over the inline value attribute. I missed out the resource key part in my snippet as I thought it wasn't important. My mistake. Sorry for wasting everyone's time!

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.

How to selectively enable a Dropdown List

I have a form containing two DropDowns lists:
Marital Status
No. of Children
Now I would like to enable the No. of Children DropDown upon selection of following items in the Marital Status DropDown:
Widow
Divorced
Awaiting Divorce
How can I do it?
In the MaritalStaus DropDownList Selected Index changed event, If the selected values match your options then enable the NoOfChild DropDownList.
protected void MaritalStaus_SelectedIndexChanged(object sender, EventArgs e)
{
//Match the selected value here : for Example:
if (MaritalStaus.SelectedValue.Equals("Divorced") || /*Other Comparisions */)
{
NoOfChild.Enabled = true;
}
}
DropDown lists have ListItem collection in them. Each ListItem has a Text and a Value. Try setting the text as "Divorced" and value as "D" or better to an integer like "1", something similar to ID. You will get these Text/Value from a Database table if you are retrieving it from the Database.
Make the No. of Children DropDown Enabled = false by default and then Enable = true as explained in the code snippet above by ebad86.
you can also use cascading dropdown from ajaxcontroltoolkit
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx
.aspx
<asp:DropDownList ID="ddlMaritalStatus" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlMaritalStatus_SelectedIndexChanged">
<asp:ListItem Text="" />
<asp:ListItem Text="Widow" />
<asp:ListItem Text="Divorced" />
<asp:ListItem Text="Awaiting Divorce" />
</asp:DropDownList>
<asp:DropDownList ID="ddlNoOfChildren" runat="server" Enabled="false">
<asp:ListItem Text="1" />
<asp:ListItem Text="2" />
<asp:ListItem Text="3" />
<!-- and so on -->
</asp:DropDownList>
aspx.cs
protected void ddlMaritalStatus_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlMaritalStatus.SelectedItem.Text == "Widow") // if widow is selected
ddlNoOfChildren.Enabled = true;
else
ddlNoOfChildren.Enabled = false;
}

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