ASP.NET Web 窗体级联下拉列表 - 第二个列表的选定值在回发时重置

本文关键字:窗体 Web NET 级联 下拉列表 列表 第二个 ASP | 更新日期: 2023-09-27 18:33:28

我的问题可能微不足道,但我无法提醒自己该怎么做。我有 2 个下拉列表:

<span>
  <asp:DropDownList ID="DDLEditWydzial" runat="server" DataSourceID="SqlListaWydzialow" 
      AutoPostBack="true" DataTextField="NazwaWydzialu" DataValueField="ident" 
      OnSelectedIndexChanged="OnWydzialChanged" EnableViewState="true">
  </asp:DropDownList>
</span>
<span>
  <span style="font-size: 8pt; font-family: Arial CE">
    <asp:DropDownList ID="DDLEditSale" runat="server" EnableViewState="true" 
        AutoPostBack="true">
    </asp:DropDownList>
  </span>
</span>

第一个填充通过SqlDataSource,第二个填充取决于前一个的选择。这是由事件处理程序完成的:

protected void OnWydzialChanged(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sworConnectionString"].ConnectionString);
    conn.Open();
    using (conn)
    {
        SqlCommand command = new SqlCommand("SELECT '-- wybierz salę --' as numer, -1 as ident UNION " +
            "SELECT 'Sala ' + s.numer as numer, s.ident FROM sala s, sala_wydzial sw where s.czyus=0 and sw.id_wydzial=" 
            + DDLEditWydzial.SelectedValue + " and sw.id_sala = s.ident", conn);
        SqlDataReader salaReader = command.ExecuteReader();
        DDLEditSale.AppendDataBoundItems = true;
        DDLEditSale.DataSource = salaReader;
        DDLEditSale.DataTextField = "numer";
        DDLEditSale.DataValueField = "ident";
        DDLEditSale.DataBind();
        conn.Close();
        conn.Dispose();
    }
}

然后,当我从第二个列表中选择值时,回发并在刷新列表包含数据之后,但第二个 DDL 中的任何内容都没有被选中。我已经检查了Page_Load,DDLEditSale是空的。

有什么想法吗?:)

编辑:OnInit和InitializeComponent代码(由ZedGraph生成):

override protected void OnInit(EventArgs e)
{
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
    this.ZedGraphWeb1.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(this.OnRenderGraph);
}

ASP.NET Web 窗体级联下拉列表 - 第二个列表的选定值在回发时重置

我已经SqlDataSources更改为ObjectDataSources,它似乎有效,只需要在会话中保留wydzial的id。当我有一点时间时,我会粘贴代码:)