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

时间:2013-09-30 12:09:51

标签: c# asp.net webforms cascadingdropdown

我的问题可能是微不足道的,但我不能提醒自己该怎么做。我有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);
}

1 个答案:

答案 0 :(得分:0)

我已将SqlDataSources更改为ObjectDataSources并且它似乎有效,只需在Session中保留wydzial的ID即可。我有一点时间会粘贴代码:)