Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di Sonikag
    Registrato dal
    Mar 2004
    Messaggi
    2,080

    Button che non esegue quello che deve

    Ho un repeater e nel fondo di ogni riga ho un place holder (ph4) dove creo dinamicamente un bottone che dovrebbe eseguire una funzione.

    nell'itemdatabound faccio questo:
    codice:
    				if(e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem) 
    				{ 
    					Button3 = new Button(); 
    					e.Item.FindControl("PH4"); 
    					Control myControl4 = e.Item.FindControl("PH4"); 
    					Button3.ID= "agg"+((DataRowView)e.Item.DataItem)[0].ToString();
    					Button3.Text="AGGIORNA"; 
    					Button3.CommandArgument= ((DataRowView)e.Item.DataItem)[1].ToString();
    					Button3.Command += new CommandEventHandler(myButton3_Command); 
    					Button3.CssClass = "bt";
    					myControl4.Controls.Add(Button3); 
    					
    					Button3.CommandArgument= ((DataRowView)e.Item.DataItem)[0].ToString();
    
    				}
    In fase di visualizzazione tutto bene... il bottone viene creato e al click viene ricaricata la pagina ma non viene esguita myButton3_Command che per il momento ha solo un response.write

  2. #2
    Moderatore di ASP.net L'avatar di djciko
    Registrato dal
    Nov 2002
    Messaggi
    6,887
    Credo che tu debba intercettare l'evento del Repeater, prima...

  3. #3
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    Sonikag := fonte enesauribile di problematiche

    ho scopiazzato un po' il tuo codice e ne ho fatto un esempietto che, manco a dirlo, funziona al primo colpo

    pagina

    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>Pagina senza titolo</title>
        <link href="../App_Themes/Tema1/StyleSheet.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <form id="form1" runat="server">
            <h3>Aggiunta pulsante e gestore evento da codice</h3>
            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
            <hr />
            <asp:Repeater ID="Repeater1" runat="server" EnableViewState="false">
                <HeaderTemplate>
                    <table border="1">
                        <tr>
                            <td>ID</td>
                            <td>Nazione</td>
                            <td>Pulsante</td>
                        </tr>
                </HeaderTemplate>
    
                <ItemTemplate>
                        <tr>
                            <td>
                                <%#NullToSpace(Eval("id"))%>
                            </td>
                            <td>
                                <%# NullToSpace(Eval("nazione")) %>
                            </td>
                            <td>
                                <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
                            </td>
                        </tr>
                
                </ItemTemplate>
                
                
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>
        </form>
    </body>
    </html>
    codice in puro basic
    codice:
    Option Strict On
    
    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
            libreria.DataBind(Me.Repeater1, StringaConnessioneTest, "select * from citta")
        End Sub
    
        Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
            Select Case e.Item.ItemType
                Case ListItemType.AlternatingItem, ListItemType.Item
                    Dim ph As PlaceHolder = DirectCast(e.Item.Controls(1), PlaceHolder)
                    Dim bt As New Button()
                    Dim dr As System.Data.Common.DbDataRecord = DirectCast(e.Item.DataItem, System.Data.Common.DbDataRecord)
                    bt.Text = dr("nazione").ToString()
                    bt.Width = Unit.Pixel(100)
                    bt.CommandArgument = dr("id").ToString & "_" & dr("nazione").ToString
                    AddHandler bt.Command, AddressOf bt_command
    
                    ph.Controls.Add(bt)
    
            End Select
        End Sub
    
        Private Sub bt_command(ByVal sender As Object, ByVal e As CommandEventArgs)
            Me.Label1.Text = e.CommandArgument.ToString
        End Sub
    End Class
    ps. visto che mi funziona me lo conservo pure nella raccolta privata
    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.