Ankita Singh

Ankita Singh

  • NA
  • 159
  • 17.6k

onchange event for cascading dropdownlist

Mar 9 2018 3:01 AM
<script type="text/javascript">
function CourseFun() {
debugger
var DropdownList = document.getElementById('<%=course.ClientID %>');
var SelectedIndex = DropdownList.selectedIndex;
var divtenper = document.getElementById('divtenper');
var divyear10 = document.getElementById('divyear10');
var divpertwelth = document.getElementById('divpertwelth');
var divyearof12 = document.getElementById('divyearof12');
var divgraper = document.getElementById('divgraper');
var divyearofgra = document.getElementById('divyearofgra');
if (SelectedIndex == 1) {
divpertwelth.style.display = "block";
divyearof12.style.display = "block";
divgraper.style.display = "none";
divyearofgra.style.display = "none";
divyear10.style.display = "block";
divtenper.style.display = "block";
}
else if (SelectedIndex == 3) {
divpertwelth.style.display = "block";
divyearof12.style.display = "block";
divyear10.style.display = "block";
divtenper.style.display = "block";
divgraper.style.display = "none";
divyearofgra.style.display = "none";
}
else if (SelectedIndex == 2) {
divyearofgra.style.display = "block";
divpertwelth.style.display = "block";
divyearof12.style.display = "block";
divyear10.style.display = "block";
divtenper.style.display = "block";
divgraper.style.display = "block";
}
}
</script>
 
this script for CourseFun change event
 
<script type="text/javascript">
function ACanChange() {
debugger
var DropdownList = document.getElementById('<%=ACan.ClientID %>');
var SelectedIndex = DropdownList.selectedIndex;
var divtenper = document.getElementById('divtenper');
var divyear10 = document.getElementById('divyear10');
var divpertwelth = document.getElementById('divpertwelth');
var divyearof12 = document.getElementById('divyearof12');
var divgraper = document.getElementById('divgraper');
var divyearofgra = document.getElementById('divyearofgra');
//divtenper.style.display = Course.value == "1" ? "block" : "none";
if (SelectedIndex == 1) {
divpertwelth.style.display = "none";
divyearof12.style.display = "none";
divgraper.style.display = "none";
divyearofgra.style.display = "none";
divyear10.style.display = "block";
divtenper.style.display = "block";
}
else if (SelectedIndex == 2) {
divpertwelth.style.display = "block";
divyearof12.style.display = "block";
divyear10.style.display = "none";
divtenper.style.display = "none";
divgraper.style.display = "none";
divyearofgra.style.display = "none";
}
else if (SelectedIndex == 3) {
divgraper.style.display = "block";
divyearofgra.style.display = "block";
divpertwelth.style.display = "none";
divyearof12.style.display = "none";
divyear10.style.display = "none";
divtenper.style.display = "none";
}
}
</script>
 
this sript for ACanChange()
 
<asp:DropDownList ID="course" runat="server" class="slectbig" AutoPostBack="True" OnSelectedIndexChanged="course_SelectedIndexChanged" onChange="return CourseFun()">
<asp:ListItem Text = "--Select course--" Value = ""></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ACan" runat="server" class="slectbig" onChange="return ACanChange()">
<asp:ListItem Value="0"> Select </asp:ListItem>
</asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
course.AppendDataBoundItems = true;
String strConnString = ConfigurationManager.ConnectionStrings["dbconnectionstr"].ConnectionString;
String strQuery = "select Courseid, CourseName from CourseName";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
{
con.Open();
course.DataSource = cmd.ExecuteReader();
course.DataTextField = "CourseName";
course.DataValueField = "Courseid";
course.DataBind();
con.Close();
}
}
}
protected void course_SelectedIndexChanged(object sender, EventArgs e)
{
ACan.Items.Clear();
ACan.Items.Add(new ListItem("--Select--", ""));
ACan.AppendDataBoundItems = true;
String strConnString = ConfigurationManager.ConnectionStrings["dbconnectionstr"].ConnectionString;
String strQuery = "select ACanid, ACanName from ACanName " + "where Courseid=@Courseid";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("@Courseid", course.SelectedItem.Value);
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
con.Open();
ACan.DataSource = cmd.ExecuteReader();
ACan.DataTextField = "ACanName";
ACan.DataValueField = "Acanid";
ACan.DataBind();
con.Close();
}
 
code of aspx.cs page
 
my problem:- if i select course id from first dropdownlist... then show all div according to condition but cascading dropdown list is not working not data show in second dropdownlist then second onchange event will not work
 
plz some help me.... thanking you

Answers (2)