Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    [C#] DropDownList e SelectedIndexChanged

    Problemino.

    Ho una pagina formata da 3 combo correlate a cascata e una tabella che si compila dati i valori delle tre combo.
    Le prime 2 combo sono AutoPostBack=true;

    X combo correlate a cascata intendo che selezionato un campo nella prima, mi riempie la seconda...selezionato un campo nella seconda mi riempie la 3...selezionato il campo nella 3 posso compilare la mia tabella tramite un bottone.

    Il problema è il seguente:

    Selezionato il campo nella prima tramite la SelectedIndexChanged riempio la 2...fin qui tutto a posto...

    Selezionato il campo nella seconda viene richiamato nuovamente il SelectedIndexChanged della prima e lo piglio in quel posto...

    Ho verificato anche tramite il codice le righe "autoscritte" da visual studio net

    codice:
    				
    private void InitializeComponent()
    	{    
    		this.ddlProgetto.SelectedIndexChanged += new System.EventHandler(this.ddlProgetto_SelectedIndexChanged);
    		this.ddlLinea.SelectedIndexChanged += new System.EventHandler(this.ddlLinea_SelectedIndexChanged);
    		this.calcolaCosto.Click += new System.EventHandler(this.calcolaCosto_Click);
    		this.Load += new System.EventHandler(this.Page_Load);
    	}
    Come vedete ogni combo ha il suo evento SelectedIndexChanged.

    No so + cosa fare!!!
    Il mio mononeurone ormai ha fatto le valigie e m'ha lasciato in completa solitudine...yuuuuhuuuuu, c'è neeessuuuuuunoooooo????...gluglugl...u

  2. #2
    Perchè si scatena nuovamente quell'evento?
    Il bind delle combo alla tabelle lo fai ad ogni round trip??

    Ciao
    Kalman
    Kalman

  3. #3
    Sono obbligato a farlo dato che ad ogni roundtrip (come lo chiami tu) le combo sono svuotate dai campi...ho provato a mettere un if(!IsPostBack) e difatti i campi delle combo mi rimarrebbero vuoti.
    Quindi devo ricompilare la prima combo tutte le volte.

    codice:
    private void Page_Load(object sender, System.EventArgs e)
    {
    	ddlProgetto.ID="ctl_1";
    	ddlProgetto.AutoPostBack=true;
    	ddlProgetto.Riempi(business.progetto.Componiprogetto(dbwrap.progetto.GetByFilter("")),"id_progetto","nome");
    
    	ddlProgetto.Width=200;
    
    	ddlLinea.AutoPostBack=true;
    	ddlLinea.Width=200;
    	ddlFase.Width=200;
    }
    
    		
    private void calcolaCosto_Click(object sender, System.EventArgs e)
    {	
    	/// Calcolo dei costi e output dati su tabella.
    }
    
    private void ddlProgetto_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    	if(ddlProgetto.ValoreIdLong!=0)
    	{
    		ddlLinea.Riempi(business.linea.Componilinea(dbwrap.linea_join.GetByFilter(" where progetto.id_progetto="+ddlProgetto.ValoreIdLong)),"id_linea","nome");
    	}
    }
    
    private void ddlLinea_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    	if(ddlLinea.ValoreIdLong!=0 && ddlProgetto.ValoreIdLong!=0)
    	{
    		ddlFase.Riempi(business.fase.Componifase(dbwrap.fase.GetByFilter(" where id_progetto="+ddlProgetto.ValoreIdLong)),"id_fase","nome");
    	}
    }
    ddlProgetto è la prima combo
    ddlLinea la seconda
    ddlFase la terza.
    Il mio mononeurone ormai ha fatto le valigie e m'ha lasciato in completa solitudine...yuuuuhuuuuu, c'è neeessuuuuuunoooooo????...gluglugl...u

  4. #4
    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
    Kalman

  5. #5
    Il risultato che ottieni tu lo ottengo pure io...ma quando seleziono la 2° combo per fare il binding della 3° il figlio di putt...mi scatena l'evento OnSelectedItemChanged della 1°...così rimango in una fase di stallo...
    Il mio mononeurone ormai ha fatto le valigie e m'ha lasciato in completa solitudine...yuuuuhuuuuu, c'è neeessuuuuuunoooooo????...gluglugl...u

  6. #6
    Help!!

    Non so + cosa fare...

    Le ho provate tutte...anche a ripartire da zero...niente da fare...
    Quando cambio item nella seconda combo per fare il binding della terza mi parte l'evento OnSelectedItemChange della prima...
    :master: :master: :master:

    Perchè
    Il mio mononeurone ormai ha fatto le valigie e m'ha lasciato in completa solitudine...yuuuuhuuuuu, c'è neeessuuuuuunoooooo????...gluglugl...u

  7. #7
    Utente di HTML.it L'avatar di Legnetto
    Registrato dal
    May 2002
    Messaggi
    1,419
    Perchè se ricarichi ad ogni postback la dropdrownlist, quando la pagina viene rimandata al server ti ripristina sempre la situazione iniziale.
    Se non hai impostato attributi Enableviewstate="false" ne alla pagina, ne alla singola ddl, non devi ricaricare ogni volta.
    La viewstate è la comodità(e l'handicap!!)maggiore di .Net.
    Ciao
    Legnetto

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.