繁体   English   中英

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

[英]ASP.NET Webforms cascading dropdown Lists - second list's selected value resets on postback

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

我已经将SqlDataSources更改为ObjectDataSources ,并且似乎可以正常工作,只需要在Session中保留wydzial ID wydzial 我有空的时候会粘贴代码:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM