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;
}
Related
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);
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);
hi i have gridview that when a row is selected, it populates into textboxes that are used to populate the gridview itself. the last field is a dropdownlist and its not displaying when the gridview is clicked. i set a breakpoint and see that its stuck on the first - 0 index. i dont know why it isnt moving forward... here is the code:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
....
if (DropDownListCurrency.Items.FindByValue(row.Cells[7].Text.ToString().Trim) != null)
{
DropDownListCurrency.SelectedValue = row.Cells[7].Text.ToString().Trim();
}
....
}
<asp:DropDownList ID="DropDownListCurrency" runat="server"
CausesValidation="True"
DataSourceID="CurrencyDropDownListDataSource"
DataTextField="Currency" DataValueField="Currency_ID"
AppendDataBoundItems="True">
<asp:ListItem Value="0" Text="<Select>" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>
why you want take the value from textbox. Will better use DataKeyNames like this inside that event
GridViewRow row = GridView.SelectedRow;
int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Value);
this work if you have only one value in DataKeyName if you look not there a index if you want have more than one value use this
int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Values["FirstValue"]);
string name = Convert.ToString(GridView.DataKeys[row.RowIndex].Values["SecondValue"]);
Does the DropDownList contains the words that you wanna show displayed?
You need to set the AutoPostBack property to True:
it should be :
<asp:DropDownList ID="DropDownListCurrency" AutoPostBack="True" runat="server"
CausesValidation="True"
DataSourceID="CurrencyDropDownListDataSource"
DataTextField="Currency" DataValueField="Currency_ID"
AppendDataBoundItems="True">
<asp:ListItem Value="0" Text="<Select>" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>
made a simple error...
called the wrong field was supposed to be value (Currency_ID) and not the text (Currency)
DropDownListCurrency.SelectedValue = GridView1.DataKeys[row.RowIndex].Values["Currency_ID"].ToString().Trim();
I'm basically trying to bind the dropdown list with the radio buttons. there are 4 options in the dropdown menu according to which the radio buttons should be selected.
with the first two options, the radio buttons should be active and with the rest of two objects in the dropdown menu, the radio buttons should become inactive.
here is my front end code for dropdown:
<asp:DropDownList ID="ddLType" runat="server" Width="406px" Height="23px">
<asp:ListItem Value="Prof">Professional</asp:ListItem>
<asp:ListItem>Enterprise</asp:ListItem>
<asp:ListItem>Maintanence</asp:ListItem>
<asp:ListItem>Reporting</asp:ListItem>
</asp:DropDownList>
here is my code for radio buttons:
<asp:RadioButtonList ID="rdoMeapSupport" RepeatDirection="Horizontal" runat="server" AutoPostBack="True" >
<asp:ListItem Value="1" Text="Yes" />
<asp:ListItem Value="0" Text="No" />
</asp:RadioButtonList>
if we select professional, the radio buttons should be active with yes as the checked option.
with enterprise, both the buttons should be active but not selected.
with maintenance and reporting, the buttons should become inactive.
First of all you have to set the property of drop down list called AutoPostBack to true. You can do this by simply selecting your drop down list and set AutoPostBack = true from properties window.
Then go to code behind file write these codes:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
if (ddLType.SelectedValue == "Professional")
{
rdoMeapSupport.Enabled = true;
rdoMeapSupport.SelectedValue = "Yes";
}
}
}
after that set event for your radio button list "SelectedIndexChanged"
and paste this code inside that
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddLType.SelectedValue == "Professional")
{
rdoMeapSupport.Enabled = true;
rdoMeapSupport.SelectedValue = "Yes";
}
if (ddLType.SelectedValue == "Enterprise")
{
rdoMeapSupport.SelectedValue = null;
rdoMeapSupport.Enabled = true;
}
if ((ddLType.SelectedValue == "Maintanence") || (ddLType.SelectedValue == "Reporting"))
{
rdoMeapSupport.SelectedValue = null;
rdoMeapSupport.Enabled = false;
}
}
Apply AutoPostBack="true" attribute to the dropdown and do the below logic in the selected index change event.
<asp:DropDownList ID="ddLType" runat="server" Width="406px" Height="23px"
onselectedindexchanged="ddLType_SelectedIndexChanged" AutoPostBack="true" >
<asp:ListItem Value="Prof">Professional</asp:ListItem>
<asp:ListItem>Enterprise</asp:ListItem>
<asp:ListItem>Maintanence</asp:ListItem>
<asp:ListItem>Reporting</asp:ListItem>
</asp:DropDownList>
<asp:RadioButtonList ID="rdoMeapSupport" RepeatDirection="Horizontal" runat="server"
AutoPostBack="True">
<asp:ListItem Value="1" Text="Yes" />
<asp:ListItem Value="0" Text="No" />
</asp:RadioButtonList>
protected void ddLType_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddLType.SelectedIndex == 0 || ddLType.SelectedIndex == 1)
{
rdoMeapSupport.Enabled = true;
}
else
{ rdoMeapSupport.Enabled = false; }
}
I think making radio buttons active, but not allowing to select is none of use, better you disable it.
You may use jquery for this:
<script>
$(function () {
$("# <%# ddLType.ClientID %>").change(function () {
var selVal = $(this).val();
if(selVal == "Prof"){
$('#rb_Statusno').removeAttr('disabled');
else
$('#rb_Statusno').attr('disabled', true);
}
</script>
For more help you may go through this post.
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
}
}
}