动态填充的下拉列表在回发时丢失内容

时间:2015-03-19 05:40:38

标签: c# asp.net .net

我花了最后两个小时阅读这个问题的每个可能的解决方案组合,我仍然没有工作。所以很明显我错过了一些东西。如果有一个下拉列表,我填充数据库调用。当列表中选定的索引发生更改时,我回发并使用另一个数据库调用的信息填充一个列表框。但是在帖子后面我放松了原始下拉列表的内容。

我已经验证了下拉列表,并且页面都将EnableViewState设置为true。

我已经尝试了何时填充下拉列表的各种组合:Page_Init,Page_Load with!isPostBack,以及DDL自身的各种附件。

所以我问专家他们是否可以看到任何错误。我是一个表格和命令行程序员进入网络,所以我很可能错过了一些愚蠢的东西。

这是我到目前为止所做的:

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

{

protected void Page_Init(object sender, EventArgs e)
{
    LoadTheSupervisors();
}

protected void Page_Load(object sender, EventArgs e)
{
}    // Page_Load() ...

protected void LoadTheSupervisors()
{
    SqlConnection cnSQL;
    string sql;
    SqlCommand cmSQL;
    SqlDataReader drSQL;

    // Load the supervisors ...
    cnSQL = new SqlConnection(ConfigurationManager.ConnectionStrings["mySQLServer"].ToString());

    try
    {
        cnSQL.Open();
        sql = "SELECT e.Employee, e.First_Name, e.Last_Name " +
                "FROM [PRODUCTION].[dbo].[Employee] e " +
                "WHERE e.Employee IN ( " +
                    "SELECT distinct(e.Supervisor) " +
                    "FROM [PRODUCTION].[dbo].[Employee] e LEFT OUTER JOIN [PRODUCTION].[dbo].[User_Values] uv  " +
                        "ON e.User_Values = uv.User_Values " +
                    "WHERE e.Status = 'Active' AND uv.Text2 = 'No' " +
                ")" +
                "ORDER BY e.Last_Name ASC " +
                "; ";

        cmSQL = new SqlCommand();
        cmSQL.Connection = cnSQL;
        cmSQL.CommandText = sql;
        drSQL = cmSQL.ExecuteReader();

        ddlSupervisors.Items.Clear();

        while (drSQL.Read())
        {   
            string tmpString = drSQL["Last_Name"].ToString() + ", " + drSQL["First_Name"].ToString();
            ddlSupervisors.Items.Add(new ListItem(tmpString, drSQL["Employee"].ToString()));
            //ddlSupervisors.Items.Add(tmpString);
        }    // while ...
    }
    catch (Exception ex)
    {
        tbError.Text = tbError.Text + ";" + ex.Message;
    }
    finally
    {
        if (cnSQL.State == ConnectionState.Open) cnSQL.Close();
        cnSQL.Dispose();
    }    // try/catch/finally ...
}    // LoadTheSupervisors() ...


protected void ddlSupervisors_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlConnection cnSQL;
    string sql;
    SqlCommand cmSQL;
    SqlDataReader drSQL;
    string selectedValue;

    //LoadTheSupervisors();

    selectedValue = ddlSupervisors.SelectedItem.Value.ToString();
    //tbError.Text = tbError.Text + ";" + selectedValue;
    // Load the employees ...
    cnSQL = new SqlConnection(ConfigurationManager.ConnectionStrings["mySQLServer"].ToString());

    try
    {    
        cnSQL.Open();
        sql = "SELECT e.Employee, e.First_Name, e.Last_Name " +
                "FROM [PRODUCTION].[dbo].[Employee] e LEFT OUTER JOIN [PRODUCTION].[dbo].[User_Values] uv  " +
                    "ON e.User_Values = uv.User_Values " +
                "WHERE e.Supervisor = '" + selectedValue + "' " +
                    "AND e.Status = 'Active' AND uv.Text2 = 'No' " + 
                "ORDER BY e.Last_Name ASC " +
                "; ";
        //tbError.Text = tbError.Text + ";" + sql;
        cmSQL = new SqlCommand();
        cmSQL.Connection = cnSQL;
        cmSQL.CommandText = sql;
        drSQL = cmSQL.ExecuteReader();

        ddlSupervisors.Items.Clear();

        while (drSQL.Read())
        {   
            string tmpString = drSQL["Last_Name"].ToString() + ", " + drSQL["First_Name"].ToString();
            lbEmployees.Items.Add(new ListItem(tmpString, drSQL["Employee"].ToString()));
            //ddlSupervisors.Items.Add(tmpString);
        }    // while ...
    }
    catch (Exception ex)
    {
        tbError.Text = tbError.Text + ";" + ex.Message;
    }
    finally
    {
        if (cnSQL.State == ConnectionState.Open) cnSQL.Close();
        cnSQL.Dispose();
    }    // try/catch/finally ...
}    // ddlSupervisors_SelectedIndexChanged() ...

} //班级Payroll_PrintTimeSheets ...

<%@ Page Title="" Language="C#"  EnableViewState="true" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="PrintTimeSheets.aspx.cs" Inherits="Payroll_PrintTimeSheets" %>

    

        

                      

              

<p>
    <asp:TextBox ID="tbError" runat="server" TextMode="MultiLine"></asp:TextBox>
</p>
<p>
    &nbsp;</p>
<p>
    &nbsp;</p>

任何建议?

1 个答案:

答案 0 :(得分:1)

在填充listBox之前,您正在清除selectedIndexChanged事件中dropDownList而不是ListBox的内容

ddlSupervisors.Items.Clear();

将该行更改为:

lbEmployees.Items.Clear();

希望这会有所帮助......