On change show alert C# asp.net - c#

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);

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);

item.Selected is false when the checkboxlist is disabled.

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" />

Select radio buttons according to dropdown list in asp.net

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.

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