2011-04-18 130 views
1

在我的項目中,我製作了一個表格,其中包含三個下拉列表,每個下拉列表中的公司,部門和職位都以「選擇」作爲默認文本。我感覺公司在表格加載事件下拉列表中,但當用戶選擇公司時默認文本是「選擇」,第二個列表應該填充選定公司的部門,但默認文本應該是「選擇」。同樣,當用戶選擇部門時,第三個列表應填充所選公司選定部門的空缺,但默認文本應爲「選擇」。 我試圖用公司名單和部門名單中的「選定的索引更改」事件分別填寫其他兩個下拉列表。但它不能按我的要求工作。以拼寫方式填寫下拉列表,並將每個下拉列表設置爲其第一個值

這是我的.aspx頁面的代碼。

<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:DropDownList> 
      </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> 

/及以下是我的.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; 

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

請告訴我的問題,其解決方案的代碼。

+0

而你真正的問題是 – 2011-04-18 12:23:51

+0

你得到的實際錯誤是什麼? – 2011-04-18 12:28:22

回答

1

我想你可能想利用jquery cascading drop downs。使用級聯下拉式javascript(jquery)將根據上一個下拉列表中選定的值回調並重新加載相關下拉列表。

相關問題