BINDING ALL COLLEGE NAMES TO A COLLEGE-DROPDOWNLIST BASED ON DATA SELECTED IN ANOTHER CITY-DROPDOWNLIST , BOTH DROPDOWNLISTS ARE PLACED INSIDE THE DATALIST

Rashmi S

Total Post:5

Points:35
Posted by  Rashmi S
 1504  View(s)
Ratings:
Rate this:
Binding all college names to a college-dropdownlist based on city name selected in another city-dropdownlist , both dropdownlists are placed inside the datalist.
how should i create SelectedIndexChanged event for city-dropdownlist and to to bind college names to college-dropdownlist? please help me out...!

this is my code
<asp:DataList ID="DLpriority" runat="server" BackColor="White"
                                BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4"
                                GridLines="Both">
                                <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                                <HeaderTemplate>
                                    <table style="width:100%;">
                                        <tr>
                                            <td style="width: 103px">
                                                Priority No.</td>
                                            <td style="width: 126px">
                                                City</td>
                                            <td align="center" style="width: 157px">
                                                College
                                            </td>
                                            <td align="center">
                                                Combination</td>
                                        </tr>
                                    </table>
                                </HeaderTemplate>
                                <ItemStyle BackColor="White" ForeColor="#330099" />
                                <ItemTemplate>
                                    <table border="3" style="width:100%;">
                                        <tr>
                                            <td>
                                                <asp:Label ID="lblProirityNo" runat="server" Text='<%# Eval("PriorityNo") %>'
                                                    Width="100px"></asp:Label>
                                            </td>
                                            <td>
                                                <asp:DropDownList ID="DDLcity" runat="server" Width="200px" AutoPostBack="True"
                                                    onselectedindexchanged="DDLcity_SelectedIndexChanged">
                                                </asp:DropDownList>
                                            </td>
                                            <td style="width: 202px">
                                                <asp:DropDownList ID="DDLcollege" runat="server" Width="200px">
                                                </asp:DropDownList>
                                            </td>
                                            <td>
                                                <asp:DropDownList ID="DDLcourse" runat="server" Width="150px">
                                                </asp:DropDownList>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                &nbsp;</td>
                                            <td>
                                                &nbsp;</td>
                                            <td style="width: 202px">
                                                &nbsp;</td>
                                            <td>
                                                &nbsp;</td>
                                        </tr>
                                        <tr>
                                            <td>
                                                &nbsp;</td>
                                            <td>
                                                &nbsp;</td>
                                            <td style="width: 202px">
                                                &nbsp;</td>
                                            <td>
                                                &nbsp;</td>
                                        </tr>
                                    </table>
                                </ItemTemplate>
                                <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                            </asp:DataList>
  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: Binding all college names to a college-dropdownlist based on data selected in another city-dropdownlist , both dropdownlists are placed inside the datalist

    Hi Rashmi, 

    I think you try to create the cascading dropdownlist, you can try the following code:

    UI Code:

    <div>

         <div>

               <label>City : </label>

                    <asp:DropDownList  ID="DDLCity" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDLCity_SelectedIndexChanged"  ></asp:DropDownList>

                </div>

                <br />

                <div>

                    <label>College : </label>

                    <asp:DropDownList  ID="DDLCollege"  runat="server" AutoPostBack="True"></asp:DropDownList>

                </div>

            </div>

    Class:

    public class City

        {

            public int Id { get; set; }

            public string CityName { get; set; }

            public string College { get; set; }

        }

    Code Behind:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

     

    namespace CascadingDropdownAspNet

    {

        public partial class Default : System.Web.UI.Page

        {

            public List<City> Cities = new List<City>();

     

            protected void Page_Load(object sender, EventArgs e)

            {

                Cities.Add(new City { Id = 1, CityName = "New York", College = "New York College 1" });

                Cities.Add(new City { Id = 2, CityName = "New York", College = "New York College 2" });

                Cities.Add(new City { Id = 3, CityName = "Boston", College = "Boston College 1" });

                Cities.Add(new City { Id = 4, CityName = "Boston", College = "Boston College 2" });

     

                if (!IsPostBack)

                {

                    DDLCity.Items.AddRange(Cities.Select(x => new ListItem(x.CityName)).Distinct().ToArray());

     

                }

            }

     

            protected void DDLCity_SelectedIndexChanged(object sender, EventArgs e)

            {

                DDLCollege.Items.Clear();

                var selectedItem = DDLCity.SelectedItem.ToString();

     

                DDLCollege.Items.AddRange(Cities.Where(x => x.CityName == selectedItem).Select(x => new ListItem(x.College)).ToArray());

            }

        }

    }



  1. Rashmi S

    Post:5

    Points:35
    Re: Binding all college names to a college-dropdownlist based on data selected in another city-dropdownlist , both dropdownlists are placed inside the datalist

    Hi...
    Thank you.. i tried your code is working fine...

    This is me code which i tried... It may help others...!

    source code :
    <asp:Panel ID="Panel1" runat="server">
                    <table style="width: 102%;">
                    <tr>
                        <td>
                        <table style="width: 90%">
                        <tr bgcolor="#F4CF9D"
                            style="font-weight: bold; font-style: normal; text-transform: capitalize; color: #990033; font-size: large">
                            <td align="left" style="height: 30px; width: 120px">
                                <strong>Register Number</strong></td>
                            <td align="left" style="height: 30px">
                                <asp:Label ID="lblRegNo" runat="server" ForeColor="Black"></asp:Label>
                            </td>
                            <td align="right" style="height: 30px">
                                <strong>Student Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong>
                            </td>
                            <td align="left" style="height: 30px">
                                <asp:Label ID="lblName" runat="server" ForeColor="Black"></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td style="width: 120px">
                                &nbsp;</td>
                            <td>
                                &nbsp;</td>
                            <td>
                                &nbsp;</td>
                            <td>
                                &nbsp;</td>
                        </tr>
                        <tr>
                            <td style="width: 120px">
                                &nbsp;</td>
                            <td align="right">
                                Total Number of priorities&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
                            <td align="left">
                                <asp:DropDownList ID="DDLcount" runat="server" Width="150px"
                                    AutoPostBack="True" onselectedindexchanged="DDLcount_SelectedIndexChanged">
                                </asp:DropDownList>
                            </td>
                            <td>
                                &nbsp;</td>
                        </tr>
                    </table>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <hr />
                         </td>
                     </tr>
                        <tr>
                            <td align="left" style="height: 30px">
                                <asp:Panel ID="Panel2" runat="server">
                              
                                <asp:DataList ID="DLpriority" runat="server" BackColor="White"
                                    BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4"
                                    GridLines="Both">
                                    <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                                    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                                    <HeaderTemplate>
                                        <table style="width:100%;">
                                            <tr>
                                                <td style="width: 103px">
                                                    Priority No.</td>
                                                <td style="width: 126px">
                                                    City</td>
                                                <td align="center" style="width: 157px">
                                                    College
                                                </td>
                                                <td align="center">
                                                    Combination</td>
                                            </tr>
                                        </table>
                                    </HeaderTemplate>
                                    <ItemStyle BackColor="White" ForeColor="#330099" />
                                    <ItemTemplate>
                                        <table border="3" style="width:100%;">
                                            <tr>
                                                <td>
                                                    <asp:Label ID="lblProirityNo" runat="server" Text='<%# Eval("PriorityNo") %>'
                                                        Width="100px"></asp:Label>
                                                </td>
                                                <td>
                                                    <asp:DropDownList ID="DDLcity" runat="server" Width="200px" AutoPostBack="True"
                                                        onselectedindexchanged="DDLcity_SelectedIndexChanged">
                                                    </asp:DropDownList>
                                                </td>
                                                <td style="width: 202px">
                                                    <asp:DropDownList ID="DDLcollege" runat="server" Width="200px"
                                                        AutoPostBack="True" onselectedindexchanged="DDLcollege_SelectedIndexChanged">
                                                    </asp:DropDownList>
                                                </td>
                                                <td>
                                                    <asp:DropDownList ID="DDLcourse" runat="server" Width="150px">
                                                    </asp:DropDownList>
                                                </td>
                                            </tr>
                                        </table>
                                        &nbsp;
                                    </ItemTemplate>
                                    <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                                </asp:DataList>
                                <br />
                                <br />
                                <asp:Button ID="btnSet" runat="server" BackColor="#D28215" Font-Bold="True"
                                    ForeColor="White" onclick="btnSet_Click" Text=" &gt;&gt; Set Priority" />
                                 </asp:Panel>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                &nbsp;</td>
                        </tr>
                    </table>
                    </asp:Panel>
     

    Code behind(.aspx.cs ) :
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;

    namespace PUseatAllot.Student
    {
        public partial class OptionEntry2  : System.Web.UI.Page
        {
            static int StudId;
            BL b = new BL();
            static int flg_editOption;
            static DataTable t_P_Details;

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    Panel1.Visible = false;
                    Panel2.Visible = false;
                    Panel3.Visible = false;

                    StudId = int.Parse(Session["StudentID"].ToString());
                    DataTable t_stud = b.Student_Get_StudId(StudId);
                    if (t_stud.Rows.Count > 0)
                    {
                        lblName.Text = t_stud.Rows[0]["StudentName"].ToString();
                        lblRegNo.Text = t_stud.Rows[0]["RegNo"].ToString();
                    }

                    //***********************************************************************************
                   
                    DataTable t_details = new DataTable();
                    DataTable tab1 = b.test_Get_CCid(StudId);
                    if (tab1.Rows.Count > 0)
                    {
                        Panel3.Visible = true;
                        //-----------------------------------------------------------------
                        DataTable t_proirNo = new DataTable();
                        t_proirNo.Columns.Add("PriorityNo", typeof(int));
                        for (int i = 1; i <= tab1.Rows.Count; i++)
                        {
                            DataRow dt = t_proirNo.NewRow();
                            dt["PriorityNo"] = i.ToString();
                            t_proirNo.Rows.Add(dt);
                        }
                        DLpriority.DataSource = t_proirNo;
                        DLpriority.DataBind();

                        foreach (DataListItem item in DLpriority.Items)
                        {
                            DropDownList ddlCity = (DropDownList)item.FindControl("DDLcity");
                            ddlCity.DataSource = b.Cities_GetAll();
                            ddlCity.DataTextField = "CityName";
                            ddlCity.DataValueField = "CityId";
                            ddlCity.DataBind();
                            ddlCity.Items.Insert(0, "Select");

                            //DropDownList ddlClg = (DropDownList)item.FindControl("DDLcollege");
                            //ddlClg.Items.Insert(0, "Select");

                            //DropDownList ddlCou = (DropDownList)item.FindControl("DDLcourse");
                            //ddlCou.Items.Insert(0, "Select");

                        }

                        //--------------------------------------------------------------------

                        string ccid = null;
                        for (int i = 0; i < tab1.Rows.Count; i++)
                        {
                            ccid = ccid + "'" + tab1.Rows[i]["CCid"].ToString() + "'" + ",";
                        }
                        ccid = ccid.Substring(0, (ccid.Length - 1));

                        DataTable tab2 = b.test_GetClgCouId_CCid(ccid);
                        if (tab2.Rows.Count > 0)
                        {
                           
                            t_details.Columns.Add("PriorityNo", typeof(int));
                            t_details.Columns.Add("CollegeName", typeof(string));
                            t_details.Columns.Add("CourseName", typeof(string));

                            for (int j = 0; j < tab2.Rows.Count; j++)
                            {
                                int clgid = int.Parse(tab2.Rows[j]["CollegeId"].ToString());
                                int couid = int.Parse(tab2.Rows[j]["CourseId"].ToString());

                                DataRow dt = t_details.NewRow();
                                DataTable tab3 = b.test_GetClgCouName_ClgCouId(clgid, couid);
                                if (tab3.Rows.Count > 0)
                                {
                                    dt["PriorityNo"] = j + 1;
                                    dt["CollegeName"] = tab3.Rows[0]["CollegeName"].ToString();
                                    dt["CourseName"] = tab3.Rows[0]["CourseName"].ToString();
                                    t_details.Rows.Add(dt);
                                }
                            }
                        }
                        else
                        {
                            //if any error msg
                        }
                    }
                    else
                    {
                        Panel1.Visible = true;
                    }


                    //************************************************************************************
                    if (t_details.Rows.Count > 0)
                    {
                        GVpriority.DataSource = t_details;
                        GVpriority.DataBind();
                    }
                    else
                    {
                        // add option entry

                        for (int i = 1; i <= 20; i++)
                        {
                            ListItem L = new ListItem(i.ToString());
                            DDLcount.Items.Add(L);
                        }
                        DDLcount.Items.Insert(0, "-- Select --");

                        Panel1.Visible = true;
                    }
                }
            }

            protected void DDLcount_SelectedIndexChanged(object sender, EventArgs e)
            {
                Panel2.Visible = true;

                int priorCount = int.Parse(DDLcount.SelectedItem.Text);

                DataTable t_proirNo = new DataTable();
                t_proirNo.Columns.Add("PriorityNo", typeof(int));
                for (int i = 1; i <= priorCount; i++)
                {
                    DataRow dt = t_proirNo.NewRow();
                    dt["PriorityNo"] = i.ToString();
                    t_proirNo.Rows.Add(dt);
                }
                DLpriority.DataSource = t_proirNo;
                DLpriority.DataBind();

                foreach (DataListItem item in DLpriority.Items)
                {
                    DropDownList ddlCity = (DropDownList)item.FindControl("DDLcity");
                    ddlCity.DataSource = b.Cities_GetAll();
                    ddlCity.DataTextField = "CityName";
                    ddlCity.DataValueField = "CityId";
                    ddlCity.DataBind();
                    ddlCity.Items.Insert(0, "Select");

                    DropDownList ddlClg = (DropDownList)item.FindControl("DDLcollege");
                    //ddlClg.DataSource = b.College_GetAll();
                    //ddlClg.DataTextField = "CollegeName";
                    //ddlClg.DataValueField = "CollegeId";
                    //ddlClg.DataBind();
                    ddlClg.Items.Insert(0, "Select");

                    DropDownList ddlCou = (DropDownList)item.FindControl("DDLcourse");
                    //ddlCou.DataSource = b.Course_GetAll();
                    //ddlCou.DataTextField = "CourseName";
                    //ddlCou.DataValueField = "CourseId";
                    //ddlCou.DataBind();
                    ddlCou.Items.Insert(0, "Select");

                }
            }

            protected void DDLcity_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList ddlCity = (DropDownList)sender;
                if (ddlCity.SelectedIndex != 0)
                {
                    int cityid = int.Parse(ddlCity.SelectedItem.Value);

                    var row = (DataListItem)ddlCity.NamingContainer;
                    DropDownList ddlCollege = (DropDownList)row.FindControl("DDLcollege");
                    ddlCollege.DataSource = b.College_GetAll_CityId(cityid);
                    ddlCollege.DataTextField = "CollegeName";
                    ddlCollege.DataValueField = "CollegeId";
                    ddlCollege.DataBind();
                    ddlCollege.Items.Insert(0, "select");
                }
                else
                {
                    var row = (DataListItem)ddlCity.NamingContainer;
                    DropDownList ddlCollege = (DropDownList)row.FindControl("DDLcollege");
                    ddlCity.SelectedIndex = 0;
                    Response.Write("<script>alert('Please Select City')</script>");
                    ddlCollege.Items.Clear();
                    ddlCollege.Items.Insert(0, "Select");

                }
            }

            protected void DDLcollege_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList ddlCollege = (DropDownList)sender;
                if (ddlCollege.SelectedIndex != 0)
                {
                    int clgid = int.Parse(ddlCollege.SelectedItem.Value);

                    var row = (DataListItem)ddlCollege.NamingContainer;
                    DropDownList ddlCourse = (DropDownList)row.FindControl("DDLcourse");
                    ddlCourse.DataSource = b.ClgCourse_GetAll_ClgId(clgid);
                    ddlCourse.DataTextField = "CourseName";
                    ddlCourse.DataValueField = "CourseId";
                    ddlCourse.DataBind();
                    ddlCourse.Items.Insert(0, "select");
                }
                else
                {
                    var row = (DataListItem)ddlCollege.NamingContainer;
                    DropDownList ddlCourse = (DropDownList)row.FindControl("DDLcourse");
                    ddlCollege.SelectedIndex = 0;
                    Response.Write("<script>alert('Please Select College')</script>");
                    ddlCourse.Items.Clear();
                    ddlCourse.Items.Insert(0, "Select");
                }
            }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!