2013-09-30 40 views
0

我的問題可能很瑣碎,但我無法提醒自己該怎麼做。我有2名下拉列表:ASP.NET Webforms級聯下拉列表 - 第二個列表的選定值在回發時重置

<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); 
} 
+0

爲什麼要禁用控件上的Viewstate? – Damon

+0

這只是一個實驗。對不起;)改變。 – Rufix

+0

我對這個問題有點困惑,你是否在第二個下拉列表中選擇一個項目後說,頁面被刷新,然後第一個下拉列表是空的?或者在第一個下拉列表中沒有選擇(突出顯示)? – Damon

回答

0

我已經改變了SqlDataSourcesObjectDataSources和它似乎工作,只需要保持會話中的wydzial的ID。當我有一點時間時,我會粘貼代碼:)

相關問題