Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
In my application,I call method at load time which fill college name into drop down list of college name.When I select one of the college,I want the faculties of corresponding that college from database.But SelectedIndexChanged event of drop down list of College name does not fire.Please give me a solution.
Thank You.
The code written by me is as follow.
C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                fillcollegename();
            }

        }


C#
void fillcollegename()
    {
        objsqlconnection = new SqlConnection();
        objsqlconnection.ConnectionString = strconnectionstring;
        objsqlconnection.Open();
        try
        {

            string strquery1 = "select * from tblCollege_Info";

            objsqlcommand = objsqlconnection.CreateCommand();
            objsqlcommand.CommandText = strquery1;
            SqlDataAdapter a = new SqlDataAdapter(objsqlcommand);
            DataTable table = new DataTable();
            a.Fill(table);
            String[] CollegeNames = new String[table.Rows.Count];
            int i = 0;
            foreach (DataRow row in table.Rows)
            {
                CollegeNames[i] = row["CollegeName"].ToString();
                i++;
            }

        ddlCollegenm.DataSource = CollegeNames;
        ddlCollegenm.DataBind();
        }
        catch
        {
        }


        objsqlconnection.Close();
    }



C#
protected void ddlCollegenm_SelectedIndexChanged(object sender, EventArgs e)
        {
            objsqlconnection = new SqlConnection();
            objsqlconnection.ConnectionString = strconnectionstring;
            objsqlconnection.Open();
            string strquery2 = "select CollegeId from tblCollege_Info where (CollegeName='" + ddlCollegenm.SelectedValue.ToString() + "')";
            objsqlcommand = new SqlCommand(strquery2, objsqlconnection);
            SqlDataReader objsqldatareader = objsqlcommand.ExecuteReader();
            if(objsqldatareader.Read())
            {
                collegeid = objsqldatareader[0].ToString();
            }
            objsqldatareader.Close();
            objsqlconnection.Close();
            fillfacultyname();
        }


C#
void fillfacultyname()
        {
            objsqlconnection = new SqlConnection();
            objsqlconnection.ConnectionString = strconnectionstring;
            try
            {
                objsqlconnection.Open();
                string strquery1 = "select FacultyName from tblFaculty_Info where (CollegeId='" + collegeid + "')";
                objsqlcommand = objsqlconnection.CreateCommand();
                objsqlcommand.CommandText = strquery1;
                SqlDataAdapter a = new SqlDataAdapter(objsqlcommand);
                DataTable table = new DataTable();
                a.Fill(table);
                String[] FacultyNames = new String[table.Rows.Count];
                int i = 0;
                foreach (DataRow row in table.Rows)
                {
                    FacultyNames[i] = row["FacultyName"].ToString();
                    i++;
                }
                ddlfacultynm.DataSource = FacultyNames;
                ddlfacultynm.DataBind();
            }
            catch
            {
            }
            finally
            {
                objsqlconnection.Close();
            }
            }
Posted

Check AutoPostBack Property of DropDownList : Should be True
 
Share this answer
 
Try by setting value of "AutoPostBack" Property of your DropDownList to "True", as below.

ASP.NET
<asp:dropdownlist AutoPostBack="true" id="ddlCollegenm" runat="server" xmlns:asp="#unknown">
 
Share this answer
 
v2
Set Property AutoPostBack="true"
 
Share this answer
 
Yeah, I agree with property AutoPostBack. But there are more reasons for this issue, do you want know that? Check my answer

ASP.NET: Error: DropDownList OnSelectedIndexChanged Event Not Firing[^]
 
Share this answer
 
v2
Comments
Abhijit Jana 20-Aug-11 23:08pm    
I just check your previous answer. Any Specific reason to asking Enable View State for dropdown list ?
thatraja 21-Aug-11 1:08am    
Not sure but it does the trick. Agree that's a weird thing.
Abhijit Jana 22-Aug-11 0:03am    
I really don't think that is the trick. Disabled View State for the page, even Disabled the ViewState for Control "ViewStateMode" this should work.
Is your page posting back after the selection is changed?

if not, then u need to set AutoPostBack Property of the ListBox Control to True

Do it From Property Window or Design File
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900