Pagina 2 di 2 primaprima 1 2
Visualizzazione dei risultati da 11 a 20 su 20

Discussione: Repeater e Button

  1. #11
    Utente di HTML.it L'avatar di mexican
    Registrato dal
    Oct 2001
    residenza
    cava de tirreni
    Messaggi
    3,541
    Ciao a tutti, mi unisco alla discussione.

    Ecco cosa ho io:

    codice:
    Protected Sub Repeater1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles Repeater1.ItemCommand
            If e.CommandName = "Salva" Then
                Dim oMyPrimaryKey As Object = e.CommandArgument
                messaggio.Text = oMyPrimaryKey
            End If
    
        End Sub
    
    
        Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
            DirectCast(e.Item.FindControl("MyButton"), Button).CommandArgument = 
            DirectCast(e.Item.FindControl("MyButton"), Button).CommandName = "Salva"
        End Sub
    Ora io come CommandArgument vorrei assegnare il campo ID preso dal DB mentre scorro il repeater... come posso fare?

  2. #12
    Utente di HTML.it L'avatar di mexican
    Registrato dal
    Oct 2001
    residenza
    cava de tirreni
    Messaggi
    3,541
    Originariamente inviato da mexican
    Ciao a tutti, mi unisco alla discussione.

    Ecco cosa ho io:

    codice:
    Protected Sub Repeater1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles Repeater1.ItemCommand
            If e.CommandName = "Salva" Then
                Dim oMyPrimaryKey As Object = e.CommandArgument
                messaggio.Text = oMyPrimaryKey
            End If
    
        End Sub
    
    
        Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
            DirectCast(e.Item.FindControl("MyButton"), Button).CommandArgument = 
            DirectCast(e.Item.FindControl("MyButton"), Button).CommandName = "Salva"
        End Sub
    Ora io come CommandArgument vorrei assegnare il campo ID preso dal DB mentre scorro il repeater... come posso fare?
    Scusate :-) ci sono riuscito ecco come:
    e.Item.DataItem("ID")

    Ora l'unica cosa che non riesco a fare... una volta messa una textbox e assegnato un text non riesco a recuperare il text sull'evento: ItemCommand
    Come posso fare?

    Scusate ancora... ho fatto anche questo e va nell' ItemCommand:

    Dim y = DirectCast(e.Item.FindControl("TextBox1"), TextBox).Text

  3. #13
    Utente di HTML.it
    Registrato dal
    Sep 2007
    Messaggi
    226
    Ciao...........
    :-) sono ancora qui per chiedere il vostro aiuto........

    All'ora ho creato così il mio Repeater:
    <asp:TextBox ID=<%#DataBinder.Eval(Container.DataItem, "id") %> runat="server"></asp:TextBox>
    <asp:Button ID="removeEmail" CommandName="Ordina" CommandArgument=<%#DataBinder.Eval(Container.DataI tem, "id") %> runat="server" Text="Ordina" ToolTip="Ordinare" />


    Ora da VB non riesco a capire come recuperare la textbox con ID = a quello configurato.
    Diciamo che :
    Public Sub MyRepeater_onItemCommand(ByVal Sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs )
    Dim pippo As String
    pippo = e.CommandArgument


    End Sub

    Mi recupera esattamente ID dell'elemento TextBox.
    Adesso come faccio a prendere il valore inserito dall'utente nella textbox specifica per l'id recuperato.

    Vorrei praticamente fare inserire un valore numerico nella text box all'altezza della riga, quanto l'utente clicca sul button recuperare la riga (questo già lo faccio) e la qta inserita nella textbox e passare il tutto a db per elaborazione.
    Mi manca il recupero della text box.

  4. #14
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    hai guardato la risposta precedente, ultima riga? dovrebbe essere la soluzione
    Pietro

  5. #15
    Utente di HTML.it L'avatar di mexican
    Registrato dal
    Oct 2001
    residenza
    cava de tirreni
    Messaggi
    3,541
    Originariamente inviato da pietro09
    hai guardato la risposta precedente, ultima riga? dovrebbe essere la soluzione
    in effetti si l'ho detto prima :-)!!!
    Se vedi il mio codice è VB e fa quello che ti serve. e serviva anche a me.

  6. #16
    Utente di HTML.it
    Registrato dal
    Sep 2007
    Messaggi
    226
    .......... ho provato ma non mi sembra sia proprio quello che serve a me...... forse non ho capito bene l'utilizzo del comando.

    Io ho questo scenario per ogni riga del repeater:
    TextBox Button
    TextBox Button
    TextBox Button
    TextBox Button


    Nella textBox l'utente inserisce un valore, numerico , quindi potrebbe inserire 1, 2, 50 , 10, ecc....

    Quando salvo mi è facile inviare il campo id del Button, ma non riesco a prendermi il valore TextBox specifico, magari solo della 3 riga, oppure solo della 4°... ecc.

    Sono un pò in difficoltà .......... anche perchè se guardo altri esempi in rete lo hanno tutti istanziato in modo diverso dal mio il repeater,,, io non sono molto esperta quindi alla fine trovo difficoltà ad applicare gli esempi al mio codice.

  7. #17
    Utente di HTML.it L'avatar di mexican
    Registrato dal
    Oct 2001
    residenza
    cava de tirreni
    Messaggi
    3,541
    Lato pubblico hai questo repeater (formattazione e campi a piacere tuo, qui c'è anche una select e un checkbox):

    codice:
    <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
                <ItemTemplate>
                    <div><%#Container.DataItem("articolo")%> - 
                        <asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox> - 
                        <asp:CheckBox ID="CheckBox1" runat="server" /> -
                        <asp:DropDownList ID="DropDownList1" runat="server">
                            <asp:ListItem>Uomo</asp:ListItem>
                            <asp:ListItem>Donna</asp:ListItem>
                        </asp:DropDownList>
                        <asp:Button ID="MyButton" runat="server" Text="Salva" />
                        
                        </div>
                </ItemTemplate>
            </asp:Repeater>
    Lato Vb questo:

    codice:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            connetti()
            Try
                If Not IsPostBack Then
    
                    strsql = "Select * From Articoli"
                    cmdSql = New MySqlCommand(strsql, conn)
                    rs = cmdSql.ExecuteReader()
                    Repeater1.DataSource = rs
                    Repeater1.DataBind()
                    rs.Close()
                    conn.Close()
    
                End If
            Catch errore As Exception
                messaggio.Text = "Si è verificato il seguente errore: " & errore.Message
            Finally
                conn.Close()
            End Try
        End Sub
    
        Protected Sub Repeater1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles Repeater1.ItemCommand
            If e.CommandName = "Salva" Then
                Dim oMyPrimaryKey As Object = e.CommandArgument
                Dim y = DirectCast(e.Item.FindControl("TextBox1"), TextBox).Text
                Dim z = DirectCast(e.Item.FindControl("CheckBox1"), CheckBox).Checked
                Dim x = DirectCast(e.Item.FindControl("DropDownList1"), DropDownList).SelectedItem.Value
                messaggio.Text = oMyPrimaryKey & "-" & y & "-" & z & "-" & x
            End If
    
        End Sub
    
        Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
            DirectCast(e.Item.FindControl("MyButton"), Button).CommandArgument = e.Item.DataItem("idarticolo")
            DirectCast(e.Item.FindControl("MyButton"), Button).CommandName = "Salva"
            DirectCast(e.Item.FindControl("TextBox1"), TextBox).Text = e.Item.DataItem("articolo")
    
    
        End Sub
    Al click del button (salva) ti scrivo nella label i valori (e Id della riga)

  8. #18
    Utente di HTML.it
    Registrato dal
    Sep 2007
    Messaggi
    226
    ok ........... questa sera provo :-)

    Grazie mille

  9. #19
    Utente di HTML.it
    Registrato dal
    Sep 2007
    Messaggi
    226
    stò provando..... ma a me non funziona :-(

    Alcuni parametri li ho impostati direttamente da codice :
    <asp:TextBox ID="TextBox1" runat="server" Text="" Width="50"></asp:TextBox>

    <asp:Button ID="MyButton" CommandName="Ordina" runat="server" CommandArgument=<%#DataBinder.Eval(Container.DataI tem, "id") %> Text="Ordina" />


    Poi nella parte Vb ho scritto il codice solo per associare
    Protected Sub MyRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles MyRepeater.ItemDataBound
    DirectCast(e.Item.FindControl("TextBox1"), TextBox).Text = e.Item.DataItem("idarticolo")


    End Sub



    Quando lancio il debug mi esce errore:
    Variabile oggetto o variabile del blocco With non impostata.
    NullReferenceException was unhandled by user code

  10. #20
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    Io ho un esempio un poco diverso.
    Nel repeater ho un <input type text

    <input type="text" value="<%# NullToString(Eval("testo"))%>" name="testo_<%# Eval("id")%>" />

    quello che importa, per recuperare i dati, è il name, composto da una parte fissa "testo_" più l'id del record, così da essere univoco e recuperabile nel server.

    Il codice (funzionante) è:

    pagina
    codice:
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="salva_con_textbox.aspx.vb" Inherits="CorsoApogeo_wrox_comandi_ed_associazione_di_dati_repeater_salva_con_textbox" %>
    
    <!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 id="Head1" runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Repeater ID="Repeater1" runat="server" EnableViewState="true">
                <HeaderTemplate>
                    <table border="1">
                        <tr>
                            <th>id</th>
                            <th>testo</th>
                        </tr>
                </HeaderTemplate>
                <ItemTemplate>
                        <tr>
                            <td><%# Eval("id")%></td>
                            <td>
                                <input type="text" value="<%# NullToString(Eval("testo"))%>" name="testo_<%# Eval("id")%>" />
                                <asp:Button ID="Button1" runat="server" Text="salva" CommandName="salva" CommandArgument='<%# Eval("id")%>' />
                            </td>
                        </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>
        </div>
        </form>
    </body>
    </html>
    codice vb
    codice:
    Option Strict On
    Imports l = libreria.ModuloWeb
    
    Partial Class CorsoApogeo_wrox_comandi_ed_associazione_di_dati_repeater_salva_con_textbox
        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
                BindData()
            End If
    
        End Sub
    
        Protected Sub BindData()
            msole.DataBind(Me.Repeater1, gl.StringaConnessioneTest, "select * from campi")
        End Sub
    
    
        Protected Sub Repeater1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles Repeater1.ItemCommand
            If e.CommandName = "salva" Then
                'recupero l'id del record
                Dim id As String = e.CommandArgument.ToString
    
                'nome della variabile ricevuta
                Dim testo As String = "testo_" & id
    
                'valore della variabile ricevuta
                Dim testo_value As String = Request.Form(testo)
    
                'salvataggio campo testo
                Using con As New OleDbConnection(gl.StringaConnessioneTest)
                    con.Open()
    
                    Dim command As OleDbCommand = con.CreateCommand()
                    command.CommandText = "UPDATE [CAMPI] SET [TESTO] = ? WHERE [ID] = ? "
    
                    command.Parameters.Clear()
    
                    command.Parameters.Add("testo", OleDbType.VarChar, 50).Value = l.StringNullToDBNull(testo_value)
                    command.Parameters.Add("id", OleDbType.Integer).Value = id
    
                    command.ExecuteNonQuery()
    
                End Using
    
            End If
    
            'rilettura dati
            BindData()
        End Sub
    End Class
    Pietro

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.