0

我有一个奇怪的回邮问题。我有两个下拉列表,项目代码和费用代码。项目选择填充收费代码下拉菜单。我注意到,当我选择一个收费代码时,该值会出现在下拉列表中一瞬间,然后更改为下拉列表中的第一个选项。该下拉列表的索引更改触发,该值是下拉列表中的第一个选项,而不是选择的选项。我不确定它为什么这样做,但它一定与回发有关。如果是回发问题,有没有办法存储下拉选择并在重新加载后恢复选择?请不要建议使用 AJAX 或更新面板,因为我们不允许。这是我的asp代码:

 <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");
        }
    }
4

1 回答 1

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");
        }
    }
于 2014-01-30T16:42:39.930 回答