开发者

Fill dropdown list in casceding way and set each dropdown list before its first value

In my project I am making one form which contains three dropdown lists each for company,department and vacancies with "Select" as their default text. I am feeling company dropdown list at form load event but default text is "Select" now when user select company, second list should be filled with departments of selected company but default text should be "select". Again when user select department,the third list should be filled with vacancies on the selected department of selected company but default text should be "select". I am trying to fill other two dropdown list with "selected index change" event of company list and department list respectively. but its not working as I want.

Here is my code for .aspx page.

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <table width="100%">
        <tr>
            <td colspan="4">
                &nbsp;</td>
        </tr>
        <tr>
            <td align="center" class="tdtitle" colspan="4">
                Search Candidates
            </td>
        </tr>
        <tr>
            <td colspan="4">
                &nbsp;</td>
        </tr>
        <tr>
            <td class="RowHeight" width="20%">
                Select Company</td>
            <td width="30%">
                <asp:DropDownList ID="companyList" runat="server" AutoPostBack="True" 
                 onselectedindexchanged="companyList_SelectedIndexChanged" Width="150px">
                <asp:ListItem>-Select Company-</asp:ListItem></asp:DropDownList>
            </td>
            <td width="20%">
                Select Department</td>
            <td width="30%">
                <asp:DropDownList ID="deptList" runat="server" AutoPostBack="True" 
                    Width="150px" onselectedindexchanged="deptList_SelectedIndexChanged" onclick="Validate();">
                <asp:ListItem>-Select Department-</asp:ListItem></asp:DropDownLi开发者_Python百科st>
            </td>
        </tr>
        <tr>
            <td class="RowHeight" width="20%">
                Select Vacancy</td>
            <td colspan="3" width="*">
                <asp:DropDownList ID="vacanyList" runat="server" Width="200px">
                <asp:ListItem>-Select Vacancy-</asp:ListItem></asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td colspan="4">
                &nbsp;</td>
        </tr>
        <tr>
            <td colspan="4">
                &nbsp;</td>
        </tr>
        <tr>
            <td colspan="4">
                &nbsp;</td>
        </tr>
        <tr>
            <td colspan="4">
                &nbsp;</td>
        </tr>
        <tr>
            <td align="center" colspan="4">
                <asp:Label ID="notifyLbl" runat="server" Font-Size="Large" ForeColor="Red" 
                    Text="Label"></asp:Label>
            </td>
        </tr>
        <tr>
            <td colspan="4">
                &nbsp;</td>
        </tr>
    </table>
 <script type="text/javascript">

     function alertOnBadSelection() {
         var select = document.getElementById('companyList');
         if (select.options[select.selectedIndex].value == "-Select Company-") {
             alert('Please Select Company!');
             return false;
         }
     }

    </script>
</asp:Content>

/And below is the code for my .aspx.cs page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class HR_Department_searcAppForVac : System.Web.UI.Page
{
    DataOperation oDo = new DataOperation();
    protected void Page_Load(object sender, EventArgs e)
    {
       // deptList.Attributes.Add("onchange", "alertOnBadSelection();");

        notifyLbl.Visible = false;
        if (!IsPostBack)
        {
            try
            {
                DataTable objCmpnyTable = oDo.DropDownList("select * from tblCompanyMaster");
                if (objCmpnyTable.Rows.Count > 0)
                {
                    foreach (DataRow Row in objCmpnyTable.Rows)
                    {
                        companyList.Items.Add(new ListItem(Row["CompName"].ToString(), Row["CompId"].ToString()));
                    }
                }
                else
                {
                    notifyLbl.Visible = true;
                    notifyLbl.Text = "There is not any company in the list.";
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        else
        {
            //deptList.SelectedIndex = -1;
            //vacanyList.SelectedIndex = 1;
        }
    }
    protected void companyList_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (companyList.SelectedIndex > 0)
            {
                deptList.Items.Clear();
                string str = "select * from vwCompWiseList where CompId=" + companyList.SelectedValue;
                DataTable objDeptTable = oDo.DropDownList("select DeptId,DeptName from vwCompWiseDept where CompId= "+companyList.SelectedValue);
                if (objDeptTable.Rows.Count > 0)
                {
                    foreach (DataRow Row in objDeptTable.Rows)
                    {
                        deptList.Items.Add(new ListItem(Row["DeptName"].ToString(), Row["DeptId"].ToString()));
                    }

                }
            }
            else
            {
                notifyLbl.Visible = true;
                notifyLbl.Text = "Select Company....";
            }
        }
        catch (Exception)
        {

            throw;
        }    
    }
    protected void deptList_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (deptList.SelectedIndex > 0)
            {
                vacanyList.Items.Clear();
                DataTable objVacancytbl = oDo.DropDownList("select VacId,VacTitle from tblVacancyMaster where DeptId =" + deptList.SelectedValue + " and CompId=" + companyList.SelectedValue);
                if (objVacancytbl.Rows.Count > 0)
                {
                    foreach (DataRow Row in objVacancytbl.Rows)
                    {
                        vacanyList.Items.Add(new ListItem(Row["VacTitle"].ToString(), Row["VacId"].ToString()));
                    }
                    vacanyList.SelectedIndex = -1;
                    vacanyList.ClearSelection();

                }
                else
                {
                    notifyLbl.Visible = true;
                    notifyLbl.Text = "Ops..!There is no available vacancy....";
                }
            }
            else
            {
                notifyLbl.Visible = true;
                notifyLbl.Text = "Select Department...";
            }
        }
        catch (Exception)
        {

            throw;
        }
    }
}

Please show my problem and its solutions.


I think you may want to leverage jquery cascading drop downs . Using a cascading drop down javascript (jquery) will call back and reload dependent drop downs based on the value(s) selected in the previous drop down.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