2014-01-29 61 views
0

我有一個奇怪的後回發問題。我有兩個下拉菜單,項目和收費代碼。項目選擇會填充收費代碼下拉菜單。我注意到,當我選擇收費代碼時,該值會在下拉菜單中出現一秒,然後更改爲下拉列表中的第一個選項。該下拉列表的索引更改觸發,該值是下拉菜單中的第一個選項,而不是所選的選項。我不確定它爲什麼這樣做,但它必須與回發有關。如果這是回發問題,是否有方法來存儲下拉選擇並在重新加載後恢復選擇?請不要建議使用AJAX或更新面板,因爲我們不允許。這裏是我的ASP代碼:級聯下拉和控制狀態(沒有AJAX,jquery或更新面板)

<p> 
    <asp:DropDownList ID="ddlProjects" runat="server" 
     onselectedindexchanged="ddlProjects_SelectedIndexChanged" 
     AutoPostBack="True" Visible="false" > 
    </asp:DropDownList> 
</p> 
    <asp:DropDownList ID="ddlChargeCodes" runat="server" AutoPostBack="true" 
     onselectedindexchanged="ddlChargeCodes_SelectedIndexChanged" Visible="false"> 
    </asp:DropDownList> 
    <p> 

和後面的C#代碼:

 protected void Page_Load(object sender, EventArgs e) 
    { 
     try 
     { 

      if (!IsPostBack) 
      { 
       LoadProjectsDropDown(); 
      }  
     //Rest of Pageload Omitted.... 


     // Initial Population of Project Dropdown 
     protected void LoadProjectsDropDown() 
    { 
     try 
     { // Populate the Projects Drop Down from Table 
      ddlProjects.Items.Clear(); 
      ddlProjects.DataSource = Time_Tracker.BLL.ProjectsManager.GetItems(); 
      ddlProjects.DataTextField = "Project_Name"; 
      ddlProjects.DataValueField = "Project_ID"; 
      ddlProjects.DataBind(); 
      ddlProjects.Items.Insert(0, new ListItem("PLEASE SELECT A PROJECT", 
      "PLEASE SELECT A PROJECT")); 
     } 
     catch (Exception ex) 
     { 
      Utilities.ErrorLog(ex.Message, ex.GetType().ToString(), ex.StackTrace, 
     @"Time_Tracker.txt"); 
     } 
     } 



     // The Index Change portion of the Project Dropdown, which builds the Charge Code dropdown 
     protected void ddlProjects_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     try 
     { // When user selects the Project, Populate Charge Codes for the selected Project 
      ddlChargeCodes.Visible = true; 
      if (!string.IsNullOrEmpty(ddlProjects.SelectedValue)) 
      { 
       ddlChargeCodes.Items.Clear(); 
       ddlChargeCodes.DataSource = Time_Tracker.BLL.TasksManager.GetChargeCodes 
     (ddlProjects.SelectedValue); 
       ddlChargeCodes.DataTextField = "Description"; 
       ddlChargeCodes.DataValueField = "Project_ID"; 
       ddlChargeCodes.DataBind(); 
       ddlChargeCodes.Items.Insert(0, new ListItem("PLEASE SELECT A CHARGE 
     CODE", "PLEASE SELECT A CHARGE CODE")); 
       Utilities.Project = Convert.ToInt16(ddlProjects.SelectedValue); 
      } 
     } 
     catch (Exception ex) 
     { 
      Utilities.ErrorLog(ex.Message, ex.GetType().ToString(), ex.StackTrace, @"Time_Tracker.txt"); 
     } 
    } 


      // The Index Change of the Charge Codes Dropdown 
      protected void ddlChargeCodes_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     try 
     { // When user selects the Charge Code, it shows a summary and asks for the number of hours 
      Utilities.Description = Convert.ToString(ddlChargeCodes.SelectedItem); 
      Utilities.Chargecode = Convert.ToString(ddlChargeCodes.SelectedItem); 
      lblHoursLabel.Visible = true; 
      txtHours.Visible = true; 
      lblConfirmation.Visible = true; 
      btnStarOver.Visible = true; 
      btnOK.Visible = true; 
      lblConfirmation.Text = "Hours on " + Utilities.SelectedDate + " For Charge Code " + Utilities.Description; 
     } 
     catch (Exception ex) 
     { 
      Utilities.ErrorLog(ex.Message, ex.GetType().ToString(), ex.StackTrace, @"Time_Tracker.txt"); 
     } 
    } 

回答

0

今天做了很多故障排除。發現在選擇費用代碼後,它會做一個頁面加載而不觸發「變更索引」事件處理程序。頁面加載完成後,它會觸發索引更改事件,並選擇第一個選擇(索引1)。我與之交談的每個人都表示,它應該像現在這樣。我終於寫出了它的查詢/數據綁定部分來填充下拉菜單,而不是使用存儲過程,現在它可以工作。如果將來有助於人們,我已經包含了代碼更改。

  protected void ddlProjects_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     try 
     { // When user selects the Project, Populate Charge Codes for the selected Project 
      ddlChargeCodes.Visible = true; 
      if (!string.IsNullOrEmpty(ddlProjects.SelectedValue)) 
      {     
       ddlChargeCodes.Items.Clear(); 
       string strConn = Time_Tracker.DAL.DBUtils.SqlConnectionString; 
       SqlConnection con = new SqlConnection(strConn); 
       SqlCommand cmd = new SqlCommand(); 
       cmd.Connection = con; 
       cmd.CommandType = CommandType.Text; 
       cmd.CommandText = "SELECT Charge_Code, Charge_Code + ' - ' + Description AS Description FROM [Time_Tracker].[dbo].[Tasks] WHERE Inactive = 0 AND Project_ID = " + ddlProjects.SelectedValue; 
       DataSet objDs = new DataSet(); 
       SqlDataAdapter dAdapter = new SqlDataAdapter(); 
       dAdapter.SelectCommand = cmd; 
       con.Open(); 
       dAdapter.Fill(objDs); 
       con.Close(); 
       if (objDs.Tables[0].Rows.Count > 0) 
       { 
        ddlChargeCodes.DataSource = objDs.Tables[0]; 
        ddlChargeCodes.DataTextField = "Description"; 
        ddlChargeCodes.DataValueField = "Charge_Code"; 
        ddlChargeCodes.DataBind(); 
        ddlChargeCodes.Items.Insert(0, "PLEASE SELECT A CHARGE CODE"); 
       } 

       Utilities.Project = Convert.ToInt16(ddlProjects.SelectedValue); 
      } 
     } 
     catch (Exception ex) 
     { 
      Utilities.ErrorLog(ex.Message, ex.GetType().ToString(), ex.StackTrace, @"Time_Tracker.txt"); 
     } 
    }