Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it L'avatar di kobe83
    Registrato dal
    Sep 2008
    Messaggi
    89

    [vb.net] Dropdownlist problema postback

    Ciao a tutti!

    Nella mia pagina ho la seguente dropdownlist :

    codice:
    <asp:dropdownlist runat="server" id="ddlarticolo" width="130px" AutoPostBack="true" >
          <asp:listitem runat="server" value=""></asp:listitem>   
    </asp:dropdownlist>
    i vari listitem vengono inseriti al page_load attraverso una sub.

    Al cambio di index della ddl vorrei che venisse effettuato il postback, cosa che non accade.
    Ecco il codice della sub che gestisce l'indexchange:

    codice:
    Sub getprezzoprodotto(ByVal Sender As Object, ByVal E As EventArgs) Handles ddlarticolo.SelectedIndexChanged
        
            conn = New OleDbConnection
            cmd = New OleDbCommand
            
            If Page.IsPostBack Then
                Response.Write("sono nell'indexchange")
            End If
            
            Try
                connettidb()
                
                cmd.CommandText = ("select prezzo_unit from articoli where cod_art='" & ddlarticolo.SelectedValue & "'")
                cmd.ExecuteNonQuery()
                da.SelectCommand = cmd
                da.Fill(ds, "prezzi")
                 
               
                If (ds.Tables("prezzi").Rows.Count > 0) Then
                    tbprezzounit.Text = ds.Tables("prezzi").Rows.Item(0).Item("prezzo_unit")
                End If
            Catch ex As Exception
                Response.Write(ex.Message)
            End Try
            
        
            
        End Sub
    la cosa strana è che al cambio dell'indice avviene il postback solo se al page load metto un response.write casuale e quindi ottengo il mio scopo, se invece tolgo il response.write non succede assolutamente nulla...

    any hint? :master:
    Ci sono TRE tipi di persone al mondo: quelli che ragionano in binario e quelli che no

  2. #2
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    questo è un esempio da adattare:
    codice:
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="a.aspx.vb" Inherits="prove_a" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
            </asp:DropDownList>
            <asp:Label ID="Label1" runat="server" Text="" EnableViewState="false"></asp:Label>
        </div>
        </form>
    </body>
    </html>
    codice:
    Option Strict On
    Imports l = libreria.ModuloWeb
    
    Partial Class prove_a
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Me.IsPostBack Then
                msole.FillListControl(Me.DropDownList1, New ListItem("", ""), New ListItem("text1", "value1"), New ListItem("text2", "value2"), New ListItem("text3", "value3"))
            End If
        End Sub
    
        Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
            Dim dd As DropDownList = DirectCast(sender, DropDownList)
            Me.Label1.Text = String.Format("Selected index: {0}, Selected value: {1}", dd.SelectedIndex, dd.SelectedValue)
    
        End Sub
    End Class
    Pietro

  3. #3
    Utente di HTML.it L'avatar di kobe83
    Registrato dal
    Sep 2008
    Messaggi
    89
    Grazie della pronta risposta!
    Non capisco però dove può essere il mio errore... scusa se sono insistente ma vorrei capire.
    Ci sono TRE tipi di persone al mondo: quelli che ragionano in binario e quelli che no

  4. #4
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    se mandi una pagina completa semplificata si può vedere :master:
    Pietro

  5. #5
    Utente di HTML.it L'avatar di kobe83
    Registrato dal
    Sep 2008
    Messaggi
    89
    purtroppo devo rimandare a lunedì.. il file è sul pc del lavoro..
    Grazie dell'aiuto pietro, ti posto il codice lunedì.
    Ci sono TRE tipi di persone al mondo: quelli che ragionano in binario e quelli che no

  6. #6
    Moderatore di ASP.net L'avatar di djciko
    Registrato dal
    Nov 2002
    Messaggi
    6,887
    posta anche il codice che popola la DDL, quello del page load, mentre il buon pietro ci spiega cos'è "msole.FillListControl"

  7. #7
    Utente di HTML.it L'avatar di kobe83
    Registrato dal
    Sep 2008
    Messaggi
    89
    Eccomi qua, vi posto il codice "incrimato"... ovviamente ho omesso le sub che non ci interessano.



    codice:
    <script runat="server">
        
        
        Dim da As New OleDbDataAdapter()
        Dim ds As New DataSet()
        Dim conn As New OleDbConnection
        Dim cmd As New OleDbCommand
        
        
        Sub page_init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
            
           
                    
        End Sub
    
        '-----------------------------
        
        Sub page_load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            
            If Not Page.IsPostBack Then
                compila_ddl()
            End If
            
            
            
            Dim data As String = Date.Now
            tbdata.Text = data.Substring("0", "10")
            
            'If Page.IsPostBack Then
            'Response.Write("a")
            'End If
                         
        End Sub
        
        '----------------------------
     
           
        Sub compila_ddl()
            
            conn = New OleDbConnection
            cmd = New OleDbCommand
                    
            
            Try
                connettidb()
                
                
                cmd.CommandText = ("select ragione_sociale, cod_cli from clienti")
                cmd.ExecuteNonQuery()
                da.SelectCommand = cmd
                da.Fill(ds, "Clienti")
            
            
                If Not Page.IsPostBack Then
                    If (ds.Tables("Clienti").Rows.Count > 0) Then
                        Dim i As Integer
                
                        For i = 0 To ds.Tables("Clienti").Rows.Count - 1
                            ddlcliente.Items.Add(New ListItem(ds.Tables("Clienti").Rows.Item(i).Item("ragione_sociale"), ds.Tables("Clienti").Rows.Item(i).Item("cod_cli")))
                        Next
                
                    End If
                End If
                
                  
                
                cmd.CommandText = ("select descr_pagamento, cod_pagamento from pagamenti")
                cmd.ExecuteNonQuery()
                da.SelectCommand = cmd
                da.Fill(ds, "Pagamenti")
            
            
                If Not Page.IsPostBack Then
                    If (ds.Tables("Pagamenti").Rows.Count > 0) Then
                        Dim i As Integer
                
                        For i = 0 To ds.Tables("Pagamenti").Rows.Count - 1
                            ddlpagamento.Items.Add(New ListItem(ds.Tables("Pagamenti").Rows.Item(i).Item("descr_pagamento"), ds.Tables("Pagamenti").Rows.Item(i).Item("cod_pagamento")))
                        Next
                
                    End If
                End If
                
                
                cmd.CommandText = ("select cod_art, descr_art from articoli")
                cmd.ExecuteNonQuery()
                da.SelectCommand = cmd
                da.Fill(ds, "Articoli")
            
            
                If Not Page.IsPostBack Then
                    If (ds.Tables("Articoli").Rows.Count > 0) Then
                        Dim i As Integer
                
                        For i = 0 To ds.Tables("Articoli").Rows.Count - 1
                            ddlarticolo.Items.Add(New ListItem(ds.Tables("Articoli").Rows.Item(i).Item("descr_art"), ds.Tables("Articoli").Rows.Item(i).Item("cod_art")))
                        Next
                
                    End If
                End If
                
                cmd.CommandText = ("select cod_tipo, descr_tipo from tipidoc")
                cmd.ExecuteNonQuery()
                da.SelectCommand = cmd
                da.Fill(ds, "Tipi")
            
            
                If Not Page.IsPostBack Then
                    If (ds.Tables("Tipi").Rows.Count > 0) Then
                        Dim i As Integer
                
                        For i = 0 To ds.Tables("Tipi").Rows.Count - 1
                            ddltipodoc.Items.Add(New ListItem(ds.Tables("Tipi").Rows.Item(i).Item("cod_tipo"), ds.Tables("Tipi").Rows.Item(i).Item("cod_tipo")))
                        Next
                
                    End If
                End If
            
           
                
            Catch ex As Exception
                Response.Write(ex.Message)
            End Try
                 
            
            disconnettidb()
        End Sub
        
        
        '-----------------------------------   
        
        
        
        Sub getprezzoprodotto(ByVal Sender As Object, ByVal E As EventArgs) Handles ddlarticolo.SelectedIndexChanged
        
            conn = New OleDbConnection
            cmd = New OleDbCommand
            
                    
            Try
                connettidb()
                
                cmd.CommandText = ("select prezzo_unit from articoli where cod_art='" & ddlarticolo.SelectedValue & "'")
                cmd.ExecuteNonQuery()
                da.SelectCommand = cmd
                da.Fill(ds, "prezzi")
                 
               
                If (ds.Tables("prezzi").Rows.Count > 0) Then
                    tbprezzounit.Text = ds.Tables("prezzi").Rows.Item(0).Item("prezzo_unit")
                End If
            Catch ex As Exception
                Response.Write(ex.Message)
            End Try
            
        
            
        End Sub
    Ci sono TRE tipi di persone al mondo: quelli che ragionano in binario e quelli che no

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 © 2026 vBulletin Solutions, Inc. All rights reserved.