Non vedo il perchè debba ricaricare sempre le combo. Non è necesario dato che le informazioni sono nel viewstate.. a meno che non l'hai disabilitato.
Mi sono fatto una paginetta di esempio con le combo in sequenza usando il db Northwind di sql
Pagina aspx
codice:<%@ Page language="c#" Codebehind="WebForm8.aspx.cs" AutoEventWireup="false" Inherits="TestVariCSharp.WebForm8" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>WebForm8</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:DropDownList id="DropDownList1" style="Z-INDEX: 101; LEFT: 80px; POSITION: absolute; TOP: 48px" runat="server" Width="232px" AutoPostBack="True"></asp:DropDownList> <asp:DropDownList id="DropDownList2" style="Z-INDEX: 103; LEFT: 80px; POSITION: absolute; TOP: 80px" runat="server" Width="232px"></asp:DropDownList> </form> </body> </HTML>
CodeBehind:
codice:using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace TestVariCSharp { /// <summary> /// Summary description for WebForm8. /// </summary> public class WebForm8 : System.Web.UI.Page { protected System.Web.UI.WebControls.DropDownList DropDownList1; protected System.Web.UI.WebControls.DropDownList DropDownList2; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if(!Page.IsPostBack){ GetCategories(); } } #region Web Form Designer generated code 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.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void GetCategories() { SqlConnection cnn = new SqlConnection("Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=localhost"); SqlCommand cmd = new SqlCommand("SELECT * FROM Categories",cnn); SqlDataAdapter da = new SqlDataAdapter(cmd); cnn.Open(); DataTable dtProdotti = new DataTable("Products"); da.Fill(dtProdotti); cnn.Close(); this.DropDownList1.DataSource = dtProdotti; this.DropDownList1.DataTextField = "CategoryName"; this.DropDownList1.DataValueField ="CategoryId"; this.DropDownList1.DataBind(); } private void GetProducts(string categoryid) { SqlConnection cnn = new SqlConnection("Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=localhost"); SqlCommand cmd = new SqlCommand("SELECT * FROM products where categoryid=" + categoryid,cnn); SqlDataAdapter da = new SqlDataAdapter(cmd); cnn.Open(); DataTable dtProdotti = new DataTable("Products"); da.Fill(dtProdotti); cnn.Close(); this.DropDownList2.DataSource = dtProdotti; this.DropDownList2.DataTextField = "ProductName"; this.DropDownList2.DataValueField ="ProductId"; this.DropDownList2.DataBind(); } private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e) { GetProducts(this.DropDownList1.SelectedItem.Value.ToString()); } } }
Fammi sapere
Ciao
Kalman

Rispondi quotando