簡體   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