Choose 1 checkbox out of 2 - c#

I got several checkbox, but I only want the the text "New Desktop" and "New Laptop" can only choose 1 out of 2. I hope it could be done in C#.
<asp:CheckBoxList ID="Service1" runat="server"
Width="251px" >
<%--onselectedindexchanged="checkBox1_CheckedChanged"--%>
<asp:ListItem text="New Login ID & Email Address" ></asp:ListItem>
<asp:ListItem text="New Desktop" Value="2" oncheckedchanged="checkBox1_CheckedChanged" ></asp:ListItem>
<asp:ListItem text="New Notebook" Value="3" oncheckedchanged="checkBox2_CheckedChanged"></asp:ListItem>
<asp:ListItem text="New Mouse"></asp:ListItem>
<asp:ListItem text="New Keyboard"></asp:ListItem>
<asp:ListItem text="New Printer"></asp:ListItem>
</asp:CheckBoxList>
//.cs pages
protected void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (Service1.Items[2].Selected == true)
{
Service1.Items[3].Enabled = false;
}
}
protected void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (Service1.Items[3].Selected == true)
{
Service1.Items[2].Enabled = false;
}
}

I am giving you one example below but still this may not be a feasible way. You need to add CheckedChanged event to each CheckBox to remove check from other CheckBoxes
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if(CheckBox1.Checked)
{
CheckBox2.Checked =false;
CheckBox3.Checked =false;
CheckBox4.Checked =false;
}
}
You should repeat the above evevt for CheckBox2 , CheckBox3 and so on. You can extend it as per your requirement.
But in this scenario I would recommend you to use the ASP.NET Radio Button Control. Please refer this link for more information on Radio Button Control.

You can use OnSelectedIndexChanged event for CheckBoxList with AutoPostBack="True" so your control send request to server and selection chenged event will be call
I.e : For design
<asp:CheckBoxList ID="Service1" runat="server" Width="251px" AutoPostBack="True" OnSelectedIndexChanged="Service1_SelectedIndexChanged">
<asp:ListItem Text="New Login ID & Email Address"></asp:ListItem>
<asp:ListItem Text="New Desktop" Value="2"></asp:ListItem>
<asp:ListItem Text="New Notebook" Value="3" ></asp:ListItem>
<asp:ListItem Text="New Mouse"></asp:ListItem>
<asp:ListItem Text="New Keyboard"></asp:ListItem>
<asp:ListItem Text="New Printer"></asp:ListItem>
</asp:CheckBoxList>
and in code: 'li' have all selected item and as per your requirement "New Desktop" and "New Laptop" only choose 1 out of 2 .
protected void Service1_SelectedIndexChanged(object sender, EventArgs e)
{
CheckBoxList li = (CheckBoxList)sender;
foreach (ListItem l in li.Items)
{
if (l.Value == "2")
{
if (l.Selected)
{
Service1.Items[2].Enabled = false;
}
else
{
Service1.Items[2].Enabled = true;
}
}
else if (l.Value == "3")
{
if (l.Selected)
{
Service1.Items[1].Enabled = false;
}
else
{
Service1.Items[1].Enabled = true;
}
}
}
}

Related

About RadioButtonList in ASP.NET

I have a RadioButtonList. I want the button to be available when I click "Agree", and not available when I click "Disagree". Default is disabled.
Now whether I agree or disagree, my btnSubmit button will become available. And it won't change back to unavailable.
function acceptprotocol() {
if (document.getElementById("rblAccept").SelectValue == 0 {
document.getElementById("btnSubmit").disabled = true;
}
else {
document.getElementById("btnSubmit").disabled = false;
}
}
RadioButtonList:
<asp:RadioButtonList ID="rblAccept" runat="server"
RepeatDirection="Horizontal" style="margin:auto"
onclick="acceptprotocol()">
<asp:ListItem Value="0">Agree</asp:ListItem>
<asp:ListItem Value="1" Selected="True">Against</asp:ListItem>
</asp:RadioButtonList>
How to solve it? Please help me.
Here's the code snippet for your solution:
RadioButtonList:
<asp:RadioButtonList ID="rblAccept" runat="server" RepeatDirection="Horizontal" style="margin:auto"
onclick="acceptprotocol()">
<asp:ListItem Value="0">Agree</asp:ListItem>
<asp:ListItem Value="1" Selected="True">Disagree</asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" Enabled="false" />
Function:
function acceptprotocol() {
var radioButtonAccept = document.getElementById('<%=rblAccept.ClientID %>').getElementsByTagName("input");;
for (var i = 0; i < radioButtonAccept.length; i++) {
if (radioButtonAccept[i].checked) {
if (radioButtonAccept[i].value == 1) {
document.getElementById('<%=btnSubmit.ClientID %>').disabled = true;
}
else {
document.getElementById('<%=btnSubmit.ClientID %>').disabled = false;
//use this bellow line if you want the after enable button button will availble all time
document.getElementById('<%=rblAccept.ClientID %>').removeAttribute("onclick");
}
break;
}
else {
document.getElementById('<%=btnSubmit.ClientID %>').disabled = true;
}
}
}

RadioButtonList verifying index

I have a Radiobuttonlist and I want after the Item with value 1 is selected to do something. When I used RadioButtonListType.SelectedValue is returning "" and when I use RadioButtonListType.SelectedIndex is returning -1.
Where I am doing wrong ?
this is my code
aspx
<asp:RadioButtonList ID="RadioButtonListType" runat="server" OnSelectedIndexChanged="RadioButtonListType_SelectedIndexChanged" ValidationGroup="emergency" AutoPostBack="True">
<asp:ListItem Value="0">Normal</asp:ListItem>
<asp:ListItem Value="1">Emergency</asp:ListItem>
</asp:RadioButtonList>
.cs
protected void RadioButtonListType_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadioButtonListSF.SelectedIndex == 1)
{
MPEEmergency.Show();
}
}

Store a Selected Items From a Listbox

I can't seem to get my application to store the selected items from a listbox.
Each time I run the code and debug it selects the first item in the list as the selected item from the listbox even if it is not selected and stores this item but for the actual items that have been selected they are ignored. This even happens if I set all properties of the listitems to Selected="false" in the ASP tag.
I have the same code used for a checkboxlist and it works fine I cannot see why this is not working?
I have not included the Answers class as it just gets and sets the properties.
<asp:ListBox runat="server" ID="lbQuestion3" SelectionMode="Multiple" Rows="7">
<asp:ListItem Value="0" >Dublin</asp:ListItem>
<asp:ListItem Value="1" >Cork</asp:ListItem>
<asp:ListItem Value="1" >Waterford</asp:ListItem>
<asp:ListItem Value="1" >Limerick</asp:ListItem>
<asp:ListItem Value="0" >Carlow</asp:ListItem>
<asp:ListItem Value="0" >Galway</asp:ListItem>
<asp:ListItem Value="0" >Kilkenny</asp:ListItem>
</asp:ListBox>
<br />
<asp:Button ID="btnQuestion4" runat="server" Text="Next Question" OnClick="btnQuestion4_Click"/>
C# code
protected void btnQuestion4_Click(object sender, EventArgs e)
{
Answers question3;
List<Answers> answers = new List<Answers>();
foreach (ListItem item in lbQuestion3.Items)
{
if (item.Selected == true)
{
question3 = new Answers(item.Text, Convert.ToInt32(item.Value), 0);
answers.Add(question3);
}
Session["ssAnswerQ3"] = answers;
Response.Redirect("Question4.aspx");
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["ssAnswerQ3"] != null)
{
List<Answers> previousAnswers = new List<Answers>();
previousAnswers = (List<Answers>)Session["ssAnswerQ3"];
foreach (ListItem item in lbQuestion3.Items)
{
foreach (var item2 in previousAnswers)
{
if (item2.Answer == item.Text)
{
item.Selected = true;
}
}
}
}
}
}
Any help would be greatly appreciated.
John

How to load items into dropdownlist after selecting item from another dropdownlist in asp.net c#

