【问题标题】:ASP.NET Webforms cascading dropdown Lists - second list's selected value resets on postbackASP.NET Webforms 级联下拉列表 - 第二个列表的选定值在回发时重置
【发布时间】:2013-09-30 12:09:50
【问题描述】:

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

【问题讨论】:

  • 为什么要禁用控件上的 Viewstate?
  • AutoPostback 在第二个下拉菜单中设置为 True 有什么特别的原因吗?
  • 很遗憾,我无法复制您的问题,似乎在我的测试项目中运行良好。我有两个下拉菜单都设置为自动回发,第二个下拉菜单保持其选择。我只能假设除了上面的代码之外还有其他东西导致了问题。很奇怪!
  • 不看代码就很难判断,你是在 OnInit 的某个时间点调用 base.OnInit(e) 吗?
  • 别担心,我知道那种感觉!我可以复制您的问题的唯一方法是在 Page 指令中添加 EnableViewState="False" 但如果我在第一个下拉列表中选择另一个项目,它只会清除我的下拉列表。我在您的 OnInit 中看不到问题,所以我认为这不是导致问题的原因。

标签: c# asp.net webforms cascadingdropdown


【解决方案1】:

我已将SqlDataSources 更改为ObjectDataSources,它似乎可以工作,只需要在会话中保留wydzial 的ID。有时间我会贴代码的:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多