I have four dropdownlistbox in my form, i want to load items into three dropdownlists according to the selection of items from the first drop down list , my first dropdown list has folloding items
Amount ,
PAC ,
Base UOM
Whatever i am selecting from first drop down list , i want to load the same selected item into remaining three dropdownlist
I have tried the following code but it is not working as expected
protected void ddl_UOM_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = ddl_UOM.SelectedItem.Value;
ddl_UOM2.Items.Add(uom_Name);
ddl_UOM3.Items.Add(uom_Name);
ddl_UOM4.Items.Add(uom_Name);
}
pls help.
You need to use Ajax Control Tool kit's Cascaded Drop-down
Here is the like for it:
http://www.asp.net/AjaxLibrary/AjaxControlToolkitSampleSite/Walkthrough/CCDWithDB.aspx
and here is the demo:
http://www.asp.net/AjaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx
here is a tutorial link:
http://www.dotnetfox.com/articles/ajax-cascading-dropdown-example-with-database-in-Asp-Net-1078.aspx
Hope this material helps.
May be you don't have set AutoPostBack property to true. Try below code sample :
ASPX:
<asp:DropDownList runat="server" ID="parentDropDown" OnSelectedIndexChanged="parentDropDown_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList runat="server" ID="child1">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="child2">
</asp:DropDownList>
Code Behind:
protected void parentDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = parentDropDown.SelectedValue;
child1.Items.Add(uom_Name);
child2.Items.Add(uom_Name);
}
If you want to remove existing item from child before you add then:
protected void parentDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = parentDropDown.SelectedValue;
child1.Items.Clear();
child2.Items.Clear();
child1.Items.Add(uom_Name);
child2.Items.Add(uom_Name);
}
You have to set AutoPostBack="true" to the first drop down list and clear other drop downs items before adding new item.
ASPX:
<asp:DropDownList ID="ddl_UOM" runat="server" OnSelectedIndexChanged="ddl_UOM_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="Amount" Value="Amount"></asp:ListItem>
<asp:ListItem Text="PAC" Value="PAC"></asp:ListItem>
<asp:ListItem Text="Base" Value="Base"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl_UOM2" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddl_UOM3" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddl_UOM4" runat="server"></asp:DropDownList>
ASPX.CS
protected void ddl_UOM_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = ddl_UOM.SelectedItem.Value;
ddl_UOM2.Items.Clear();
ddl_UOM2.Items.Add(new ListItem(uom_Name, uom_Name));
ddl_UOM3.Items.Clear();
ddl_UOM3.Items.Add(new ListItem(uom_Name, uom_Name));
ddl_UOM4.Items.Clear();
ddl_UOM4.Items.Add(new ListItem(uom_Name, uom_Name));
}
protected void ddl_UOM_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = ddl_UOM.SelectedItem.Value;
ddl_UOM2.Items.Clear();
ddl_UOM3.Items.Clear();
ddl_UOM4.Items.Clear();
ddl_UOM2.Items.Add(uom_Name);
ddl_UOM3.Items.Add(uom_Name);
ddl_UOM4.Items.Add(uom_Name);
}
After writing code in ddl_UOM_SelectedIndexChanged() , call ddl_UOM_SelectedIndexChanged() method from Page_Load()
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
ddl_UOM_SelectedIndexChanged(sender, e );
}
}
catch (Exception)
{
throw;
}
}
You should try to use PageMethods in JS to load the dropdowns any way you like any time you like.

Choosing dropdownlist value

I have created event for select index changed for dropdownlist like below but the event is not firing.I dont know what is going wrong?
<asp:DropDownList ID="ddlcurrency" runat="server" OnSelectedIndexChanged="ddlcurrency_SelectedIndexChanged1" >
<asp:ListItem Value="Nrs" >Nrs</asp:ListItem>
<asp:ListItem Value="$" >$</asp:ListItem>
</asp:DropDownList>
protected void ddlcurrency_SelectedIndexChanged1(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (ddlcurrency.Items.FindByText("$").Selected == true) //keeping the currency value in session
{
Session["Curr"] = "Dol";
}
else
{
Session["Curr"] = "Nrs";
}
}
}
try this:
<asp:DropDownList ID="ddlcurrency" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlcurrency_SelectedIndexChanged1" >
<asp:ListItem Value="Nrs" >Nrs</asp:ListItem>
<asp:ListItem Value="$" >$</asp:ListItem>
</asp:DropDownList>
Have you tried setting a break point at the SelectedIndexChaged event of the DDL to check if HITS there?
<asp:DropDownList ID="ddlcurrency" runat="server" AutoPostback="true" OnSelectedIndexChanged="ddlcurrency_SelectedIndexChanged1" >
<asp:ListItem Value="Nrs" >Nrs</asp:ListItem>
<asp:ListItem Value="$" >$</asp:ListItem>
</asp:DropDownList>
protected void ddlcurrency_SelectedIndexChanged1(object sender, EventArgs e)
{
if (ddlcurrency.Items.FindByText("$").Selected == true) //keeping the currency value in session
{
Session["Curr"] = "Dol";
}
else
{
Session["Curr"] = "Nrs";
}
}
Set autopost back property of dropdown control to true
<asp:DropDownList ID="ddlcurrency" runat="server"
OnSelectedIndexChanged="ddlcurrency_SelectedIndexChanged1" AutoPostBack="true" >

Categories

Resources